Introduction: Python and Raspberry Pi Temperature Sensor

It's been quite a long time, when I’d first posted this RPi sample. Time passed, I'd gathered some feedback form the community, took into account the comments, and this sample is ready to be published at Instructables for the public interested in microcontrollers and the Internet of Things.

What does this example show? How easy, in just a few hours, you can build your first IoT fun project. Building a remote temperature sensor and a remotely controlled LED can be a very good starting point.

It involves replacing LED with power relay and controlling light in your room, adding humidity sensors and, say, light sensors to better monitor the environment and report everything on your mobile device.

This sample can work on virtually any embedded linux board that has Python and 1-wire kernel drivers. It’s possible to connect a number of 1-wire devices to the same line.

You can check the video and follow the 1-6 steps instructions.

Step 1: You Will Need

  • Rasbperry Pi Rev. B (http://raspberrypi.org) or any other inexpensive Embedded Linux development board, like gumstix, or beaglebone.
  • Dallas 1-wire temperature sensor: DS18B20 – available everywhere :-).
  • Any LED
  • 510 ohm resistor for LED and 4.7K ohm (or so) resistor for DS18B20
  • Breadboard

The schematic is as follows (pic.1), the sensor will be powered from RasPi’s Pin 2. No external power needed.

Step 2: Configuring Raspberry Pi

  1. Download SD card image of Raspbian and expand it onto an SD card.
  2. Find a reliable power supply – Micro-USB smartphone chargers are usually a good choice. It should be capable of supplying 5V up to 1A (if you want to use a wifi module with it).
  3. You don’t have to use HDMI display and USB keyboard – you can connect it to your laptop or network using Ethernet and let RasPi obtain the DHCP address and connect to it using ssh using pi/raspberry as login/password.
  4. Connect LED/Sensor as shown on Figure 1 above.
  5. Load 1-wire kernel modules that come pre-installed but not loaded: sudo modprobe w1-gpio and then sudo modprobe w1_therm
  6. Add lines w1-gpio and w1_therm into /etc/modules using sudo nano /etc/modules so they get loaded automatically the next time you restart it.
  7. Find your sensor: ls /sys/bus/w1/devices/ it should look like 28-00000393268a
  8. Test the sensor by printing its output: cat /sys/bus/w1/devices/28-00000393268a/w1_slave.

Step 3: Installing DeviceHive on Raspberry Pi

  1. Download the latest version of DeviceHive.
  2. Connect to your RasPi using ssh. Create a folder for devicehive sources: mkdir ~/devicehive
  3. Using scp, copy the Python source code from your local machine to RasPi (for example: scp –r ~/Downloads/devicehive/python/device/Source/* pi@192.168.2.2:~/devicehive). This will copy all libraries and samples.
  4. Go to devicehive.com/playground and create an account for yourself for a playground if you don’t already have one.
  5. On your RasPi, edit ~/devicehive/examples/raspi_led_thermo.py and change _W1_FILENAME and _API_URL url (usually http://nnXXX.pg.devicehive.com/api if created using playground) to match the environment, using nano editor.
  6. Install Twisted: sudo apt-get install python-twisted. Our python library is built using the twisted engine. A very powerful tool for building protocol and connectivity libraries.
  7. Install DeviceHive device libraries: from ~/devicehive run sudo python setup.py install
  8. Run the script: “sudo python ~/devicehive/examples/raspi_led_thermo.py”

Step 4: Access Your Device From Admin UI

  1. After a first run of the script, you can see the device and the readings from the sensor in Admin UI (http://nnXXX.pg.devicehive.com/admin, login as admin / your_playground_password). Go to the Devices tab, and click “detail” for your device, then go to “notifications”.
  2. You can also send on/off messages to the LED. Go to “commands” for your device, click “enter new command”, set the name as UpdateState and the parameters as {"equipment":"LED","state":"1"}, then click “push”. The LED will turn on. Guess how to turn it off.

Step 5: Create a Client App

  1. Login to the DeviceHive administrative console. Go to the Users tab and create new user of the Client role. Click on the Networks button for this user and grant access to your device network.
  2. Now it’s time for the actual client app. Let’s use JavaScript, the client sample code to match your installation of playground. For example go to ~/examples/RasPiClient of the JavaScript repository.
  3. Open index.html file. Find the line where the new DeviceHive instance is created and insert your assigned DeviceHive service URL, login and password of the client user you've created. As a result, it should look like at the picture here.
  4. The GUID specifies a unique identifier of the device we will be interacting with, it does not have to be changed if you use sample devices from DeviceHive sources.
  5. Open index.html in your browser. The application should now work, displaying current LED state, and allowing you to switch the LED on/off and get temperature readings.

Step 6: Congratulations! You Are Done!

Your internet-enabled temperature sensor and LED are now talking to your JavaScript page using DeviceHive. You’ve just saved yourself a bunch of time designing the protocol, implementing libraries, troubleshooting and deploying. Well done! Now go and innovate.

Comments

author
benvk85 (author)2015-01-28

Hi, is the temperature sensor interchangeable with the DHT22 temp sensor? http://www.adafruit.com/product/385

Cheers

author
DmitryK2 (author)benvk852015-01-29

There is an example on how RasPi works with DH18B20 temperature sensor. It utilizes Dallas 1-wire protocol. DHT22 temp. sensor uses its own protocol. That is they are not interchangable. But you can easily find on the Internet "how-to"s on connecting DHT22 to RasPi.

author
sjtgs (author)2014-10-10

Nice and simple