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

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

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.

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.

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.

Run the script: “sudo python ~/devicehive/examples/raspi_led_thermo.py”

Access your device from Admin UI

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

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

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.

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.

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:

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.

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.