Go Down

Topic: SD Card list files to HTML strange output (Read 3 times) previous topic - next topic

pacosino

Hi,
I'm creating a web output from a Mega 2560 for senzors value and sd card content and I use the code belllow that I found on a site. The strange problem is that the output of the file names is not a string, but a number like for instance as in the bellow output. Whay doesn't print correctly the filenames?
Quote
SD Card Files:
73667795767912649.698869
    86796865707912649.698869
    667979846988.767971
    83698286736773/
    6873837549/


Arduino sketch is this:
Code: [Select]
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
created 18 Dec 2009
by David A. Mellis
modified 4 Sep 2010
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,100, 199);
// Initialize the Ethernet server library with the IP address and port you want to use (port 80 is default for HTTP):
EthernetServer server(80);

// include the SD library:
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module, Arduino Ethernet shield: pin 4
const int chipSelect = 4; 

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip);  // start the Ethernet connection and the server:
  server.begin();
}

#define BUFSIZ 100
void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline character) and the line is blank, the http request has ended, so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          // output the value of each analog input pin
          client.print("<html><head><title>test page</title><meta http-equiv='refresh' content='5'></head><body><b>page title</b><hr /><br /><table border=1><tr><td><b>SENZOR</b></td><td><b>TYPE</b></td><td><b>PIN</b></td><td><b>VALUE</b></td></tr>");
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            if (analogChannel == 0) {
                client.print("<tr><td>LIGHT</td><td>A</td><td>"); //example of senzor
                client.print(analogChannel);
                client.print("</td><td>");
                client.print(analogRead(analogChannel));
                client.print("</td></tr>");
            }
            else{
                client.print("<tr><td>N/A</td><td>A</td><td>");
                client.print(analogChannel);
                client.print("</td><td>");
                client.print(analogRead(analogChannel));
                client.print("</td></tr>");
            }
          }
          for (int digitalChannel = 0; digitalChannel < 54; digitalChannel++) {
                client.print("<tr><td>N/A</td><td>D</td><td>");
                client.print(digitalChannel);
                client.print("</td><td>");
                client.print(digitalRead(digitalChannel));
                client.print("</td></tr>");
          }
          client.print("</table></body><hr />");
         
          //Start SD Card files explorer section
          client.println("<h2>SD Card Files:</h2>");
          // print all the files on the SD Card
          pinMode(53, OUTPUT);     // change this to 53 on a mega
          // we'll use the initialization code from the utility libraries since we're just testing if the card is working!
          if (!card.init(SPI_HALF_SPEED, chipSelect)) {
            Serial.println("initialization failed. Things to check:* card is inserted? * wiring correct?did you change the chipSelect pin to match shield/module?");
            return;
          } else {
           Serial.println("SD Card is present.");
          }
          if (!volume.init(card)) {
            Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
            return;
          }
         
          root.openRoot(volume);  //access the root of the SD card volume
          // list all files in the card with date and size
          root.ls(LS_R | LS_DATE | LS_SIZE);  //for serial box output
          dir_t p;
          root.rewind();
          client.println("<ul>");
          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("<li><a href=\"");
            for (uint8_t i = 0; i < 11; i++) {
              if (p.name[i] == ' ') continue;
              if (i == 8) {
                client.print('.');
              }
              client.print(p.name[i]);
            }
            client.print("\">");
         
            // print file name with possible blank fill
            for (uint8_t i = 0; i < 11; i++) {
              if (p.name[i] == ' ') continue;
              if (i == 8) {
                client.print('.');
              }
              client.print(p.name[i]);
            }
            client.print("</a>");
         
            if (DIR_IS_SUBDIR(&p)) {
              client.print('/');
            }

            // print modify date/time if requested
            if (0 & LS_DATE) {
               root.printFatDate(p.lastWriteDate);
               client.print(' ');
               root.printFatTime(p.lastWriteTime);
            }
            // print size if requested
            if (!DIR_IS_SUBDIR(&p) && (0 & LS_SIZE)) {
              client.print(' ');
              client.print(p.fileSize);
            }
            client.println("</li>");
          }
          client.println("</ul>");
         
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

cnavnath

you need to write
file.write(String(filename))
you need to convert that to String befor print

pacosino

I changed the part where the filenames are written to html as seen bellow:
Code: [Select]
// print any indent spaces
            client.print("<li><a href=\"");
            for (uint8_t i = 0; i < 11; i++) {
              if (p.name[i] == ' ') continue;
              if (i == 8) {
                client.print('.');
              }
              client.print(String(p.name[i]));
            }
            client.print("\">");
         
            // print file name with possible blank fill
            for (uint8_t i = 0; i < 11; i++) {
              if (p.name[i] == ' ') continue;
              if (i == 8) {
                client.print('.');
              }
              client.print(String(p.name[i]));
            }


But the output is the same (for now I commented the datetime and size part of the code, for simplicity):
Quote
SD Card Files:
    76738384656712649.848884
    65826885737879.728477
    7865787984656683.7483


cnavnath

oops now i get what you need
so you need to write client.write() function instead of client.print()

cnavnath

and dont use that String in client.write

Go Up