After
my Raspberry Pi powered door alarm I thought about implementing a temperature monitoring system with the Raspberry. I just found out about 1wire support on the Raspberry Pi a few weeks ago and wanted to try it myself.
After you've finished this tutorial you'll have a cheap temperature monitoring Raspberry pi with a nice zoomable,
live updating web UI with as many sensors as you wish and even from remote networks like this (german):
You'll need these hardware components (if you make it like I did):
1. A Raspberry Pi (duh)
I use a standard raspbian and with just 3 commands you're ready for reading 1wire sensors (more on that later)
2. Temperature sensor
I bought a 1 wire temperature sensor for about 5€. Although 1 wire is not really true. It has 3 wires but the data uses just one wire.
I bought
this one (sheet in german) which has a DS18S20 sensor inside.
3. One 4.7k Ohm resistor
We'll be needing this to keep our raspberry GPIO ports from frying
4. One screw terminal with 3 pins
I'm using this because it's the easiest way to re-use cables and sensors because they don't have to be soldered.
Let's build it!
This will actually be the shortest part. Since I'm using the screw terminal all we'll need to do is solder (or connect somehow) the resistor between the left and center terminal like this:
The second and last part of the building process is to connect the raspberry and the sensor to the terminal.
(GPIO Pin numbers acording to
this graph, "=>" means connect to)
GPIO 1 => +3.3V on the Terminal
GPIO 6 => GND on the Terminal
GPIO 7 => DATA on the Terminal
+ Wire of the sensor => +3.3V on the Terminal
- Wire of the sensor => GND on the Terminal
DATA wire of the sensor => DATA on the Terminal
The hardware is now ready!
Now let's log into our Raspberry Pi!
First we'll have to tell the kernel that we'll be using the 1 wire communication. We do this by editing (as root) /etc/modules and adding the following lines:
wire
w1_gpio
w1_therm
After a restart we check if the sensor was found
ls /sys/bus/w1/devices
You should see something like this:
If you only see the w1_bus_master1 folder that means your sensor was not recognised by the system. Check your wires and if you connected the + and - cables correctly.
If you see another folder (in my case it's 10-00080224e359) you're ready to get data from your sensor!
Every sensor has a different ID so yours will be different from mine! Keep that in mind when you copy the commands!
Next lets see if it's really working by reading the w1_slave file inside your sensors directory:
cat /sys/bus/w1/devices/10-00080224e359/w1_slave
The temperature is right in this file at t=28750 which means 28.750°C (damn you, heatwave)
Congratulations! You can now log your temperature inside or outside (or even in water if your sensor is water proof)
But I didn't stop there
I wanted to create a website which displays all the info and logs the sensor as well as the CPU temperature.
But I didn't want to rely on the raspberry as a webserver rather than just a node that sends it's info to another server like this:
This way I could still see historic data without killing the Raspberry's SD card slowly.
This is the script (it's written in PHP ran in php5_cli.. don't judge me I'm a web developer :D) that runs on the Raspberry, checks the CPU and sensor temperature every 60 seconds and then sends it to a server script (more on that later)
Script: read_sensors.php
You might want to change two things: The device ID of your temperature sensor and the IP and folder of your Webserver
<?php
$webserver_ip_and_port = 'http://192.168.1.115/temperatur/';
$device_id = '10-00080224e359';
$cputemp = "/sys/class/thermal/thermal_zone0/temp";
$wiretemp = "/sys/bus/w1/devices/'.$device_id.'/w1_slave";
$data = array();
while(1)
{
$data = file($wiretemp);
$data = explode('t=',$data[1]);
$wtemp = $data[1]/1000;
$ctemp = implode(file($cputemp))/1000;
$tdata = array('CPU'=>$ctemp,'room'=>$wtemp);
echo "CPU: $ctemp\nRoommtemp: $wtemp\n";
$null = file($webserver_ip_and_port."get.php?data=".rawurlencode(json_encode($tdata)));
sleep(60);
}
This script should be executed with the command "php read_sensors.php". You'll need to have the package php5_cli installed.
Good so our raspberry can read the temperature and sends it to another server.
The server setup is pretty straight forward since you only have to push the webserver folder from
this repository I made on your webspace and you're finished! The code I wrote features a backend API where the script looks every minute for changes and if your monitoring computer is disconnected for a few minutes and then reconnects it will update all the data that you've missed because of the disconnect.
Please let me know what you think and if you have any questions I'll be happy to answer them :D