2009
11.07

In this third and final part I’ll show you how to get the arduino to access a website using an ethernet shield and php. The data can then be displayed in a number of ways. Here the data is simply printed one line at a time.

Each time motion is detected, the networked arduino connects to the web and executes a php script which writes the time and date of the event to a text file. When you want to check if motion was detected, acces a different php script which displays all (if any) recorded events.

Check my next post to see how to send sensor data from the arduino to a website using php.

If you followed part 1 and part 2 all you need to do for part 3 is change the code of the receiving arduino since we’re only changing the output from ‘blink a led’ to ‘access a website’.

Sketch :

#include <Ethernet.h>

int ledPin = 13;    // pin for the LED
int RFinPin = 8;    // pin for the RF receiver
int val = 0;         // variable for reading the pin status
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };    // unique mac address
byte ip[] = { 192, 168, 1, 36};        // unique and valid ip for your network
byte server[] = { 77, 222, 78, 32};  // ip google

void setup() {
   Ethernet.begin(mac, ip);
   pinMode(ledPin, OUTPUT);    // declare LED as output
   pinMode(RFinPin, INPUT);    // declare RF receiver as input
   Serial.begin(9600);
   delay(1000);
}

Client client(server, 80);

void loop(){
   val = digitalRead(RFinPin);        // read input value
   if (val == HIGH) {
     digitalWrite(ledPin, HIGH);      // turn LED ON
     if (client.connect()) {
       Serial.println("connected");
       client.println( "GET /folder/movement.php HTTP/1.1");
       client.println("Host: yourdomain.com");
       client.println("Connection: close");
       client.println();
       client.stop();
     } else {
       Serial.println("connection failed");
     }
   } else {
     digitalWrite(ledPin, LOW);       // turn LED OFF
   }
   delay(5000);
}

The script movement.php is executed when the arduino connects to www.yourdomain.com/folder/movement.php

movement.php code :

<?php

$myFile = "movement_data.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = date("D, d M Y H:i:s") . "\n";
fwrite($fh, $stringData);

fclose($fh);

?>

The script display_movement.php takes the movement data and prints one line for every event recorded.

display_movement.php code:

<?php

 $fileName = "movement_data.txt";

 if(file_exists($fileName)){
      $file = fopen($fileName,'r');

      while(!feof($file)){
                $name = fgets($file);
                if($name != NULL){
                echo('<br>'. "Movement registered on : ".$name.'</br>');
			}
        }

        fclose($file);
}
?>

When display_movement.php is executed you should see something like :

movement Arduino wireless motion detector : part 3

In the next post I’ll discuss how to write sensor data (light intensity, temperature,…) to a website and plot a line chart using php.

Back to homepage

  • Share/Bookmark

No Comment.

Add Your Comment
Please leave these two fields as-is:
Prove you are human by reading this resistor:
0Ω+/- 5%

0
0
1
2
3
4
5
6
7
8
9

0
0
1
2
3
4
5
6
7
8
9

0
0
1
2
3
4
5
6
7
8
9

5
5
10
20

Match the sliders on the left to each color band on the resistor.

Click Here for a new resistor image.

If you'd like to learn more, read about resistor color codes here.