2010
07.24

When I originally constructed the Temperature Graph, it was based on work by the OpenEnergyMonitor, and came with a Perl script on the web server end.  I never much liked this for a couple reasons, its a dying language and I am not at all familiar with it.  I also had to mess around with a cgi-bin directory, and things did not work out of the box with my webserver.

Inspiration hit me the other day, that I should be able to accomplish this in PHP very easily, and this proved to be true.  I had a working script in a few minutes, and had it largely cleaned up and working in no time at all.  Add to this, I am not at all proficient in PHP, I did this entirely with the book “Learning PHP 5″ by David Sklar (ISBN-13 #978-0596005603) and the online PHP documentation at the PHP.net website.

I still need to add some functionality, clean up the code, and add more robust error handling, but this is it!  The php script below (sensitive bits redacted) is fully functional and running on the temperature graph.  It uses the pear module, which did not come with PHP 5 out of the box.  Read the howtos on installation for your distro as your mileage will vary.

<?php
ini_set(‘include_path’, ini_get(‘include_path’));

// load PEAR DB
require ‘DB.php’;

// Connect to the database
$db = DB::connect(‘mysqli://**********:**********@localhost/’ . $_GET["db"]);
if(DB::isError($db))
{
die (“Cannot Connect: ” . $db->getMessage());
}

// Setup automatic error handling
$db->setErrorHandling(PEAR_ERROR_DIE);

$temperature = floatval($_GET["temp"]);
$vtime = time() * 1000;

$q = $db->query(‘INSERT INTO ‘ . $_GET["table"] . ‘ (TEMP_001, Time) VALUES (?,?)’, array($temperature, $vtime));

if (DB::isError($q))
{
die(“query error: ” . $q->getMessage());
}
?>

The URL construction to insert the data into the database is very simple, the parameters are “db”, “table” and “temp”.  db = the database name, table = the table in which to store the data, and temp is the floating point value of the temperature.

For example (sensitive bits redacted)…

http://www.ka1kjz.com/php_db/index.php?db=**********&table=DATA&temp=75.94

And finally, I only had to change the GET statement in the Arduino to get it to work, the sketch is below (sensitive bits redacted), and available for download in the usual place.  You will also need the library and data for the TMP421 temperature sensor.

#include “Wire.h”
#include <LibTemperature.h>
#include <Ethernet.h>

//Sets the unique mac address for the ethernet board
//set this according to your network!!!
byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
byte ip[] = {192,168,2,51};
byte gateway[] = {192,168,2,1};
byte server[] = {***,***,***,***};

//Setup a client
Client client(server, 80);

LibTemperature temp = LibTemperature(0);

void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway);
delay(1000);
}

void loop()
{
float tempC = temp.GetTemperature();
Serial.print(“Temp: “);
Serial.print(tempC);
Serial.print(” degC  “);
float tempF = (tempC * 9 / 5) + 32;
Serial.print(tempF);
Serial.println(” degF”);
delay(100);

//Send the data
if (client.connect())
{
client.print(“GET http://www.ka1kjz.com/******/index.php?db=**********&table=*****&temp=”);
client.print(tempF);
client.println();
client.stop();
}
else
{
Serial.println(“Failed to connect to client”);
}
delay(300000);
}

Share on Facebook
  1. Now do it all using CodeIgniter and then you will be cherried out