Author Topic: Reading SD cards over wifi  (Read 9962 times)

digi_guy

  • Jr. Member
  • **
  • Posts: 87
Reading SD cards over wifi
« on: January 17, 2017, 07:22:39 pm »
I've been playing around with the SC card reader that came with the TFT screen, and put together a script that will display the files as a webpage. Each file is hot-linked so you can then click on the file name and in theory your Oak will display the file.

It's kind of a hog-pog of other projects I've been working on, but the basics came from https://www.arduino.cc/en/Tutorial/listfiles

If I get time I might play around a bit and add the ability to make/destroy files and directories. And it would be nice if it looked prettier but that's not really my thing.


Code: [Select]
#include <ESP8266WiFi.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <SD.h>
// For the Adafruit shield, these are the default.
#define TFT_DC 1
#define TFT_CS 6

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

WiFiServer server(80);  //the port that requests are made on
WiFiClient client;
File webFile;

void setup() {
  server.begin();  //starts listening for requests

  tft.begin();
  tft.setRotation(2);
  tft.fillScreen(ILI9341_BLACK);
  tft.setTextColor(ILI9341_WHITE); 
  tft.setCursor(0, 0); 
  tft.setTextSize(2);

  char myIpString[24];
  IPAddress myIp = WiFi.localIP();
  sprintf(myIpString, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]);
  tft.println("ipAddress: ");   
  tft.println(String(myIp[0])+"."+String(myIp[1])+"."+String(myIp[2])+"."+String(myIp[3]));   

  tft.print("SSID: ");
  tft.println(WiFi.SSID());

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  tft.println("Signal strength:");
  tft.print(rssi);
  tft.println(" dBm");
 
// Initialize the SD card
  SD.begin(4);
 
  }


void loop() {
  // listen for incoming clients
  client = server.available();
  client.setTimeout(50);
 
  if (client) {
    if (client.connected()) {
       
      String inMsg = client.readStringUntil(' '); //client.find("GET ");
      String path = client.readStringUntil(' ');
     
         
      if (path == "/index") {
        sendFiles();
        while (client.read() !=-1); // empty the buffer
        client.stop();
      } else if (path == "/directory") {
        sendFiles();
        while (client.read() !=-1); // empty the buffer
        client.stop();
      } else if (path == "/favicon.ico") {  //respond to favicon request
        while (client.read() !=-1); // empty the buffer
        client.println(F("HTTP/1.1 404 OK"));
        client.println();
        client.println();
        client.stop();   
      }
      else {
        findFiles(path);
        while (client.read() !=-1); // empty the buffer
        client.stop();
      }   
    }
  }
}




void sendResponse(String response){
    sendResponseStart();
    sendResponseChunk(response);
    sendResponseEnd();
}

void sendResponseStart(){
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("Connection: close");
  client.println();
  // send web page
  webFile = SD.open("index.htm");        // open web page file
  webFile.setTimeout(1);
  if (webFile) {
    while(webFile.available()) {
       client.print(webFile.readStringUntil('\n')); // send web page to client       
    }
    webFile.close();
  }
    client.println();
    client.println();   
}

void sendResponseChunk(String response){
    client.println(response.length()+2,HEX);
    client.println(response);
    client.println();
}

void sendResponseEnd(){
    client.println(F("0"));
    client.println();
}


void sendPage(String pageName) {
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("Connection: close");
  client.println();
  // send web page
  webFile = SD.open(pageName);        // open web page file
  if (webFile) {
    while(webFile.available()) {
       client.print(webFile.readString()); // send web page to client       
    }
    webFile.close();
  } else {
    client.println("File not found");
    }
    client.println();

} //send of sendPage


void sendFiles() {
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("Connection: close");
  client.println();
  client.println("<!DOCTYPE html><html><head>");
  client.println("<style>p {text-indent: 25px;} div {left: 10px;  position: relative;} ul {list-style-type: none;}</style>");
  client.println("</head><body>");
  client.println("<h1>Printing files on SD card</h1>");
 
  webFile = SD.open("/");
  client.print("<div><ul>");
  client.print("<div><li><strong>/root</strong></li>");
  printDirectory(webFile);
  client.print("</ul></div>");
  client.println("</body></html>");
  client.println();
}




void printDirectory(File dir) {
   String direct = dir.name();
   if (direct == "/") {direct = "/root";}
   while(true) {

     File entry =  dir.openNextFile();
     
     if (! entry) {
       // no more files
       break;
     }
     
     if (entry.isDirectory()) {
      String dirToSend = "<ul><li><strong>/";
      dirToSend += entry.name();
      dirToSend += "</strong></li>";
      client.print(dirToSend);     
       printDirectory(entry);
       client.print("</ul>");
       
     } else {
      String msgToSend = "<li><a href=\"" + direct + "/" + entry.name() + "\">" + entry.name() + "</a>......" + entry.size() + "</li>";
      client.println(msgToSend);
     }
     entry.close();
   
   }
}

void findFiles(String fileName) {
   
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("Connection: close");
  client.println();
  client.println("<!DOCTYPE html><html><head>");
  client.println("<style>p {text-indent: 25px;} div {left: 10px; position: relative;}</style>");
  client.println("</head><body>");
  client.println("<h4>Displaying: "+fileName+"</h4>");
 
  webFile = SD.open("/");
  client.print("<div>");
  printFile(webFile, fileName);
  client.print("</div><hr><div><a href=\"/index\">Click here to return</a></div></body></html>");
  client.println();
}


void printFile(File dir, String fileName) {
   String direct = dir.name();
   if (direct == "/") {direct = "/root";}
   while(true) {

     File entry =  dir.openNextFile();
     
     if (! entry) {
       // no more files
       break;
     }
     
     if (entry.isDirectory()) {
      printFile(entry, fileName);
     
     } else { // first check if the names match, then display the file
        String currentName = direct+"/"+entry.name();
        if (currentName == fileName) { //display the file}
          while (entry.available()) {
            client.println(entry.readStringUntil('\n'));
          }
        }

     }
     entry.close();
   
   }
}

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Reading SD cards over wifi
« Reply #1 on: January 17, 2017, 11:34:50 pm »
What... no spiffy animations or pretty pictures... how boring!  8)

Keep writing code like this and I'll be able to just copy and paste it as is into my project...  so please keep adding! ;) ;D But seriously, really good to know that this is all coming together, and nothing 'bad' has happened by trying to add another 'module' to the code... I hate it when you do one part (i.e. TFT), and then start working on another (i.e. SD) and everything breaks (and you seriously wonder why you didn't just leave it alone).  :'(

One (semi-unrelated) question... does WiFi.SSID() work on the Oak? If it does, it may help another user with a problem they were having...

digi_guy

  • Jr. Member
  • **
  • Posts: 87
Re: Reading SD cards over wifi
« Reply #2 on: January 18, 2017, 09:13:11 am »
I forgot to mention that to access the list of files simply go to [ip address of your Oak]/index

And yes, WIFI.SSID() works.