Go Down

Topic: web server to read file from SD card and show its content as HTML (Read 2 times) previous topic - next topic

belalsaleh

Jan 08, 2013, 12:16 pm Last Edit: Jan 08, 2013, 12:19 pm by belalsaleh Reason: 1
I'm working on a monitoring project that logs some data to the SD card of the official Ethernet Shield as csv file, I want to show the logs contents on a web page that is produced by the arduino , but when I try to open the csv file for reading after the client is connected to the server , I get an error but when I try to open it on startup its working OK.

I have read that I should switch between ethernet SS PIN and SD card SS pin but I cannot manage to make it work !!

Is it possible to switch ethernet SS pin while the client is connected and then switch it back after reading from SD card without losing the client connection??

here is my code :
Code: [Select]

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2, 230);
EthernetServer server(80);

File myFile;

void setup()
{
 
 Ethernet.begin(mac, ip);
 server.begin();

 Serial.begin(9600);
 
 Serial.print("Initializing SD card...");
 pinMode(10, OUTPUT);
 
  if (!SD.begin(4)) {
   Serial.println("initialization failed!");
   return;
 }
 Serial.println("initialization done.");
}


void loop()
{
EthernetClient client = server.available();
 if (client)
 {
   boolean current_line_is_blank = true;
   while (client.connected()) {
     if (client.available()) {
       char c = client.read();
       if (c == '\n' && current_line_is_blank) {
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println();
         client.println("<h2>CSV Data : </h2>");

        // csv file data
        myFile = SD.open("datalog.csv");
        if (myFile) {
        Serial.println("datalog.csv:");
 
       // read from the file until there's nothing else in it:
       while (myFile.available()) {
          client.println(myFile.read());
       }
       myFile.close();
     } else {
       Serial.println("error opening datalog.csv"); // ################### I GOT THIS ERROR
     }

         break;
       }
       if (c == '\n') {
         current_line_is_blank = true;
       } else if (c != '\r') {
         current_line_is_blank = false;
       }
     }
   }
   delay(1);
   client.stop();
 }

}


SurferTim

Quote
Is it possible to switch ethernet SS pin while the client is connected and then switch it back after reading from SD card without losing the client connection??

Yes. I use both for FTP. Here is my code:
http://playground.arduino.cc/Code/FTP
It uses two sockets and the SD simultaneously to transfer files to or from my FTP server.

The SS pins are handled by the low level libraries of each device. The trick is setting them up. Both devices need the SS line HIGH when leaving the setup() function.

belalsaleh

Thank you Tim for your answer ,
I have changed my code to set them both HIGH before the end of setup()..
but still having the same error !

SurferTim

If you can't open the file with SD.open(filename), then there are normally two reasons.
1) Opening a SD file takes a lot of SRAM, and you ran out.
2) The file doesn't exist

Quote
I get an error but when I try to open it on startup its working OK.

Post the code that did work when you opened the file in setup().

belalsaleh

I'm sure the file exists because the same exact code is working OK on startup():
and the file is only about 21 words ..

Code: [Select]

void setup()
{
 
  Ethernet.begin(mac, ip);
  server.begin();

  Serial.begin(9600);
 
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);
 
   if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

          myFile = SD.open("datalog.csv");
         if (myFile) {
         Serial.println("datalog.csv:");
   
        // read from the file until there's nothing else in it:
        while (myFile.available()) {
           Serial.println(myFile.read());// ################ I GOT THE FILE CONTENTS ON THE SERIAL MONITOR SCREEN
        }
        myFile.close();
      } else {
        Serial.println("error opening datalog.csv");
      }
}

Go Up