Python and Raspberry Pi Temperature Sensor

DeviceHive is powerful and yet easy. To make it even easier to understand, let’s take this simple example of the fun project that you can build in just a few hours. How about building a remote temperature sensor and a remotely controlled LED? This can be a very good starting point to built from. 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 (1) python (2) 1-wire kernel drivers

In order to do this, you will need:

  • Rasbperry Pi Rev. B ( 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, the sensor will be powered from RasPi’s Pin 2. No external power needed:


The following steps will turn all of the above into a remotely controlled Internet connected device.

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
  9. .

Installing DeviceHive on Raspberry Pi

  1. Download the latest version of DeviceHive. Go to and follow the instructions.
  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/* :~/devicehive). This will copy all libraries and samples.
  4. Go to and create an account for yourself for a playground if you don’t already have one.
  5. On your RasPi, edit ~/devicehive/examples/ and change _W1_FILENAME and _API_URL url (usually 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 install
  8. Run the script: “sudo python ~/devicehive/examples/”

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 (, 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. ☺

Create 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 created in Step 1. As a result, it should look like the following:

    var deviceHive = new DeviceHive("", "myclientuser", "mypassword");   
    app.start(deviceHive, "9f33566e-1f8f-11e2-8979-c42c030dd6a5");
  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.

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

< Back to samples