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

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.

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.The latest Raspbian images have SSH disabled by default. To enable SSH without connecting monitor and keyboard insert microSD card into your reader and create empty file named ssh in the root of boot partition.

Connect LED/Sensor as shown on Figure 1 above.

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.

Find your sensor: ls /sys/bus/w1/devices/ it should look like 28-00000393268a

Test the sensor by printing its output: cat /sys/bus/w1/devices/28-00000393268a/w1_slaveSample output:

Open downloaded file 'raspi_led_thermo.py' with nano or vi editor and change 'SERVER_REFRESH_TOKEN' variable with your correct refresh token(you can get it in Admin UI).

Run the script: python raspi_led_thermo.py

Access your device

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

You can also send on/off messages to the LED. Go to “commands” for your device, click “enter new command”, set the name as led/on to turn on LED, then click “push”. The LED will turn on. Guess how to turn it off. ☺

With the same DeviceHive Python library client application which would switch LED or monitor temperature can be implemented. DeviceHive also has a bunch of libraries for other programming languages, you will find all of them here. Even if there is no suitable library you can always use simple RESTful calls described here.

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