This is now my 3rd attempt at being able to read this sd card from within the Esp8266webserver. I have made a class WebServer, and would like my sd card to contain my site. What throws me off, is I am able to access the SD card everywhere with the exception of inside of the .on callbacks.
I can read from/write to the sd card from Everywhere except for inside of a call back. What makes things even more odd, if I init my sd card using SD.begin(0) inside of the call back, it ALSO fails, That really is confusing. For somereason I can't get SD access inside of the callback.
Any ideas or suggestions would really really be appreciated.
so:
void WebServer::handlers(){
// THIS WORKS!!!!!
File myFile;
myFile = SD.open("test.htm");
if (myFile) {
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// END THIS WORKS!!!!
server.on("/", [this](){ sayHello(); }); // <- HERE IS WHERE THE TROUBLE STARTS!!!
server.begin();
}
Here is what isn't working, i have removed some code to make it more easy to see:
void WebServer::sayHello() { //<- HERE IS THE CALLBACK, This always fails
File myFile;
myFile = SD.open("test.htm");
if (myFile) {
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
Serial.println("error opening test.txt");
}
server.send(200, "text/plain", "hello from esp8266!");
}
void WebServer::handlers(){
server.on("/", [this](){ sayHello(); }); // <- HERE IS THE SETUP FOR THE CALLBACK (TROUBLE HERE)
server.begin();
}
Here is my full code for reference:
#include <ESP8266WebServer.h>
#include "webserver.h"
#include <SD.h>
WebServer::WebServer(int n) { //, Storage*& storage
ESP8266WebServer server(n);
if (!SD.begin(0)) {
Serial.println("SD initialization failed!");
} else {
Serial.println("SD initialization success!");
}
}
void WebServer::sayHello() {
File myFile;
myFile = SD.open("test.htm");
if (myFile) {
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
server.send(200, "text/plain", "hello from esp8266!");
}
void WebServer::handlers(){
File myFile;
myFile = SD.open("/test.htm");
if (myFile) {
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
Serial.println("error opening test.txt");
}
server.on("/", [this](){ sayHello(); });
// server.onNotFound(handleNotFound(server));
server.onNotFound([this, SD]() {
handleNotFound();
});
server.begin();
}
void WebServer::handleClients() {
server.handleClient();
}
bool WebServer::stream(File dataFile, String dataType) {
return server.streamFile(dataFile, dataType) != dataFile.size();
}
void WebServer::writeFile(String message) {
File myFile;
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
myFile.println(message);
myFile.close();
}
}
bool WebServer::loadFromSdCard(String path) {
Serial.print(path);
String dataType = "text/plain";
if (path.endsWith("/")) {
path += "index.htm";
}
if (path.endsWith(".src")) {
path = path.substring(0, path.lastIndexOf("."));
} else if (path.endsWith(".htm")) {
dataType = "text/html";
} else if (path.endsWith(".css")) {
dataType = "text/css";
} else if (path.endsWith(".js")) {
dataType = "application/javascript";
} else if (path.endsWith(".png")) {
dataType = "image/png";
} else if (path.endsWith(".gif")) {
dataType = "image/gif";
} else if (path.endsWith(".jpg")) {
dataType = "image/jpeg";
} else if (path.endsWith(".ico")) {
dataType = "image/x-icon";
} else if (path.endsWith(".xml")) {
dataType = "text/xml";
} else if (path.endsWith(".pdf")) {
dataType = "application/pdf";
} else if (path.endsWith(".zip")) {
dataType = "application/zip";
}
File dataFile = SD.open(path.c_str());
if (dataFile.isDirectory()) {
path += "/index.htm";
dataType = "text/html";
dataFile = SD.open(path.c_str());
}
if (!dataFile) {
return false;
}
if (server.hasArg("download")) {
dataType = "application/octet-stream";
}
Serial.print(dataFile.size());
Serial.print("About to send it!");
while (dataFile.available()) {
Serial.write(dataFile.read());
}
if (server.streamFile(dataFile, dataType) != dataFile.size()) {
Serial.print("Returned too little data");
}
dataFile.close();
return true;
}
void WebServer::handleNotFound() {
Serial.print(server.uri());
if (loadFromSdCard(server.uri())) {
return;
}
String message = "SDCARD Not Detected\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " NAME:" + server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
Also, I want to add, I am seeing this message while compiling. Because I can access the files in other places, I have been ignoring it, but I am seeing:
extern SDClass SD;
^
C:\Users\kevin\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.6.3\libraries\SD\src/SD.h:206:16: note: 'SDClass SD' declared here