server sd files logger

I am trying to implement a Data logger for a Davis meteo station.
I used the server example, the problem is that when I remotely access a file the arduino seems to stop working
I stops logging and rading dataand serving.

If any of you can give any suggestions I'd be glad

Thanks,

#include <SdFat.h>
#include <SdFatUtil.h>
#include <Ethernet.h>
#include <SPI.h>
#include <SoftwareSerial.h>

//%%%%%%%%%%%%%%%%%%%%%%%% ETHERNET STUFF %%%%%%%%%%%%%%%%%%%%%%%%/
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 174 };
EthernetServer server(80);

//%%%%%%%%%%%%%%%%%%%%%%%% SDCARD STUFF %%%%%%%%%%%%%%%%%%%%%%%%/
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
SdFile myFile;
SdFat sd;

SoftwareSerial Davis(5, 3); // RX, TX
char FileName[13];
int t[50]={-1};
int i=1;
const int id=13;
int Min=-10;
int d[60];
long previousMillis;

double temp;
int hum;
double vel_v=0.0001;
int dir_v;
double bar;
double lluv=0.0001;
double lluv_ac=0.0001;
int rad;

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))

void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}

void setup() {
pinMode(17, OUTPUT);
pinMode(18, OUTPUT);
pinMode(19, OUTPUT);
Serial.begin(9600);
Davis.begin(19200);
//PgmPrint("Free RAM: ");
//Serial.println(FreeRam());

// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH); // but turn off the W5100 chip!

if (!card.init(SPI_FULL_SPEED, 4)) error("card.init failed!");

// initialize a FAT volume
if (!volume.init(&card)) error("vol.init failed!");

//PgmPrint("Volume is FAT");
//Serial.println(volume.fatType(),DEC);
//Serial.println();

if (!root.openRoot(&volume)) error("openRoot failed");

// list file in root with date and size
// PgmPrintln("Files found in root:");
// root.ls(LS_DATE | LS_SIZE);
//Serial.println();

// Recursive list of all directories
//PgmPrintln("Files found in all dirs:");
//root.ls(LS_R);

//Serial.println();
//PgmPrintln("Done");

// Debugging complete, we start the server!
Ethernet.begin(mac, ip);
server.begin();
}

void ListFiles(EthernetClient client, uint8_t flags) {
// This code is just copied from SdFile.cpp in the SDFat library
// and tweaked to print to the client output in html!
dir_t p;

root.rewind();
client.println("

    ");
    while (root.readDir(&p) > 0) {
    // done if past last used entry
    if (p.name[0] == DIR_NAME_FREE) break;
    // skip deleted entry and entries for . and ..
    if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;
    // only list subdirectories and files
    if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
    // print any indent spaces
    client.print("
  • <a href="");
    for (uint8_t i = 0; i < 11; i++) {
    if (p.name == ' ') continue;

    • if (i == 8) {*
    • client.print('.');*
    • }*
      _ client.print((char)p.name*);_
      _
      }_
      _
      client.print("">");*_

    * // print file name with possible blank fill*
    * for (uint8_t i = 0; i < 11; i++) {
    _ if (p.name == ' ') continue;
    if (i == 8) {
    client.print('.');
    }
    client.print((char)p.name);
    }*_

    * client.print("");*

    * if (DIR_IS_SUBDIR(&p)) {
    _ client.print('/');
    }
    // print modify date/time if requested*

    * if (flags & LS_DATE) {*
    * root.printFatDate(p.lastWriteDate);
    client.print(' ');
    root.printFatTime(p.lastWriteTime);
    }
    // print size if requested*

    * if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {*
    * client.print(' ');
    client.print(p.fileSize);
    }
    client.println("

  • ");
    }
    client.println("
");
}
// How big our line buffer should be. 100 is plenty!
#define BUFSIZ 64*_

void servidor()
{digitalWrite(17, HIGH);
* char clientline[BUFSIZ];*
* int index = 0;*

* EthernetClient client = server.available();*
* if (client) {digitalWrite(17, HIGH);*
* // an http request ends with a blank line*
* boolean current_line_is_blank = true;
_ // reset the input buffer*
* index = 0;*_

* while (client.connected()) {*
* if (client.available()) {*
* char c = client.read();*

* // If it isn't a new line, add the character to the buffer*
* if (c != '\n' && c != '\r') {*
* clientline[index] = c;*
* index++;*
* // are we too big for the buffer? start tossing out data*
* if (index >= BUFSIZ)*
* index = BUFSIZ -1;*

* // continue to read more data!*
* continue;*
* }*

* // got a \n or \r new line, which means the string is done*
* clientline[index] = 0;*

* // Print it out for debugging*
* Serial.println(clientline);*

* // Look for substring such as a request to get the root file*
* if (strstr(clientline, "GET / ") != 0) {*
* // send a standard http response header*
* client.println("HTTP/1.1 200 OK");*
* client.println("Content-Type: text/html");*
* client.println();*

* // print all the files, use a helper to keep it clean*
* client.println("

- Archivos -

");
ListFiles(client, LS_SIZE);
_ } else if (strstr(clientline, "GET /") != 0) {
// this time no space after the /, so a sub-file!*

char *filename;_

* filename = clientline + 5; // look after the "GET /" (5 chars)*
* // a little trick, look for the " HTTP/1.1" string and*
* // turn the first character of the substring into a 0 to clear it out.*
* (strstr(clientline, " HTTP"))[0] = 0;*

* // print the file we want*
* //Serial.println(filename);*

* if (! file.open(&root, filename, O_READ)) {
_ //client.println("HTTP/1.1 404 Not Found");
//client.println("Content-Type: text/html");
//client.println();
client.println("

File Not Found!

");
client.stop();
return;
}*_

* //Serial.println("Opened!");*

* client.println("HTTP/1.1 200 OK");*
* client.println("Content-Type: text/plain");*
* client.println();*

* int16_t c;
_ while ((c = file.read()) > 0) {
// uncomment the serial to debug (slow!)
//Serial.print((char)c);
client.print((char)c);
}
file.close();
client.stop();
return;
} else {
// everything else is a 404*

* client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
client.println("

File Not Found!

");
client.stop();
return;
}
client.stop();
return;
}
}
// give the web browser time to receive the data*

* delay(2500);
client.stop();
digitalWrite(17, LOW);
}
}
//////////////////////////////////////////////////////////////////////
void LeerFH()
{digitalWrite(19, LOW);
Davis.println("GETTIME");
delay(10);
t[3]=0;
for(i=1;i<=8;i++) // (Davis.available())
{
t=Davis.read();
delay(5);
}
Davis.flush();
i=1; // reiniciar el contador*

* t[7]=t[7]-100; //ajuste al año*_

if(t[3]<0||t[3]>59||t[4]<0||t[4]>23||t[5]<1||t[5]>31||t[6]<1||t[6]>12||t[7]<0||t[7]>100)
* {delay(25);*
* Davis.println("GETTIME");*
* delay(10);*
* t[3]=0;*
* for(i=1;i<=8;i++) // (Davis.available())*
_ {t*=Davis.read();
delay(5);
}
Davis.flush();
i=1; // reiniciar el contador*

* t[7]=t[7]-100; //ajuste al año*
* }
if(t[3]<0||t[3]>59||t[4]<0||t[4]>23||t[5]<1||t[5]>31||t[6]<1||t[6]>12||t[7]<0||t[7]>100)
{delay(50);
Davis.println("GETTIME");
delay(10);
t[3]=0;
for(i=1;i<=8;i++) // (Davis.available())
{t=Davis.read();
delay(5);
}
Davis.flush();
i=1; // reiniciar el contador*

* t[7]=t[7]-100; //ajuste al año*
* }
Serial.print("Fecha y hora=");
Serial.print(t[7]+2000);
Serial.print("-");
Serial.print(t[6]);
Serial.print("-");
Serial.print(t[5]);
Serial.print(" ");
Serial.print(t[4]);
Serial.print(":");
Serial.print(t[3]);
Serial.print(":");
Serial.println(t[2]);
//delay(10);
digitalWrite(19, HIGH);
}
//////////////////////////////////////////////////////////////////////*_

//////////////////////////////////////////////////////////////////////

void LeerSe()
{ digitalWrite(19, LOW);
Davis.println("LOOP 1");
delay(4);
for (i=1;i<=60;i++)//Davis.available())
{ d*=Davis.read();*

  • delay(4);*
  • }*
  • Davis.flush();*
  • i=1;*
    _ temp=0.5*((d[15]*256+d[14])-320)/9; // ºC_
  • hum=(d[35]); // % *
    vel_v=0.44704*d[16]; // m/s
  • dir_v=d[18]; // º*
    _ bar=0.0254*(d[10]*256+d[9]); // mmHg_
    _ lluv=(d[44]*256+d[43])*0.2; // mm/hr_
    lluv_ac=(d[57]*256+d[56])*0.2; // mm
    _ rad=(d[47]*256+d[46]); // W/m2_
    //// **************************************************************************************
    //for(i=1;i<=2;i++)
    //{
  • if(temp<-20||temp>100)*
  • { Davis.println("LOOP 1");*
  • delay(4);*
  • for (i=1;i<=2;i++)*
    _ { d*=Davis.read();_
    _
    delay(4);_
    _
    }_
    _ temp=0.5((d[15]256+d[14])-320)/9; // ºC_
    _
    }_
    _
    if (hum<0||hum>100)_
    _
    { Davis.println("LOOP 1");_
    _
    delay(4);_
    _
    for (i=1;i<=2;i++)_
    _ { d=Davis.read();
    delay(4);
    }
    hum=(d[35]); // %
    }_
    if (vel_v>50|| vel_v<0)
    _ { Davis.println("LOOP 1");
    delay(4);
    for (i=1;i<=2;i++)
    { d=Davis.read();
    delay(4);
    }_

    vel_v=0.44704d[16];

    }
    * if (dir_v>360||dir_v<0||dir_v==255)
    _ { Davis.println("LOOP 1");
    delay(4);
    for (i=1;i<=2;i++)
    { d=Davis.read();
    delay(4);
    }_

    dir_v=d[18];
    _}
    if (rad==128||rad>10000||rad<0)
    { Davis.println("LOOP 1");
    delay(4);
    for (i=1;i<=2;i++)
    { d=Davis.read();
    delay(4);
    }
    rad=(d[47]256+d[46]);
    }

    * if (lluv_ac<0)*
    * { Davis.println("LOOP 1");
    delay(4);
    for (i=1;i<=2;i++)
    { d=Davis.read();
    delay(4);
    }
    lluv_ac=(d[57]*256+d[56])*0.2;
    }
    if(lluv<0||lluv>2000)
    { Davis.println("LOOP 1");
    delay(4);
    for (i=1;i<=2;i++)
    { d=Davis.read();
    delay(4);
    }
    lluv=(d[44]256+d[43])0.2;
    }

    if(bar<0)
    { Davis.println("LOOP 1");
    delay(4);
    for (i=1;i<=2;i++)
    { d=Davis.read();
    delay(4);
    }
    bar=0.0254(d[10]256+d[9]);
    }
    //}
    ////////////////////////////////////////////////////////// ASIGNAR VALORES INVALIDANTES 9999*

    if(temp<-20||temp>100)
    * temp=9999;
    if (hum<0||hum>100)
    hum=9999;_

    if (vel_v>50|| vel_v<0)
    vel_v=9999;
    if (dir_v>360||dir_v<0||dir_v==255)
    dir_v=9999;
    if (rad==128||rad>10000||rad<0)
    _ rad=9999;_
    if (lluv_ac<0)//||lluv_ac!=lluv_aca)
    lluv_ac=9999;
    if(lluv<0||lluv>2000)
    _ lluv=9999;
    if(bar<0)
    bar=9999;
    //////////////////////////////////////////////////////////TERMINA VALORES INVALIDANTES*

    //********************************************************************************************
    *//////////////////////////////////////// *_

//Serial.println("Datos leidos");
//Serial.print("temp=");
//Serial.println(temp);
//Serial.print("hum=");
//Serial.println(hum);
//Serial.print("vel_v=");
//Serial.println(vel_v);
//Serial.print("dir_v=");
//Serial.println(dir_v);
Serial.print("bar=");
Serial.println(bar);
//Serial.print("lluv=");
//Serial.println(lluv);
//Serial.print("lluv_ac=");
//Serial.println(lluv_ac);
//Serial.print("dir_v=");
//Serial.println(dir_v);
//Serial.print("rad=");
//Serial.println(rad);
* //}*
* Serial.flush();*
* digitalWrite(19, HIGH);*
}
//////////////////////////////////////////////////////////////////////
void Logger()
{
* if (sd.begin(4, SPI_FULL_SPEED))
_ { digitalWrite(18, LOW);
sprintf(FileName, "%.2d%.2d%.2d%.2d.csv", t[7], t[6], t[5],id); //ddmmaa*

* delay(5);_
if (!myFile.open(FileName, O_RDWR | O_CREAT | O_AT_END))
_ { Serial.println("ERROR DE MEMORIA");
digitalWrite(18, LOW);
delay(200);
}
else*

* { digitalWrite(18, LOW);
myFile.print(t[4]);
myFile.print(":");
myFile.print(t[3]);
myFile.print(":");
myFile.print(t[2]);
myFile.print(",");
myFile.print(temp);
myFile.print(",");
myFile.print(hum);
myFile.print(",");_

myFile.print(vel_v);
_ myFile.print(",");_
myFile.print(dir_v);
_ myFile.print(",");
myFile.print(bar);
myFile.print(",");_

myFile.print(lluv_ac);
_ myFile.print(",");
myFile.println(rad);
myFile.close();
delay(100);
Serial.print("Almacenado en:");
Serial.println(FileName);
digitalWrite(18, HIGH);
}
}
else{
Serial.println("ERROR DE MEMORIA");
digitalWrite(18, LOW);
delay(200);
}
}
//////////////////////////////////////////////////////////////////////
void loop()
{
servidor();
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > 50000||previousMillis==0)
{ previousMillis = currentMillis;
LeerFH();*_

* if(Min!=t[3])*
* {*
* LeerSe();*
* Logger();*
* Min=t[3];*
* t[3]=-1;*
* }*
}
delay(10);
}

I think the problem is in the servidor() function

If you are using an Uno, you are probably running out of SRAM. Try using the F() function on all your static strings to reduce your SRAM use. For example

Serial.println("This string is moved to SRAM before printing");
Serial.println(F("This string is printed directly from program memory"));

The same with the client.print() and client.println() calls also.