Data to web

To ensure that the data is available for reporting and viewing I transfer the data to my host and then parse the URL with some PHP code to then write it to the database.

In this process the first step was to get my Arduino Ethernet connected and do a successful test. To do this I used the Ethernet WebClient which connects to google and shows some text coming up on the (serial) monitor.

When using and testing this I did find that I didn’t always remember to update and set the correct MAC address (on the backside of the board). I needed to depower the board before it would work correctly again.

The 2nd step was to try and connect to my own website with some URL parameters. This took me a day or 2 to get it working correctly 🙂

This resulted in the following code;

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x8E, 0xDB };
char server[] = "yourweb.com"; // without the www
 
// Initialize the Ethernet client library
EthernetClient client;
 
String URLdata="";
unsigned long ReadData=112211;
 
void setup() {
   Serial.begin(9600);                   
 
 // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  Serial.println("Starting.....");
  delay(5000);
}
 
void loop()
{
    sendData();
}
 
// this method makes a HTTP connection to the server:
void sendData() {
  // put together the data part
  URLdata="type=test";
  URLdata+="&reading=";
  URLdata+=ReadData;
 
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    // This is the URL from the base call, where I will be 
    // receiving the data. Using this the full setup would be
    // www.yourweb.com/power/insert_readings.php?.. then the params
 
    client.print("GET /power/insert_readings.php?");
    client.print(URLdata);
    client.println(" HTTP/1.1"); 
    client.println("Host: yourweb.com");
    client.println();
    Serial.print(URLdata);
    delay(100);
    client.stop();
  } 
  else 
  {
    // if you couldn't make a connection:
    // write to SD card as backup
    writeSDcard(URLdata);
  }
}

While testing this setup I had a couple of issues;

  1. I wanted to cleanup my code, so I removed all code that printed ‘empty’ lines. Including the client.println(); at the end of the ethernet connection – DON’T. It is required! 🙂
  2. After each call to my webpage you can first test if the connection is alive. I upload all my data 1 x every 10 min, and to keep things consistant, I connect, and when finished disconnect (client.stop();). Using this I have had no connectivity issues.

To process the data on my website, I have the following code in PHP to read and insert into the MYSQL database. How to configure, setup and data structure I will leave up to you!

Using a couple of bits of code found on the net, I use the following code:

PHP code    
<?php
// ip control
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
	$ip = getenv("HTTP_CLIENT_IP");
} else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
	$ip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
	$ip = getenv("REMOTE_ADDR");
} else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
	$ip = $_SERVER['REMOTE_ADDR'];
} else {
	$ip = "unknown";
}
 
echo "IP adres is: $ip <br>";
 
if ($ip==" ** here your IP Address ** ")  // www.whatismyip.com
{
	echo "Welcome! <br>";
}
else
{
	echo "Sorry, no access allowed <br> <br>";
	exit();
}
 
$date = date('m/d/Y h:i:s a', time());
echo "$date <br>";
$username = " ** you username ** ";
$password = " ** your password **";
$hostname = "localhost"; 
 
//connection to the database
$dbh = mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db("power",$dbh);
echo "Connected to MySQL<br>";
 
$type.=$_GET['type'];
$readData.=$_GET['reading'];
 
echo "$type en $readData  <br>";
$q=mysql_query(" INSERT INTO table (type,readings) VALUES ('{$type}','{$readData}') ")or die(mysql_error());
mysql_close();
 
echo "Insert complete<br>";
?>