First my apologize for my bad anglish. I'm a noob to arduino and programming, may be somebody can help me to solved my problem.
I have a TXT on SD Card, i want to read the file line by line and send it using ethernet shield. I have Mega 2560 and official ethernet shield with micro SD slot.
Here is my skecth...
#include <TinyGPS.h>
#include <SD.h>
#include <stdlib.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xD2, 0xC2 };
IPAddress ip(192,168,0,3);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);
IPAddress server(192,168,0,5);
EthernetClient client;
boolean SDavailable;
char inChar;
int connectLoop;
char netBuffer[60];
byte myTest[5];
void setup() {
Serial.begin(9600);
// disable w5100
pinMode(10,OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(10, LOW); //ethernet on
digitalWrite(4, HIGH); //SD card off
Ethernet.begin(mac, ip, gateway);
Serial.print("Starting SD...");
digitalWrite(10, HIGH);
digitalWrite(4, LOW);
if(!SD.begin(4)) {
Serial.println("failed");
SDavailable = false;
}
else {
SDavailable = true;
Serial.println("ok");
}
}
void loop() {
digitalWrite(10, LOW);
digitalWrite(4, HIGH);
if(client.connect(server, 80))
{
//Serial.println("connected...");
//Serial.println("ARDUINO: forming HTTP request message");
client.print("GET /arduino/insert2.php?netBuffer=");
client.print(netBuffer);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close\r\n");
// read server response
while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
// set connectLoop to zero if a packet arrives
connectLoop = 0;
}
//connectLoop++;
// if more than 10000 milliseconds since the last packet
if(connectLoop > 10000)
{
// then close the connection from this end.
Serial.println();
Serial.println(F("Timeout"));
client.stop();
}
// this is a delay for the connectLoop timing
delay(1);
}
Serial.println();
Serial.println(F("disconnecting."));
// close client end
client.stop();
//client.flush();
}
else
{
Serial.println("connection failure");
}
//String dataString = "";
//dataString = netBuffer;
//Serial.println(dataString);
//LineNo++;
if(SDavailable != false) {
digitalWrite(4, LOW);
digitalWrite(10, HIGH);
File fh = SD.open("log.txt",FILE_READ);
int chPos = 0;
int lineNo = 0;
if(!fh)
{
Serial.println("SD open fail");
return;
}
while(fh.available())
{
char ch = fh.read();
if(ch == '\n') {
chPos = 0;
sscanf(netBuffer,"%u","%u","%u","%u","%u","%u",&myTest[0],&myTest[1],&myTest[2],&myTest[3],&myTest[4],&myTest[5]);
Serial.println(netBuffer);
lineNo++;
}
else if(ch == '\r') {
// do nothing
}
else if(chPos < 59) {
netBuffer[chPos] = ch;
chPos++;
netBuffer[chPos] = 0;
}
}
fh.close();
}
}
this is the file content
CRM001,20130527094818,-6.88125,107.58240,0.43,305.50
CRM001,20130527094819,-6.88125,107.58240,0.91,185.26
CRM001,20130527094821,-6.88126,107.58240,1.50,184.12
CRM001,20130527094822,-6.88127,107.58239,1.56,192.71
CRM001,20130527094824,-6.88128,107.58239,0.94,200.54
CRM001,20130527094826,-6.88127,107.58239,0.61,324.25
The sketch above give me the correct output in Serial monitor but when i send it to my gateway (php) on my local server, it only send the last line from the txt file. My plan is to log gps data to sd card for every 10 second (i already have a working sketch for this) and create a new file for every certain period i.e a day, week or month. When a client is connected i want to read the file on sd card line by line send it to my gateway application which will store the data to mysql database. When there's no client connected then i only want to log the data to sd card and remember the last sent record so if the client connect again it will continue sent from the last record.
What is the best way to do this plan since the logger will be used for years.
Any advice will be much appreciated.
Thank you