Skip to content

Instantly share code, notes, and snippets.

@shige-ta
Last active June 28, 2018 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shige-ta/d82808a9e88cb20f1c051aa9d34b6f5f to your computer and use it in GitHub Desktop.
Save shige-ta/d82808a9e88cb20f1c051aa9d34b6f5f to your computer and use it in GitHub Desktop.
#include <WiFi.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
const char* ssid = "";
const char* password = "";
WiFiServer server(80);
String listDir(fs::FS &fs, const char * dirname, uint8_t levels){
String s = "";
File root = fs.open(dirname);
if(!root){
return s;
}
if(!root.isDirectory()){
return s;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
s.concat(" DIR : ");
s.concat(file.name());
s.concat("<br>");
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
s.concat(" FILE: ");
s.concat(file.name());
s.concat(" SIZE: ");
s.concat(file.size());
s.concat("0000<br>");
}
file = root.openNextFile();
}
return s;
}
void createDir(fs::FS &fs, const char * path){
Serial.printf("Creating Dir: %s\n", path);
if(fs.mkdir(path)){
Serial.println("Dir created");
} else {
Serial.println("mkdir failed");
}
}
String sd()
{
if(!SD.begin()){
Serial.println("Card Mount Failed");
return "";
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return "";
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
return listDir(SD, "/", 0);
}
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println();
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.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
String sdlistdata;
void loop(){
WiFiClient client = server.available();
if (client) {
Serial.println("New Client.");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("Click <a href=\"/H\">reload</a><br>");
client.println();
client.print(sdlistdata);
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
if (currentLine.endsWith("GET /H")) {
sdlistdata = sd();
}
}
}
client.stop();
Serial.println("Client Disconnected.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment