Showing posts with label temperature. Show all posts
Showing posts with label temperature. Show all posts

Monday, 15 February 2016

NodeMCU + DHT22 (ESP8266) wifi thermometer/humidity sensors pt2 with OLED

Wifi temperature / humidity sensor for under £20 with an OLED display

So I wanted a display output for the wifi temp modules, so I bought a few of these: http://www.ebay.co.uk/itm/111474769297?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
which are 128x64 displays with an i2c interface.It uses a multi-master protocol and has 2 x i2c lines which are SDA (serial data) and SCL (serial clock).

The reason for these, is that they use less power than a traditional LCD, have persistence when powering them, and are brighter than LCDs.


Code wise, I separated it into main code and a init loader (init being the first code loaded when the esp boots).
This basically 
Loader:
gpio.mode(1,gpio.OUTPUT)
gpio.write(1,gpio.HIGH)
wifi.setmode(wifi.STATION)
wifi.sta.config("wifi name","wifi passwd")
print (wifi.sta.getip())
gpio.write(1,gpio.LOW)
dofile("main.lua")


Main.lua
print("Running main")
gpio.mode(2,gpio.OUTPUT)
BLUE=1
gpio.mode(BLUE,gpio.OUTPUT)
gpio.write(BLUE,gpio.HIGH)
-- Set the LED to flash
pwm.stop(2)
pwm.setup(2,1,10)
pwm.start(2)
-- Load library

sda_pin = 5
scl_pin = 6
dht_pin = 4
oled_addr = 0x3c
-- Counter for heartbeat
cnt = 1
state = 0
-- global for display/read dht
humidtwo = 0
temptwo = 0
-- Heap limit
heap_limit= 22000

function init_OLED(sda,scl)
     sla = 0x3c
     i2c.setup(0, sda, scl, i2c.SLOW)
     disp = u8g.ssd1306_128x64_i2c(sla)
     disp:setFont(u8g.font_6x12)
     disp:setFontRefHeightExtendedText()
     disp:setDefaultForegroundColor()
     disp:setFontPosTop()
end
function read_sensor_values()
  local varh,vart
  dht22.read(dht_pin)
  varh = dht22.getHumidity()
  vart = dht22.getTemperature()
  if varh ~= nil then
    humidtwo = (varh/10).."."..(varh%10)
  else
    print ("Previous H : " ..humidtwo)
  end
  if vart ~= nil then
    temptwo = (vart/10).."."..(vart%10)
  else
    print ("Previous T : " ..temptwo)
  end
end

function display_sensor_values(vvar)
  disp:firstPage()
  disp:setFont(u8g.font_6x10)
  disp:setFontRefHeightExtendedText()
  disp:setDefaultForegroundColor()
  disp:setFontPosTop()
  local x,y,ip,nm,st,deg,result
  if state==1 then
    st=string.char(176)
  else
    st=" "
  end
  deg=string.char(176)
  repeat
    disp:drawRFrame(0, 0, 128-1, 64-1, 1)
    x=4
    y=8
    disp:drawStr(x, y, 'T:' .. (temptwo) .. deg ..'C   H:' .. (humidtwo) .. '%RH' )
    y = y+14
    disp:drawStr(x, y,  st .. ' H:' .. (node.heap()) .. ' C:' .. (cnt) )
    y = y+14
    if wifi.sta.status()==0 then result='STA_IDLE' end
    if wifi.sta.status()==1 then node.restart() end
    if wifi.sta.status()==2 then result='STA_WRONG PASSWD' end
    if wifi.sta.status()==3 then result='STA_NO AP FOUND' end
    if wifi.sta.status()==4 then result='STA_CONNECT FAILED' end
    if wifi.sta.status()==5 then result='STA_GOT_IP' end
    if vvar==1 then
      ssid,password,bssid_set,bssid=wifi.sta.getconfig()
      result= ssid
      ssid,password,bssid_set,bssid=nil,nil,nil,nil
    end
    disp:drawStr(x, y, result  )
    y = y+14
    ip,nm=wifi.sta.getip()
    if ip ~= nil then
        disp:drawStr(x, y, 'IP:' .. (ip)  )
    else
        disp:drawStr(x, y, 'IP: Cannot get IP')
        node.restart()
    end

  until disp:nextPage() == false

  cnt = cnt + 1
  if cnt > 999 then
     cnt=1
  end
end

init_OLED(5,6)

i2c.setup(0, sda_pin, scl_pin, i2c.SLOW)
disp = u8g.ssd1306_128x64_i2c(oled_addr)

  sv=net.createServer(net.TCP, 2)
  sv:listen(80,function(c)
      c:on("receive", function(c, pl)
         print(pl)
         if pl=="1" then
            print ("gpio1 low")
            gpio.write(1,gpio.LOW)
         end
         if pl=="2" then
            print ("gpio1 high")
            gpio.write(1,gpio.HIGH)
         end
      end)
        dht22 = require("dht22")
        gpio.mode(BLUE,gpio.OUTPUT)
        gpio.write(BLUE,gpio.HIGH)
        read_sensor_values()
        display_sensor_values()
        dht22=nil
        print("Humidity:    "..humidtwo.." %")
        print("Temperature: "..temptwo.." deg C")
        c:send("H:"..humidtwo.." ; T:"..temptwo.."\r\n")
        c:close()
        gpio.write(BLUE,gpio.LOW)
      
        hp=node.heap()
        if hp<5000 then
            node.restart()
        end
  
        collectgarbage()
       end)
     
tmr.alarm( 2, 1000, 1, function()
  if state == 0 then
    display_sensor_values(1)
  end
  if state == 1 then
    display_sensor_values(0)
  end
  state = (state + 1) %2
  collectgarbage()
end)
The code above requires dht22.lc to be loaded ( from here ), which should be compiled on the ESP2866, once uploaded as a LUA.

Doing a list should show something like this:
dht22.lc        : 1308 bytes
init.lua        : 199 bytes
main.lc         : 4176 bytes
main.lua        : 3521 bytes
The OLED display should be like so:
T: 25.3oC   H: 26.9%RH
* H:22824  C:288
STA_GOT_IP
IP: 10.10.0.164

So with this it will display the (T:) temperature in the top left, with the humidity level (H:) in the top right.
The next line shows a heartbeat icon, followed by heap size in bytes (H:).
I've programmed this so that if the heap space get below 5000 bytes, it will restart itself.
C: is the count, and when it reaches 999, it will go back to 1. This is purely to show it's doing some processing.
The next line alternates between the wifi status codes and the actual SSID it's connected to. This alternates every second.
The last line (IP:) shows the ip currently assigned to the device.

My output to RRD graph is something like this:



The drop outs are caused by the wifi signal (or lack of).
Part 1 here

Wednesday, 4 November 2015

NodeMCU + DHT22 (ESP8266) wifi thermometer/humidity sensors pt1

Wifi temperature / humidity sensor for under £20

Overview

So I travel a lot, and want to know what's happening in the house whilst I am abroad.
I've researched and there are many solutions out there (like here or here), but mostly they are way, way over priced at over £150 EACH per unit.

I need at least 5 units, for the bathroom, kitchen, boiler room etc. so even doing it with a raspberry pi would have been pricey at around £40-50 per 'sensor' (using the DHT22 for readings), so an outlay of about £250.

What I have found is something even better. The ESP8266. This is a self contained wifi module with built in TCP/IP stack for around £3 and under the size of a 50pence piece. Whoa - cheap eh? Ok, so the issue here is that it can't do a 5v-3v logic shift nor can I be bothered to figure out the electronics to step it down and connect it to USB TTL etc.

So.... I researched a bit more and there is a board that combines the ESP8266 and the step down and the USB TTL and allows me to either publish data via a message queue or host a webserver! This is the NodeMCU

Fantastic little thing that does everything you need it to + can be powered from a micro USB connection.

Parts list

ok so the parts list (you may not need to purchase everything, as you may have some parts already)
1 x NodeMCU (Amazon or ebay) - Beware of the version you get tho! Check the PCB colour and size.
1 x DHT22 temperature/humidity sensor (Amazon or ebay)
A breadboard /protoboard to prototype stuff (pimoroni) - you'll solder it onto this.
A solderless breadboard to test things work (pimoroni)
Soldering iron
Micro USB cable for connecting to a PC
some cheap micro usb charger
Wires for testing (Jumper jerky makes it easier)
Wires for permanent installation (pimoroni)

Total cost of the above is about £30 for the first unit. Cost of the 2nd unit will be about £13, 3rd unit about £13 too, so for 5 units, it's about £16 each. Not bad

Circuit diagram

 

Dev software

ESP software - for loading up LUAs and code

Firmware

Download the firmware

Flashing firmware

config tab
select first slot
flash on tab one
ensure the MAC addresses show. If not, then something went wron.
A green tick at the end shows success.

Modules

DHT22 from here

Upload it with esplorer from above using the 'upload' button. Then compile by clicking reload on the middle right side, then a list of modules will appear. Right click and select compile.

Code

Set the wifi access point and set NIC to DHCP

print(wifi.sta.getip())
--nil
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","password")
print(wifi.sta.getip())
--192.168.18.110
 
View wifi SSIDs
function listap(t)
  for k,v in pairs(t) do 
    print(k.." : "..v) 
  end 
end 
wifi.sta.getap(listap)

onboard LED

Flash the blue LED near the USB connector
gpio.write(0,gpio.LOW)
gpio.write(0,gpio.HIGH)

GPIO LEDs

Flash an LED on GPIO pin 2
You can change the duty cycles by checking the docs http://www.nodemcu.com/docs/
 pwm.setup(2,1,10)
pwm.start(2)

Reading temperature/humidity

PIN=4
dht22 = require("dht22")
dht22.read(PIN)
t = dht22.getTemperature()
h = dht22.getHumidity()
humi=(h/10).."."..(h%10)
temp=(t/10).."."..(t%10)
print("Humidity:    "..humi.." %")
print("Temperature: "..temp.." deg C")
dht22 = nil
package.loaded["dht22"]=nil

 
All together:
print(wifi.sta.getip())
gpio.mode(2,gpio.OUTPUT)
--gpio.write(2,gpio.HIGH)
--gpio.write(2,gpio.LOW)
--gpio.write(1,gpio.LOW)
gpio.write(0,gpio.LOW)
BLUE=1 -- blue LED is on GPIO 1
pwm.stop(2)
pwm.stop(1)

pwm.setup(2,1,10)
pwm.start(2)

gpio.write(0,gpio.HIGH)


  sv=net.createServer(net.TCP, 2) 
  sv:listen(80,function(c)
      c:on("receive", function(c, pl)
         print(pl) 
      end)
        gpio.mode(BLUE,gpio.OUTPUT)
        gpio.write(BLUE,gpio.HIGH)
        PIN=4
        dht22 = require("dht22")
        dht22.read(PIN)
        t = dht22.getTemperature()
        h = dht22.getHumidity()
        humi=(h/10).."."..(h%10)
        temp=(t/10).."."..(t%10)
        print("Humidity:    "..humi.." %")
        print("Temperature: "..temp.." deg C")
        dht22 = nil
        package.loaded["dht22"]=nil
        c:send("H:"..humi.." ; T:"..temp.."\r\n")
        c:close()
        gpio.write(BLUE,gpio.LOW)
       end)
Note the above server doesn't respond to normal HTTP requests.  it responds with a simple:
H:xx ; T: xx
and disconnects after the initial connect.
You can use either curl or netcat to get the data.
It also flashes the blue LED whilst a network operation is in effect.
The white LED I've used blinks every second, to show it is working.

Dashboard

RRDtool - Create the RRD

This creates a RRD for 5 sensors, values 0 to 100 and 524160 data points (60*24*7*52) so 1 data point a minute
<?
    echo "Creates blank temp graphs";
    $options = array (
     "--step", "60",            // Use a step-size of 1 minute
     "--start", "-1 day",         "DS:hum1:GAUGE:100:0:100",
     "DS:hum2:GAUGE:100:0:100",
     "DS:hum3:GAUGE:100:0:100",
     "DS:hum4:GAUGE:100:0:100",
     "DS:hum5:GAUGE:100:0:100",
     "RRA:AVERAGE:0.3:1:524160",
    );


    $ret = rrd_create("./hum.rrd", $options);
    if (! $ret) {
     echo "<b>Creation error: </b>".rrd_error()."\n";
    }
?>

In part two, I'll show how to render the graphs

Part 2 here



Saturday, 18 October 2014

Creating RRD graphs for Raspbery Pi / DS18B20


So I wanted to move the TME sensor I bought in my previous post to the bitcoin mining room, to measure the temperature there.

I still needed to monitor the temp in the comms cupboard where I have 4 HP Microserver NASs, UPS, firewall, HP managed switch etc etc to make sure it isn't going to melt.

I saw this excellent article here that uses simple off the shelf electronics to accomplish this. Following this and buying the parts listed from Maplins and Pimori and ebay.

Maplins parts list:
Resistor pack (you never know when you might need more)

Pimori parts list:
Hook up wire (looked cool with all the colours)
ADA fruit pi - t cobbler
Adafruit raspberry pi perma proto
Raspberry Pi GPIO cable

Ebay parts list:
ds18b20t (waterproof version)
Total cost of parts incl Raspberry pi b model < £90

So I did some basic config following the article there and basically adding the following modules to /etc/modules
w1-gpio
w1_therm
then in /sys/bus/w1/devices/ should have the sensors with their individual serials there

phil@temp-comms /sys/bus/w1/devices $ ls -al
total 0
drwxr-xr-x 2 root root 0 Oct 18 00:13 .
drwxr-xr-x 4 root root 0 Oct 18 00:13 ..
lrwxrwxrwx 1 root root 0 Oct 18 00:13 28-0000062d26d0 -> ../../../devices/w1_bus_master1/28-0000062d26d0
lrwxrwxrwx 1 root root 0 Oct 18 00:13 28-0000062e10ae -> ../../../devices/w1_bus_master1/28-0000062e10ae
lrwxrwxrwx 1 root root 0 Oct 18 00:13 28-0000062e511a -> ../../../devices/w1_bus_master1/28-0000062e511a
lrwxrwxrwx 1 root root 0 Oct 18 00:13 w1_bus_master1 -> ../../../devices/w1_bus_master1
phil@temp-comms /sys/bus/w1/devices $ 



For each sensor there will be a 28-xxx directory, and under each directory 280000062xxxx there is is a file called w1_slave. Cat that and it'll have the temperature
phil@temp-comms /sys/bus/w1/devices/28-0000062d26d0 $ cat w1_slave
c7 01 4b 46 7f ff 09 10 01 : crc=01 YES
c7 01 4b 46 7f ff 09 10 01 t=28437
phil@temp-comms /sys/bus/w1/devices/28-0000062d26d0 $
t=temperature, but it's 1000 higher than it should, so just divide it by 1000 to get the temp in degrees celcius.



and then followed this article to use multiple temp sensors with the wiring like so




So what this article doesn't tell you is how to produce the graphs.
So obviously setting up cron with 1 minute intervals like so in /etc/cron.d/logs
* * * * * root /usr/local/bin/temp.sh 

with temp.sh containing
#!/bin/sh
file=/var/www/temp.txt
echo -n spare \ > $file
cat /sys/bus/w1/devices/28-0000062e511a/w1_slave | grep t | awk '{print $10}' | cut -d = -f 2 >> $file

echo -n comms_top \ >> $file
cat /sys/bus/w1/devices/28-0000062d26d0/w1_slave | grep t | awk '{print $10}' | cut -d = -f 2 >> $file

echo -n comms_bottom \ >> $file
cat /sys/bus/w1/devices/28-0000062e10ae/w1_slave | grep t | awk '{print $10}' | cut -d = -f 2 >> $file
Note the serials for the temp sensors were ascertained by individually plugging them in, making a note and appying labels physically to the device so I knew what I was monitoring.

So after the the data was grabbed and stored in a text file, you have to process it. I decided on PHP, cos it's easy and can be used easily on apache, which I can use to publish the data for view via a browser.
So after installing apache2, libapache2-mod-php5, php5-rrd,  rrdtool etc you need to input the data into an RRD

Using PHP and 
#!/usr/bin/php

 // Fetch current time
    $t = time();
    $filename1="/mnt/ramdisk/temp-spare.txt";
    $filename2="/mnt/ramdisk/temp-bottom.txt";
    $filename3="/mnt/ramdisk/temp-top.txt";

    $handle = fopen($filename1, "r");
    $contents1 = fread($handle, filesize($filename1));
    fclose ($handle);
    $contents1=(float)$contents1/1000;

    $handle = fopen($filename2, "r");
    $contents2 = fread($handle, filesize($filename2));
    fclose ($handle);
    $contents2=(float)$contents2/1000;

    $handle = fopen($filename3, "r");
    $contents3 = fread($handle, filesize($filename3));
    fclose ($handle);
    $contents3=(float)$contents3/1000;

   $option = array ("$t:$contents1:$contents2:$contents3");
        echo "$t ,$contents1, $contents2, $contents3 ";
    $ret = rrd_update("/mnt/ramdisk/temp-comms.rrd", $option);

    if (! $ret) {
              echo "<b>Graph error comms: </b>".rrd_error()."\n";
    }

then you've gotta create the GIFs
index.php
// for comms cupboard
function create_graph_temperature_comms ($output, $start, $title)
{
    $options = array(

    "--slope-mode",
    "--start", $start,
    "--title=$title",
    "--vertical-label=Temperature in oC",
    // oops got the top and bottom values mixed up on input
    "DEF:temp_spare=/mnt/ramdisk/temp-comms.rrd:spare:AVERAGE",
    "DEF:temp_top=/mnt/ramdisk/temp-comms.rrd:temp_bottom:AVERAGE",
    "DEF:temp_bottom=/mnt/ramdisk/temp-comms.rrd:temp_top:AVERAGE",

    "DEF:temp_sparemin=/mnt/ramdisk/temp-comms.rrd:spare:MIN",
    "DEF:temp_sparemax=/mnt/ramdisk/temp-comms.rrd:spare:MAX",
    "DEF:temp_topmin=/mnt/ramdisk/temp-comms.rrd:temp_top:MIN",
    "DEF:temp_topmax=/mnt/ramdisk/temp-comms.rrd:temp_top:MAX",
    "DEF:temp_bottommin=/mnt/ramdisk/temp-comms.rrd:temp_bottom:MIN",
    "DEF:temp_bottommax=/mnt/ramdisk/temp-comms.rrd:temp_bottom:MAX",
   
    "CDEF:ttemp_spare=temp_spare,1,*",
    "CDEF:ttemp_top=temp_top,1,*",
    "CDEF:ttemp_bottom=temp_bottom,1,*",
   
    "VDEF:vtemp_spare=temp_spare,AVERAGE",

    "LINE:ttemp_spare#00aa00:Spare:",
    "GPRINT:temp_spare:AVERAGE:Avg temp \\t %6.2lf",
    "GPRINT:temp_spare:LAST:Last temp \\t %6.2lf \\n",

    "LINE:ttemp_top#cc0000:Top:",
    "GPRINT:temp_top:AVERAGE:Avg temp \\t %6.2lf",
    "GPRINT:temp_top:LAST:Last temp \\t %6.2lf \\n",

    "LINE:ttemp_bottom#000088:Bottom:",

    "COMMENT:\\n",
    "--alt-autoscale",
    "--alt-y-grid",
   

   
  
    "GPRINT:vtemp_spare: test %6.2lf",
    );

    $ret = rrd_graph($output, $options);
        if (! $ret) {
        echo "<b>Graph error for temp comms</b>".rrd_error()."\n";
        }
}
And you'll get something like this:




Sunday, 24 August 2014

Creating RRD graphs for TME Ethernet thermometer TME_C_EU pt2

 Ok, so last post I created the graphs, but they don't look that good. They don't seem to scale right. They scale from 0 to about 30oC


So it's showing values 0-30oC, which doesn't show the zoomed in detail, as the fluctuations are only 27oC +- 3oC. This is to do with auto-scaling and that stacked area graphs start from 0.
The detail we need is actually in the 25-30 range.

So I've had to change the index.php script so that it auto scales. It doesn't look as fancy, but at least you can see the temperatures better.
    "--slope-mode",
    "--start", $start,
    "--title=$title",
    "--vertical-label=Temperature in oC",
    "DEF:temp=/mnt/ramdisk/temp.rrd:temp:AVERAGE",
    "DEF:tempmin=/mnt/ramdisk/temp.rrd:temp:MIN",
    "CDEF:ttemp=temp,1,*",
    "LINE:ttemp#E54400::",
    "COMMENT:\\n",
    "--alt-autoscale",
    "--alt-y-grid",
    #"--rigid",
    "GPRINT:temp:MAX:Max temp %6.2lf",
    "GPRINT:temp:MIN:Min temp %6.2lf",
    "GPRINT:temp:AVERAGE:Avg temp %6.2lf",

To make this prettier, you could also define the max/min values for the entire graph
$options = array(
    "--slope-mode",
    "--start", $start,
    "--title=$title",
    "--vertical-label=Temperature in oC",
    "DEF:temp=/mnt/ramdisk/temp.rrd:temp:AVERAGE",
    "DEF:tempmin=/mnt/ramdisk/temp.rrd:temp:MIN",
    "DEF:tempmax=/mnt/ramdisk/temp.rrd:temp:MAX",
    "CDEF:ttemp=temp,1,*",
    "VDEF:ttempmin=tempmin,MINIMUM",
    "VDEF:ttempmax=tempmax,MAXIMUM",
    "HRULE:ttempmin#4444FF::dashes=2,2",
    "HRULE:ttempmax#FF5500::dashes=2,2",
    "LINE:ttemp#000000::",
    "COMMENT:\\n",
    "--alt-autoscale",
    "--alt-y-grid",
    #"--rigid",
    "GPRINT:temp:MAX:Max temp %6.2lf",
    "GPRINT:temp:MIN:Min temp %6.2lf",
    "GPRINT:temp:AVERAGE:Avg temp %6.2lf",
        );
This will draw a horizontal ruler at the min and max values with blue and red dashed lines, respectively.

Saturday, 23 August 2014

Creating RRD graphs for TME Ethernet thermometer TME_C_EU pt1

So I decided to buy one of these network temperature sensors from Papouch here to monitor my 19" rack at home. I wanted to graph the temperature variations from it, as it can get quite hot when all the other servers are on.

Setup Apache with mod_php5 + rrd+ php5_cli

There are many articles on setting up apache + mod_php5 + rrdtools + php5_cli, so i won't bother you with the details here.

Create the RRD

One you've installed rrdtool and php5 cli, create the below file and chmod a+x so that it is executable.
Once run, this will create your RRD to input data called temp.rrd
#!/usr/bin/php
<?
echo "Creates blank temp graphs";
$options = array (
 "--step", "60",            // Use a step-size of 1 minute
 "--start", "-1 day",     // this rrd started 6 months ago
 "DS:temp:GAUGE:100:U:U",
"RRA:MIN:0.5:60:8800",
"RRA:MAX:0.5:60:8800",
"RRA:AVERAGE:0.5:2:328800",
);


$ret = rrd_create("./temp.rrd", $options);
if (! $ret) {
 echo "<b>Creation error: </b>".rrd_error()."\n";
}

?>

Create the graphs

This should be your index.php This should be in your /var/www directory
<?

create_graph_temperature("/var/www/temp-hr.gif","-1h","Hourly temperature");
create_graph_temperature("/var/www/temp-2hr.gif","-2h","Bi-Hourly temperature");
create_graph_temperature("/var/www/temp-daily.gif","-23h","Daily temperature");
create_graph_temperature("/var/www/temp-weekly.gif","-1w","Weekly temperature");
create_graph_temperature("/var/www/temp-monthly.gif","-1m","Monthly temperature");
create_graph_temperature("/var/www/temp-yearly.gif","-1y","Yearly temperature");

function create_graph_temperature ($output, $start, $title)
{
    $options = array(
    "--slope-mode",
    "--start", $start,
    "--title=$title",
    "--vertical-label=Temperature in oC",
    "DEF:temp=/mnt/ramdisk/temp.rrd:temp:AVERAGE",
    "CDEF:ttemp=temp,1,*",
    "CDEF:ttempq=temp,6,/",
    "AREA:ttempq#F31616::STACK",
    "AREA:ttempq#F03D11::STACK",
    "AREA:ttempq#ED650D::STACK",
    "AREA:ttempq#EA8D08::STACK",
    "AREA:ttempq#E7B504::STACK",
    "AREA:ttempq#E5DD00::STACK",
    "COMMENT:\\n",
    "GPRINT:temp:MAX:Max temp %6.2lf",
    "GPRINT:temp:MIN:Min temp %6.2lf",
    "GPRINT:temp:AVERAGE:Avg temp %6.2lf",
        );
    $ret = rrd_graph($output, $options);
        if (! $ret) {
        echo "<b>Graph error for temp</b>".rrd_error()."\n";
        }
}


?>

<img src=temp-hr.gif><img src=temp-2hr.gif></br>
<img src=temp-daily.gif><img src=temp-weekly.gif></br>
<img src=temp-monthly.gif><img src=temp-yearly.gif></br>
Note:you may need to chgrp www-data and chmod g+w on the /var/www directory, so that apache can write the GIFs to there. 

Update data into RRD

Do to this, I wrote a small PHP script
#!/usr/bin/php
<?
 // Fetch current time
    $t = time();
    $filename="/mnt/ramdisk/temp.txt";

    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose ($handle);
    $contents=(float)$contents/10;
    $option = array ("$t:$contents");
    echo "$t ,$contents";
    $ret = rrd_update("/mnt/ramdisk/temp.rrd", $option);

    if (! $ret) {
    mail ("blah@gmail.com","temp insert rrd error","trying to insert temp from temp.txt into temp.rrd");
        #echo "<b>Graph error: </b>".rrd_error()."\n";
    }


?>

Crontab

I've set up my TME_C on IP address 192.168.2.120
Note I've saved the output to a ramdisk (as my machine boots from a USB key)
 crontab -e on root
* * * * * /usr/local/bin/update_temp_script.sh
Note you should be calling a script which does the update, then inserts it into the RRD in sequence. If you don't it maybe updating the RRD with blank values.
update_temp_script.sh
/usr/bin/curl -s http://192.168.2.120/fresh.xml | grep sns  | awk '{print $10}' | cut -d "\"" -f 2 > /mnt/ramdisk/temp.txt 
/usr/local/bin/update_temp.php
That's it - it's that simple.
You should have graphs looking similar to this:


Now you can also setup a daily cron job to tar.gz these files to the local file system instead of leaving it on a ramdisk or what not. If you leave it on the ramdisk, you'll lose it if the server powers it off.

part 2 here

NOTE - Have corrected this as i was only storing about 1/5 of the data, so have upped the data storage points on the RR. This was calculated at one data point every 60 seconds. So 60 seconds in 24 hrs = 1440 points.
Hourly points for a year = 366 *24 = 8784 points. I rounded this to 8800