cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Connect Raspberry Pi to ThingWorx Part 4

No ratings

 

 

Step 12: Connect to Temperature Sensor

 

This step is optional. Additional instructions are provided for developers who are interested in interfacing with sensors.

 

DHT11_pinout-0-0-0-0-1502296397.png

The DHT11 and DHT22 digital temperature and humidity sensors are inexpensive and available from several sources:

The Raspberry Pi does not come with any built-in analog to digital conversion capability and because these sensors are digital they can be interfaced easily with a Raspberry Pi. We will be using a Python library developed by Adafruit that simplifies interfacing with these sensors.

 

Install Adafruit Python Library for Sensors

 

  1. We will use Git to download the Adafruit DHT11 Python from GitHub. Check if Git is already installed by opening a command window and typing the command:
    git
    • If you see a "command not found" error message use this command to install Git:
      sudo apt-get install git-core
    • If you get an error installing Git, run the command:
      sudo apt-get update

      then try to install Git again.

  2. Change into the EMS directory:
    cd microserver
  3. Download the Adafruit library with this command:
    git clone https://github.com/adafruit/Adafruit_Python_DHT.git
  4. Change into the directory that was just downloaded:
    cd Adafruit_Python_DHT
  5. Install Python build libraries:
    sudo apt-get install build-essential python-dev
  6. Build and install the library with this command:
    sudo python setup.py install

 

Connect Sensor to Raspberry Pi

  1. Power down the Raspberry Pi before making any wire connections. To prevent any flash memory corruption, enter the command shutdown -h now then wait a few seconds for it to complete before disconnecting the power supply.
  2. Use female-to-female jumper wires to connect the sensor as shown below. The black wire is connected to ground, the red wire is 5v or VCC, and the yellow wire carries is the digital signal.

WARNING: Double check your connections before applying power. Mistakes can destroy the sensor and the Raspberry Pi!

Temp_sensor_raspberry_pi_bb-0-0-0-0-1502297003.png

 

3. Apply power and boot the Raspberry Pi.

4. Change into the EMS directory:

 

cd microserver

5. Test the sensor with this commmand:

 

./Adafruit_Python_DHT/examples/AdafruitDHT.py 11 4

 

In a few a seconds the current temperature and humidity will be displayed.

  • Change the 11 parameter to 22 if you are using the DHT22 sensor.
  • The 4 parameter is the GPIO pin number of the Raspberry Pi that is conneCted to the sensor's signal pin. This command is the same command the luaScriptResource will use to get temperature and humidity readings.

 

Modify Lua template file

A dozen lines need to be added to the file PiTemplate.lua file in the /microserver/etc/custom/templates directory.

 

  1. After the line:
    properties.cpu_volt  =     { baseType="NUMBER",  pushType="ALWAYS", value=0 }

    Add the two lines:

    properties.temp  =     { baseType="NUMBER",  pushType="ALWAYS", value=0 }
    properties.humidity  =     { baseType="NUMBER",  pushType="ALWAYS", value=0 }
  2. After the line:
    local voltCmd = io.popen("vcgencmd measure_volts core")

    Add the line:

    local sensorCmd = io.popen("./Adafruit_Python_DHT/examples/AdafruitDHT.py 11 4")
  3. After the line:
    properties.cpu_volt.value = s

    Add these 9 lines:

    -- set property temp and humidity
    local sensor = sensorCmd:read("*a")
    log.debug("[PiTemplate]",string.format("raw sensor %s", sensor))
    s = string.match(sensor,"Temp=(%d+\.%d+)");
    log.debug("[PiTemplate]",string.format("scaled temp %.1f", s))
    properties.temp.value = s
    s = string.match(sensor,"Humidity=(%d+\.%d+)");
    log.debug("[PiTemplate]",string.format("scaled humidity %.1f", s))
    properties.humidity.value = s
  4. Stop and then restart luaScriptResource by using the following commands.
    ps -efl
    • Will list all running processes, note the number in the PID column for ./lusScriptResource
      kill -9 PID#
    • Replace PID# with number noted above and the process will be ended.
  5. Run LuaScriptResource by executing the following command:
    sudo ./luaScriptResource

 

Update Properties of PiThing

 

  1. Log onto ThingWorx Foundation server.
  2. Click on the Home icon in Composer then broswse to Things > PiThing > Properties and click Manage Bindings button.
     

    manage-binding.png

  3. Click on the Remote tab, then drag and drop the temp and humidity Properties one at a time to the green plus sign next to Create new Properties.
  4. Click Done to close the binding window, then click Save.

NOTE: The temp and humidity Properties will be updated every 30 seconds.

 

 

Step 13: Next Steps

 

Congratulations! You've successfully completed the Connect Raspberry Pi to ThingWorx guide, and learned how to:

 

  • Set up Raspberry Pi
  • Install, configure and launch the EMS
  • Connect a remote device to ThingWorx

 

Learn More

 

We recommend the following resources to continue your learning experience:

 

CapabilityGuide
ManageData Model Introduction
BuildGet Started with ThingWorx for IoT

 

Additional Resources

 

If you have questions, issues, or need additional information, refer to:

 

ResourceLink
CommunityDeveloper Community Forum
SupportEdge SDKs and WebSocket-based Edge MicroServer (WS EMS) Help Center
ExternalRaspberry Pi Documentation

 

Version history
Last update:
‎Nov 11, 2022 04:14 PM
Updated by:
Labels (2)
Contributors