Pages: [1] 2   Go Down
Author Topic: web server to read file from SD card and show its content as HTML  (Read 2247 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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();
  }

}

« Last Edit: January 08, 2013, 06:19:13 am by belalsaleh » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5846
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 !
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5846
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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");
      }
}
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
still having the same error !

Could you describe the error, please?

-br
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could you describe the error, please?

-br

I cannot open a file for reading from SD card when a client is connected to a web server on my arduino UNO r3 with Ethernet Shield ,
the full code on the first post..

Thank you..
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5846
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I used this as a test, and it worked good.
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// change to your network settings
IPAddress ip( 192,168,2,2 );
IPAddress gateway( 192,168,2,1 );
IPAddress subnet( 255,255,255,0 );

EthernetServer server(80);

void setup()
{
  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");
 
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  digitalWrite(10,HIGH);

  delay(2000);
  server.begin();
  Serial.println("Ready");
}

void loop()
{
  EthernetClient client = server.available();
  if(client) {
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];

    Serial.print("Client request: ");
   
    while (client.connected()) {
      while(client.available()) {
        char c = client.read();

        if(currentLineIsGet && tCount < 63)
        {
          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;         
        }

        if (c == '\n' && currentLineIsBlank) {
          Serial.println(tBuf);
          Serial.print("POST data: ");
          while(client.available()) Serial.write(client.read());
          Serial.println();

          // send a standard http response
          Serial.println("Sending response");
          client.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n");

          File fh = SD.open("index.htm");

          if(fh) {
           
            byte clientBuf[64];
            int clientCount = 0;
 
            while(fh.available()) {
              clientBuf[clientCount] = fh.read();
              clientCount++;
   
              if(clientCount > 63)
              {
                Serial.println("Packet");
                client.write(clientBuf,64);
                clientCount = 0;
              }
            }
         
            if(clientCount > 0) client.write(clientBuf,clientCount);

            fh.close();
          }
          else Serial.println("file open failed");

          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        }
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    Serial.println("done");
  }
}

I uploaded a file to the SD card with the html in it as index.htm. Here is the contents.
Code:
<html><body>
TEST
</body></html>

Does this work for you?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this work for you?
no !!!

The web page is blank, and I got "file open failed" error on the serial monitor:""

Code:
Starting SD..ok
Ready
Client request: GET / HTTP/1.1

POST data:
Sending response
file open failed
done
this is driving me crazy !!! I have tried tens of codes with no lock ...
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5846
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What Arduino are you using? I am testing this on a Mega 2560, and it is showing that code has barely enough SRAM to run on an Uno. Maybe you are running out of SRAM.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using Arduino Uno Rev3..

can I test the SRAM some how ?

or is there is away to reset the arduino to its "factory settings"?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5846
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I include this function code to any sketch I want to check the SRAM.
Code:
int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval); 
}

Then call it something like this.
Code:
Serial.print("Free SRAM = ");
Serial.println(freeRam());
It does not show 0 when you run out of memory. It will show an unrealistic amount of SRAM remaining. If I run my Mega out of memory, it will return more free SRAM than my Mega has.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
or is there is away to reset the arduino to its "factory settings"?
It is returned to it's "factory settings" every time you upload a new sketch (except for EEPROM, but that has nothing to do with your issue).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks you all..

The SRAM is 657  right before the open file failed..
and 813 right before the end of setup()....


Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5846
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That should be pretty close to correct. I figured a bit over 300 remaining as an estimate. If you are not out of memory, I don't know what to tell you. It should work.
Logged

Pages: [1] 2   Go Up
Jump to: