Abercam Upgrade

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.

I dug out a 4.7k ohm resistor that I used for this Adafruit tutorial regarding temp sensing on the Pi, and used PHP from this YouTube video to get the temperature.

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


This guide I have refered to suggests installing the PHP MySQL libraries if you are using MySQL. Another good, similar, guide is here.

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

Abercam with temp

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.



The Scripts


//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[1]);

//Setup some nice formatting
$temp = number_format($temp[1] / 1000, 1, ‘.’, ”);

echo $temp;



<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js” type=”text/javascript”></script>

<script type=”text/javascript”>

function refreshTemp(){
$(‘#tempHolder’).load(‘temp.php’, function(){
setTimeout(refreshTemp, 5000);
<div id=”tempHolder”></div>