10.25
In this part we’ll connect to a webpage and fetch some data from a php script.
When building a project it’s better to test and debug each part on its own instead of trying to debug a much larger system. So we’ll start by attaching an ethernet shield and get it to talk to a webpage. Once that is up and running we’ll connect the second arduino and rf.
Note: For this part you’ll need a webhosting account with a company who provides access to PHP on it’s accounts. Or you’ll need to run a local webserver. Contact your webhost to verify if your account has PHP access enabled.
First we’ll create a script named ‘time.php’ which will print out the date and time when we visit www.yourdomain.com/time.php . Open the file and paste the following code:
<?php
// Prints something like: Sunday 25th of October 2009 07:12:46 PM
echo date('l jS \of F Y h:i:s A');
?>
Now close, save and upload time.php to your domain. If you put the script in the www or public_html folder it will be accesible from www.yourdomain/time.php if you’ve placed it inside another folder make sure to use www.yourdomain.com/folder/time.php in the next part.
Before we continue, open a webbrowser and go to www.yourdomain/time.php or wherever you placed the script and make sure it shows the date and time.
When the script is working, start the arduino IDE , create a new sketch and copy paste the code below
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { xxx, xxx, xxx, xxx};
byte server[] = { yyy, yyy, yyy, yyy};
Client client(server, 80);
void setup()
{
pinMode(switchPin, INPUT);
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.print("GET http://www.yourdomain.com/folder/time.php HTTP/1.0\n");
client.print("Host: www.yourdomain.com\n\n");
client.println();
}
else {
Serial.println("connection failed");
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
byte ip[] = { xxx, xxx, xxx, xxx};
Make sure to change the xxx, xxx, xxx, xxx with your settings, where xxx, xxx, xxx, xxx should be an available ip in the range of your network. Check your router for the range of ip-adresses in your network.
in my case the above line would read :
byte ip[] = { 192, 168, 1, 36};
Next is the ip address of the server you wish to connect to. If your webhost does not provide you with a unique ip dedicated to your domain you should try using google’s ip. To check if your domain is asigned a unique ip ( your domain is the only one hosted on that ip ) open a command prompt and ping your domain with ‘ping www.yourdomain.com’. On the next line the ip adress associated with your domain should show up on windows in the form of ‘Ping statistics for domain xxx.xxx.xxx.xxx’. Now open a browser and type in xxx.xxx.xxx.xxx instead of www.yourdomain.com. If you are seeing your domain, you have a unique ip assigned to your domain. If not, ping www.google.com and use the ip adress returned for the yyy.yyy.yyy.yyy below.
byte server[] = { yyy, yyy, yyy, yyy};
So the server ip becomes :
byte server[] = { 74, 125, 67, 100};
These settings should get you up and running. The only thing left to do is change these lines to match your domain and the url of the time.php script :
client.print("GET http://www.yourdomain.com/folder/time.php HTTP/1.0\n");
client.print("Host: www.yourdomain.com\n\n");
Now upload the code to the arduino and open the serial monitor.
If all went well the monitor should show something similar:
As you can see second to last line displays the correct date and time.
Check part 3 where I show you how to put it all together to create a weblogging wireless motion detector.


Hey like what you’re doing! Im currently developing a similar concept, I have build little sensor nodes with xBee radios and each node has a motion detector, temp sensor and light sensor, and it relays back to a central node with the webserver on it, but uploading to a php script seems a lot better in terms of performance and storage capacity, could you upload a picture using php? Ive currently added on a camera, serving up images but it is very slow and only 1 connection at a time. Im terrible at web coding php, html etc. hence why I have stuck with having the webserver on the arduino.
Nice! I’ve been looking into wireless sensor networks for a couple of years now but never gotten to them (yet). I remember a similar project and hardware over at libelium . Do you have a website or blog where you log your project(s)? You can always mail me with details if you prefer. I’m going to post the php code this friday or saterday when I get the time. The reason I didn’t post it yet was that I had some trouble with permissions on a host of mine so everything worked just fine, except the logging of data. I’ll contact you when I’ve put up part 3.