Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X
This step is optional. Additional instructions are provided for developers who are interested in interfacing with sensors.
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.
git
sudo apt-get install git-core
sudo apt-get update
then try to install Git again.
cd microserver
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo apt-get install build-essential python-dev
sudo python setup.py install
WARNING: Double check your connections before applying power. Mistakes can destroy the sensor and the Raspberry Pi!
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.
A dozen lines need to be added to the file PiTemplate.lua file in the /microserver/etc/custom/templates directory.
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 }
local voltCmd = io.popen("vcgencmd measure_volts core")
Add the line:
local sensorCmd = io.popen("./Adafruit_Python_DHT/examples/AdafruitDHT.py 11 4")
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
ps -efl
kill -9 PID#
sudo ./luaScriptResource
NOTE: The temp and humidity Properties will be updated every 30 seconds.
Congratulations! You've successfully completed the Connect Raspberry Pi to ThingWorx guide, and learned how to:
We recommend the following resources to continue your learning experience:
Capability | Guide |
Manage | Data Model Introduction |
Build | Get Started with ThingWorx for IoT |
If you have questions, issues, or need additional information, refer to:
Resource | Link |
Community | Developer Community Forum |
Support | Edge SDKs and WebSocket-based Edge MicroServer (WS EMS) Help Center |
External | Raspberry Pi Documentation |