So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By RedFawkx
#78960 Hi, I have a few sensors attached to my nodeMCU and i have successfully stored the values in a few text files in a SDcard connected via a sdcard reader. The files are named "1.txt" and "2.txt" I'm using a router that isnt connected to the internet to help connect the nodemcu to the laptop.


How do I display on a website a directory of all the files available inside the sdcard (like the listfiles example) and download the text files over the internet?

I have tried to display a directory to a website using the code below but it it always tells me that 'client' isnt declared in this scope. I'm not sure how to download the file either. I was thinking of the user using 192.168.x.xxx/dir to see the files he could download and when he sees a file named "1.txt" , typing 192.168.x.xxx/1.txt or 192.168.x.xxx/2.txt will download the respective files to his computer.

Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

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

File root;
const char* ssid = "-----";
const char* password = "------";
WiFiServer server(80);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  //while (!Serial) {
  // wait for serial port to connect. Needed for native USB port only
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");
  // Print the IP address
  Serial.print("Connection IP: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
  // }
  Serial.print("Initializing SD card...");

  if (!SD.begin(15)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

}

void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

  // Match the request

  if (request.indexOf("/dir") != -1)
  {
    root = SD.open("/");
    printDirectory(root, 0);

  }
}

void printDirectory(File dir, int numTabs) {

  while (true) {
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println(""); //  do not forget this one
    client.println("<!DOCTYPE HTML>");
    client.println("<html>");
    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      client.print(':');
    }
     client.print(entry.name());
    if (entry.isDirectory()) {
      client.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      client.print("\t\t");
      client.println(entry.size(), DEC);
    }
    entry.close();
       
  }
client.println("</html>");
}