Gabriella Levine

ongoing and past work

Logging data remotely to the web, no computer

So I switched over the client to here: http://levinegabriella.com/understanding_networks/liveApplet/

(I have to figure out why no text is appearing on the graph – i had temperature and light and date markers)

the php server is written here: http://levinegabriella.com/understanding_networks/dataLogger.php

The server reads and writes to a text file here: http://www.levinegabriella.com/understanding_networks/liveApplet/DATAcl.TXT

See code below for Arduino and php and processing

Next step: The day I left NY my dataLogging Arduino “crashed”, after working for a week . It could have been any number of things. The SD card, the server, a short?
Try Pachube, and Watch Dog to restart Arduino if it crashes.
Can i do some virtual debugging?

Also, Arduino UNO ran out of space (the functions include: data logger, ethernet connection to a server, temp sensor, light sensor, real time clock – But there are many more sensors on board (gps, humidity, accelerometer, barometric pressure) so I’ve moved everything over to the MEGA. Had to rearrange the SPI / I2C pin connections, but besides that it’s all good.

(all the code (php, processing, arduino) can also be downloaded here)

More to come…

<?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 = 'liveApplet/DATAcl.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 '<p>' . $fileContents . '</p>';
//split the string of the newLines:
$strings = explode("\n", $fileContents);
//loop over the array of the lines, adding a break at the end of each
foreach ($strings as $thisString){
echo $thisString . "<br/>\n";
}
}
}
?>

Arduino:
[/python]
/*
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
#include
#include
#include “RTClib.h”
#include

// 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); // my IP server
const int requestInterval = 30000;
long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
boolean requested;
const int resetLED = 13;
float temp;
float voltage;
// 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):

const int chipSelect = 4;//changed from 8
const int LOCATION_FILE_NUMBER_LSB = 0x00;
const int LOCATION_FILE_NUMBER_MSB = 0x01;

File dataFile;
RTC_DS1307 RTC;
EthernetClient client;
DateTime now;

void setup() {
// start the serial library:
Serial.begin(9600);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
// A2 is the ground, A3 is the power:
digitalWrite(A2, LOW);
digitalWrite(A3, HIGH);
pinMode(10, OUTPUT);

if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:

}
Serial.println(“card initialized.”);

Wire.begin();
RTC.begin();
delay(50);
if (! RTC.isrunning()) {
Serial.println(“RTC is NOT running!”);
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
dataFile = SD.open(“data.txt”, FILE_WRITE);
delay(500);

// start the Ethernet connection:
Ethernet.begin(mac);
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}

// connectToServer();
// give the Ethernet shield a second to initialize:
delay(1500);
blink(resetLED, 3);
Serial.println(“connecting…”);
connectToServer();
}

void loop()
{

now = RTC.now();
if(client.connected()){
if(!requested){

requested = makeRequest();
// Serial.println(“requesting!”);
}
if(millis() – lastAttemptTime>requestInterval){
//if youre not connected and two minutes have passed, attempt to connect again
client.stop();
// Serial.println(“stopping and reconnecting!”);

// getData();
delay(1500);
//connectToServer();
}
// 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();
delay(1500);
if(millis() – lastAttemptTime>requestInterval){
//if youre not connected and two minutes have passed, attempt to connect again
connectToServer();
//try to reconnect here…
}
}

}

void getData(){
voltage = 5 * analogRead(A0) / 1024.0;
//float temp = 5 * analogRead(A1) / 1024.0;
temp=(analogRead(A1))/10;

// Serial.print(voltage);
// Serial.print(F(“,”));
// Serial.print(temp);
// Serial.print(F(“,”));
// Serial.println(” “);
//
if (dataFile) {
DateTime now = RTC.now();

dataFile.print(now.month());
dataFile.print(‘/’);
dataFile.print(now.day());
dataFile.print(‘/’);
dataFile.print(now.year());
dataFile.print(F(“,”));
dataFile.print(now.hour());
dataFile.print(F(“:”));
dataFile.print(now.minute());
dataFile.print(F(“:”));
dataFile.print(now.second());
dataFile.print(F(“,”));
dataFile.print(voltage);
dataFile.print(F(“,”));
dataFile.print(temp);
dataFile.println();

}
dataFile.flush();

}

void connectToServer(){
// Serial.println(“connecting to server…”);
if (client.connect(server, 80)) {
requested = false;
}

lastAttemptTime = millis();
}

boolean makeRequest() {
// Serial.println(“requesting”);
getData();
// Make a HTTP request:

client.print(“GET /understanding_networks/dataLogger.php?data=”);
client.print(now.month());
client.print(‘/’);
client.print(now.day());
client.print(‘/’);
client.print(now.year());
client.print(F(“,”));
client.print(now.hour());
client.print(F(“:”));
client.print(now.minute());
client.print(F(“:”));
client.print(now.second());
client.print(F(“,”));
client.print(voltage);
client.print(F(“,”));
client.print(temp);
client.println(” HTTP/1.1 “);
client.println(“HOST: www.levinegabriella.com”);

client.println();
return true;

}

void blink(int thisPin, int howManyTimes){
for (int blinks = 0;blinks digitalWrite(thisPin, HIGH);
delay(200);
digitalWrite(thisPin, LOW);
delay(200);
}
}

//questions: what other sensors will be good?
//what is too much data
//i seem to be making my request twice

//when my request interval is more than 5000 i get
//10/13/2011,11:14:56,3.67,15.00
//10/13/2011,11:15:5,3.65,15.00
//10/13/2011,11:15:16,0.00,0.00
//10/13/2011,11:15:44,0.00,0.00
//why

//can you go back a directory
[/python]

Leave a Reply