Go Down

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


Thank you very much Tim for your support and patience,

after hours of debugging I found out that the file does not open from the first time I call "SD.open" , but does open correctly the second time,

I have tried to delay some time after the open call and before the read call , but with no success..

so I added the open code once again right after the first call and it's working  :) :
Code: [Select]

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

That seems odd I know , but still better than nothing..


Below is some test code you might try if you know you have a good file on your sd card. I've noticed that under some conditions the sd card may become unaccessable and require the card to be removed and reinserted or the arduino cold booted with the card in it to regain access to the sd card.

Code: [Select]

//zoomkat 12/26/12
//SD server test code
//open serial monitor to see what the arduino receives
//address will look like when submited
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
String readString;


void setup(){


  // disable w5100 while setting up SD
  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);



void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
        //if HTTP request has ended
        if (c == '\n') {

          Serial.println(readString); //print to serial monitor for debuging

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          //client.println("Content-Type: image/jpeg");
          //client.println("Content-Type: image/gif");
          //client.println("Content-Type: application/x-javascript");
          //client.println("Content-Type: text");

          //File myFile = SD.open("boom.htm");
          File myFile = SD.open("HYPNO.JPG");
          //File myFile = SD.open("BLUEH_SL.GIF");
          //File myFile = SD.open("SERVOSLD.HTM");
          if (myFile) {
            // read from the file until there's nothing else in it:
            while (myFile.available()) {
            // close the file:

            //stopping client

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up