Finally got around to adding the temperature sensor to Abercam. The temperature sensors I got (DS18B20 ones I believe) use the Dallas 1-Wire protocol to communicate with the Pi, meaning you can attach many temperature sensors in parallel, and the Pi can differentiate between them all and interrogate them individually, without having to use tons of GPIO pins, one for each sensor.
The PHP script simply opens the w1_slave files, finds and nicely formats the temperature. The HTML uses JQuery to request the .php file every 5 seconds and updates the HTML accordingly.
Hang about, I haven’t got PHP on my Pi
No problem, if you aren’t using a LAMP server – e.g I’m using lighttpd for my webserver, install php with the command:
sudo apt-get install php5-common php5-cgi php5
sudo apt-get install php5-mysql
After, enable the fastcgi-php module and reload the server:
sudo lighty-enable-mod fastcgi-php
sudo service lighttpd force-reload
That all works lovely on a local network, but when using my website to try and access the Pi to get the temperature, I ran into the problem of being stopped from accessing another server and displaying its information on my webpage due to the threat of cross-site scripting attacks. Ways to get round this are make an API or possibly use cURL. I also considered using FTP to upload a file every minute from the Pi containing the current temperature, which would be included in the webpage. Currently, I am using the below script, with a 20 second timeout, and the line “header(‘Access-Control-Allow-Origin: *’);” in the php file it is accessing.
This works really well now, with the page requesting an updated temperature every 20 seconds. Might have to revert to the FTP solution if getting the temp from the Pi is too slow or causes it too much extra traffic.
//File to read
$file = ‘/sys/bus/w1/devices/28-000004abb89f/w1_slave’;
//Read the file line by line
$lines = file($file);
//Get the temp from second line
$temp = explode (‘=’, $lines);
//Setup some nice formatting
$temp = number_format($temp / 1000, 1, ‘.’, ”);