Raspberry Pi and 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:
900

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. Enable 1-wire kernel modules that come pre-installed but not enabled: echo "dtoverlay=w1-gpio" | sudo tee -a /boot/config.txt and reboot sudo reboot. If you have OS which was released before January of 2015 (kernel older then 3.18) then run sudo modprobe w1-gpio && sudo modprobe w1_therm.
  6. Find your sensor: ls /sys/bus/w1/devices/ it should look like 28-00000393268a
  7. Test the sensor by printing its output: cat /sys/bus/w1/devices/28-00000393268a/w1_slave

Installing DeviceHive on Raspberry Pi

  1. Download the latest version of DeviceHive cloud server and deploy it. Go to http://docs.devicehive.com/ and follow the instructions.
  2. Connect to your RasPi using ssh.
  3. Copy devicehive-python sources git clone https://github.com/devicehive/devicehive-python.git or wget https://github.com/devicehive/devicehive-python/archive/stable.zip && unzip stable.zip && mv devicehive-python-stable/ devicehive-python/. This will copy all libraries and samples.
  4. 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.
  5. Change directory cd devicehive-python and install python library on RasPi: sudo apt-get install python-dev && sudo python setup.py install
  6. On your RasPi, edit ~/devicehive-python/examples/raspi_led_thermo.py and change _API_URL url (usually http://playground.devicehive.com/api/rest if created using playground), _ACCESS_KEY, _W1_FILENAME (by default it tries to auto detect, change it if you need it) and _DEVICE_ID to match the environment, using nano editor. _DEVICE_ID should be unique for each server instance.
  7. Run the script: sudo python ~/devicehive-python/examples/raspi_led_thermo.py

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. 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 UpdateLedState 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. 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 ~/devicehive-javascript/examples/browser/RasPiClient of the JavaScript repository.
  2. Install Bower (for ubuntu sudo apt-get install npm nodejs-legacy and sudo npm install -g bower) and bower install in RasPiClient directory.
  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 = $.dhClient("http://playground.devicehive.com/api/rest", "SampleAccessKeyChangeToYours");
app.start(deviceHive, "Your-DeviceId-Here");

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