So, for my sensor network, the Gumstix Linux box is running PHP 5.0 and the Cherokee web server. This seems to be very robust and I haven't had to reboot it in ages. The main webpage comes up on my internal network like this:
This displays the main info off of the remote sensors and also takes an RSS feed off of www.weatherunderground.com for the current weather in Hamilton, Ontario. This allows me to cross-check the temp readings with the actual weather reported for the area. Note that this still includes a "moisture" reading, but the sensor is now been taken off the system. This was my attempt to detect if it was raining or not based on this circuit from Rob Faludi.
The LED2 On button allows me to control the secondary LED on the remote unit. This is essentially just a way to test remote controlling a digital output over the web and the Zigbee link. When you click the button, it launches a small script that sends out an "h", which turns the light ON. The button will then toggle to the "off" mode and when you press it again it sends an "l" (lower case L) to turn off the LED.
Below is the source code in all it's messy glory. It uses the indispensable "php_serial.class.php" class from Remy Sanchez (found here ) which is absolutely essential to do anything between PHP and your serial port. The script then grabs the Weather Underground RSS feed and generates the HTML to load the web page.
Arduino.php:
<?php include "php_serial.class.php"; // configure the serial port using php_serial.class.php $serial = new phpSerial; $serial->deviceSet("/dev/ttyS2"); $serial->confBaudRate(9600); $serial->confParity("none"); $serial->confCharacterLength(8); $serial->confStopBits(1); $serial->confFlowControl("none"); $serial->deviceOpen(); $serial->sendMessage("g"); usleep(100000); $serial->flush; $read = $serial->readPort(); $ard_array = explode(',',$read,7); if (empty($ard_array[6])){ usleep(100000); $serial->sendMessage("g"); usleep(100000); $read = $serial->readPort(); $ard_array = explode(',',$read,7); } if ($ard_array[0] == 1) { $plusMinus = "-"; } elseif ($ard_array[0] == 0){ $plusMinus = "+"; } $weather = simplexml_load_file('http://www.weatherunderground.com/auto/rss_full/global/stations/71297.xml?units=both'); echo "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>"; echo "<html>"; echo "<head>"; echo "<meta content='text/html; charset=ISO-8859-1' http-equiv='content-type'>"; echo "<meta http-equiv='refresh' content='6'>"; echo "<img style='float: left; width: 81px; height: 90px;' src='images/arduino-nano.jpg'>"; echo " <title>Arduino Control</title>"; echo "</head>"; echo "<body>"; echo "<br>"; echo "<h1><span style='font-family: Arial;'>Arduino Control Panel</h1>"; echo "<br></br>"; echo "<table style='text-align: left; width: 50%;' border='1' cellpadding='2' cellspacing='2'>"; echo "<tbody>"; echo "<tr>"; echo "<th>Deg C</th>"; echo "<th>Light Level</th>"; echo "<th>LED 1 ON/OFF</th>"; echo "<th>Moisture</th>"; echo "<th>LED 2 ON/OFF</th></tr>"; echo "<tr>"; echo "<td>$plusMinus$ard_array[1]</td>"; echo "<td>$ard_array[2]</td>"; echo "<td>$ard_array[3]</td>"; echo "<td>$ard_array[4]</td>"; echo "<td>$ard_array[5]</td>"; echo "</tr>"; echo "</tbody>"; echo "</table>"; echo "<br>"; echo "<h2><span style='font-family: Arial;'>Current Weather in Hamilton</h2>"; foreach ($weather->channel->item as $item) { $w = $item->description . ""; $newchar = str_replace("|", "", $w); $newestchar = str_replace("Temperature","Current temp", $newchar); $newest2char = str_replace("°F","F",$newestchar); $newest3char = str_replace("°C","C",$newest2char); $newest4char = str_replace("percent","%",$newest3char); $newest5char = str_replace(" / "," ",$newest4char); $newest6char = str_replace(" "," ",$newest5char); $newest7char = str_replace("Direction:","Dir:",$newest6char); $newerchar = trim($newest7char); $wchar = substr($newerchar,0,80); $wchar = substr($newerchar,0,300); echo "<table style='text-align: left; width: 50%;' border='1' cellpadding='2' cellspacing='2'>"; echo "<tbody>"; echo "<tr>"; echo "<td>$wchar</td>"; echo "</tr>"; echo "</tbody>"; echo "</table>"; } echo "<br>"; echo "<a href='arduino_data2.php'>View Log</a><br>"; echo "<br>"; echo "<table>"; echo "<tbody>"; echo "<tr>"; echo "<td>"; if ($ard_array[5] == 1) { echo "<form action='led_off.php' method='get'>"; echo "<input type='hidden' name='variablename' value='variablevalue'>"; echo "<input type='submit' value='TURN LED2 OFF'/>"; echo "</form>"; } if ($ard_array[5] == 0){ echo "</td>"; echo "<td>"; echo "<form action='led_on.php' method='get'>"; echo "<input type='hidden' name='variablename' value='variablevalue'>"; echo "<input type='submit' value='TURN LED2 ON'/>"; echo "</form>"; } echo "</td>"; echo "</tbody>"; echo "</table>"; echo "<br>"; echo "<hr style='width: 100%; height: 2px;'>"; echo "<br>"; echo "<img style='float: left; width: 81px; height: 90px;' src='images/gumlogo.gif'>"; echo "<h1><span style='font-family: Arial;'>Gumstix Controls</h1>"; echo "<br>"; echo "<table>"; echo "<tbody>"; echo "<td>"; echo "<form action='turn_off.php' method='get'>"; echo "<input type='hidden' name='variablename' value='variablevalue'>"; echo "<input type='submit' value='TURN OFF GUMSTIX'/>"; echo "</td><td>"; echo "</form>"; echo "<form action='reboot_gum.php' method='get'>"; echo "<input type='hidden' name='variablename' value='variablevalue'>"; echo "<input type='submit' value='REBOOT GUMSTIX'/>"; echo "</form>"; echo "</td>"; echo "</tbody>"; echo "</table>"; echo "<hr style='width: 100%; height: 2px;'>"; echo "<img src='images/powered_by_cherokee.png'>"; echo "</body>"; echo "</html>"; $serial->deviceClose(); ?>
The other function of the Gumstix is also to act as a data logger. Clicking the link for the log takes you to this page:
This is a very small SQLite database that keeps track of the readings of the remote units sensors. The code below uses a PDO statement to open the database and generate the HTML to display its contents. Eventually, I will need to put some further refinements on it so you can select results from different days and maybe graph the temerature.
Arduino_data2.php:
<?php $db = new PDO('sqlite:/media/card/back_up/home/root/arduino.db'); $st = $db->query('SELECT * FROM tbl'); $results = $st->fetchAll(); echo "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>"; echo "<html>"; echo "<head>"; echo "<meta content='text/html; charset=ISO-8859-1' http-equiv='content-type'>"; echo "<meta http-equiv='refresh' content='10' >"; echo " <title>Data Log</title>"; echo "</head>"; echo "<body>"; echo "<h1>Arduino Data Log</h1>"; echo "<br></br>"; echo "<form action='clear_log.php')'>"; echo "<input type='submit' value='Clear Log'/>"; echo "</form>"; echo "<table style='text-align: left; width: 50%;' border='1' cellpadding='2' cellspacing='2'>"; echo "<tbody>"; echo "<tr>"; echo "<th>Index</th>"; echo "<th>Date-Time</th>"; echo "<th>Light Level</th>"; echo "<th>LED1</th>"; echo "<th>Moisture</th>"; echo "<th>LED2</th>"; echo "<th>+/-</th>"; echo "<th>Temp</th>"; echo "</tr>"; $rows = count($results); for ($i=0; $i <= $rows; $i++){ echo "<tr>"; for ($x=0; $x <= 7; $x++){ echo "<td>{$results[$i][$x]}</td>"; } echo "</tr>"; } echo "</tbody>"; echo "</table>"; echo "<br>"; echo "<a href='arduino.php'>Return to Control Panel</a><br>"; echo "</body>"; echo "<html>"; $db = null; ?>
How does the data actually get into the database? This is pretty straightforward. Read the serial port with the aforementioned PHP serial class and put the results into an array, then use the SQL INSERT command to put it into the database:
data_logging.php:
<?php include "php_serial.class.php"; // configure the serial port using php_serial.class.php $serial = new phpSerial; $serial->deviceSet("/dev/ttyS2"); $serial->confBaudRate(9600); $serial->confParity("none"); $serial->confCharacterLength(8); $serial->confStopBits(1); $serial->confFlowControl("none"); $serial->deviceOpen(); $serial->sendMessage("g"); usleep(100000); $serial->flush; $read = $serial->readPort(); $ard_array = explode(',',$read,7); $serial->deviceClose(); if ($ard_array[0] == 0){ $ard_array[0] = "'+'"; } elseif ($ard_array[0] == 1){ $ard_array[0] = "'-'"; } //print_r($ard_array); putenv("TZ=UTC+5"); $db = new PDO('sqlite:/media/card/back_up/home/root/arduino.db'); $db->exec("INSERT INTO tbl (lightLev,LED1,SW1,LED2,plus_minus,temp) VALUES ($ard_array[2],$ard_array[3],$ard_array[4],$ard_array[5],$ard_array[0],$ard_array[1])"); Print("Insert successful\n"); ?>
This is driven by a crontab item on the Gumstix that runs every 15 minutes. For those out there who might be struggling to figure out crontab on Linux, the way I did this was via the command "crontab -e" and I included the line:
*/15 * * * * /usr/bin/php /var/www/data_logging.php > /dev/null
Well, that should be enough for now on my sensor network project. Soon, I will have to start actually building some of the elements of the solar pool heater concept that started all this. I now have some solenoid valves and a small pump, so I need to do a proptype of an Arduino managing flud control. Maybe I might do something again with RFID first!