Gabriella Levine

ongoing and past work

How to get data on the internet without a computer

I did this using the Arduino ethernet shield.

Below is the script for the server that Arduino calls in its HTTP request.

The server has the following function: if there is data added to it through a GET request, it stores that data in a text file (called datalog.txt) that is also on the same server. That way my sensor data can be stored as a text file. Secondly, when the server is called with no request, it shows the text of the text file. This way, I can write a program to pull the data from that website and graph it (processing.js, perhaps… more to come later this week on that).

click here for a text file of the code in case it is not showing up correctly below

<?php
// put the name and path of the text file in a variable.
// this is the text file where we'll store the data:
$filename = 'datalog.txt';

//make sure the file is not empty:
if (file_exists($filename)) {
       // get the contents of the file
       // and put them in a variable called $fileContents:
       $fileContents = file_get_contents($filename);

       // if there is new data from the client, it'll
       // be in a request parameter called "data".
       if (isset($_REQUEST['data'])) {
               // append what the client sent as 'data' to
               // the variable holding the file contents:
               $fileContents = $fileContents . "\n". $_REQUEST['data'];
               // put the file contents back into the file
               // you're overwriting the whole file when you do this:
               file_put_contents($filename, $fileContents);
       } else {
               // there was no data sent in the request
               // so show the old stuff:
               echo $fileContents;
       }
}
?>

From the Arduino side, this is the code I use, for now, to append my text file. It is adapted from the example WebClient in the Ethernet library on Arduino.

I just had to enter my MAC address of the ethernet shield and the IP address of the server I am making the request to.

The next step will be to continually make requests as sensor data comes in, to have a legible CSV.

/*
  Web client
 
 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 18 Dec 2009
 by David A. Mellis
 
 */

#include <SPI.h>
#include <Ethernet.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[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03 };
IPAddress server(69,89,31,63); // Google

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // start the serial library:
  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(;;)
      ;
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /understanding_networks/dataLogger.php?data=it_works_yay HTTP/1.1");
    client.println("HOST: www.levinegabriella.com");
   // client.println("GET /understanding_networks/dataLogger.php?data=1233212321232123 HTTP/1.0");
  // http://www.levinegabriella.com/understanding_networks/dataLogger.php?data=123
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}

Leave a Reply