Pages

Tuesday, 3 February 2015

Display real outside temperature on your android home screen (DS18B20)

Finally, my first raspberry GPIO project. What I am happy about, is that my >>hello world<< of electronics is not just a blinking diode (although even blinking diode can be quite powerful:)) but very useful and practical yet simple project which aim is to display real, outdoor temperature on android smartphone's home screen.

1) Ingridients:
- raspberry pi (with RAMP),
- 1-Wire temperature sensor: DS18B20 (waterproof version preferable):
- 4,7kΩ resistor:
- minimum 3-core cable (I used spare 4-core telephone cable):
- breadboard and jumpers (for prototyping only),
- android smartphone.

2) Hardware setup:
DS18B20 has 3 pins - see below for bare version schematics. For waterproof version: GND - black, DATA - white or yellow, Vdd - red.
Connect the temperature sensor with the resistor and raspberry pins as follows:
- wire GPIO7 with 3,3V using 4,7kΩ resistor,
- connect 3,3V raspberry pin to Vdd
- connect GPIO7 pin to DATA line
- connect GND to GND
As you can see you can make paraller connection using just one GPIO data pin and one resistor and get reliable results thanks to unique addressing of the sensors.
See below if you are unsure which pin is which (I use WiringPi naming convention). Also, find reference here.
Now do not forget to extend the reach of your sensor and put it outside:) Any meteorologist reading this will probably curse me, but I just installed mine under one of the windowsills on the first floor of the house.
3) System setup:

Edit the file /boot/config.txt and add the following line:
dtoverlay=w1-gpio,gpiopin=4

Also edit the file /etc/modules and add the following lines:
wire
w1-gpio
w1-therm
and reboot.

4) Reading the sensor:
To list all conneced devices do:

ls /sys/bus/w1/devices/

and you should see:

28-00203456e231  28-033475b26eea  w1_bus_master1

all the 28-xxxxxxxxxxxx directories represent each connected DS18B20 device.

If you cat the file /sys/bus/w1/devices/28-xxxxxxxxxxxx/w1_slave

you should see:

e2 ff 55 00 7f ff 0c 10 06 : crc=06 YES
e2 ff 55 00 7f ff 0c 10 06 t=-1875

what is interesting from our point of view is the value after "t=" at the end of second row. If you divide it by 1000 you will get the temperature in °C.

5) Webserver
Create subdirectory in your DocumentRoot of apache webserver, let's say "temperature", so it's accessible on http://raspberry_ip_address/temperature, where raspberry_ip_address can be either your internal or external IP address or domain/hostname.

Create PHP script which will read the w1_slave file (it's permission is by default 444/r--r--r--), parse it and display the temperature. You can also download my simple script (nothing fancy, a mixture of html, css and php, enough to get the job done) and edit the variable $sensorId with your DS18B20's unique address 28-xxxxxxxxxxxx:


You can extend the script, add authentication, what not. As you can see by investigating the source code of this script, it reads the temperature at runtime - that's fine. You can take different approach - read the temperature at some intervals, write it to a database/flat file/cloud and then read the latest value - whatever suits you.

6) Android configuration:
Go to Play Store and install Meta Widget. Now create the meta widget and configure it to your liking (you have to provide the URL of the temperature "website" created in step 5, click on the section of the site you wish to display and adjust some parameters like widget title, background, text size, etc). Mine looks like on the picture on the right, just underneath the "stock" weather widget:

Voilà!!!

Useful links:
http://picoboard.pl/raspberry-pi-obsluga-termomertrow-ds18b20
http://www.it-adviser.net/raspberry-pi-temperaturmessung-mit-ds18b20-1-wire-sensor
http://www.danielhansen.net/2013/03/raspberry-pi-temperature-logging-using.html
http://josy1024.blogspot.com/2014/02/raspbmc-und-temperaturmessung-mit.html
http://projects.privateeyepi.com/home/temperature-sensor-project-using-ds18b20