Hello,
I have been following along your ebook and while most examples have been generally successful after some troubleshooting, I have yet to find a way to get the example from section 4.1 to work.
When I upload the code to my esp8266 (and I have created the approprate js, css and html files in a data folder in my project’ I get the error in the serial monitor “Reading file: /input1.txt – failed to open file for reading. Reading file: /input2.txt – failed to open file for reading.”
When I navigate to the ip address in a browser it says that the site cannot be found. Other examples from the book are running fine when tested so I know there is no issue with my board.
code:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include “LittleFS.h”
#include <Arduino_JSON.h>
// Replace with your network credentials
const char* ssid = “I put in the correct ssid”;
const char* password = “i put in the corred password”;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
// Search for parameter in HTTP POST request
const char* PARAM_INPUT_1 = “input1”;
const char* PARAM_INPUT_2 = “input2”;
//Variables to save values from HTML form
String input1;
String input2;
// File paths to save input values permanently
const char* input1Path = “/input1.txt”;
const char* input2Path = “/input2.txt”;
JSONVar values;
// Initialize LittleFS
void initFS() {
if (!LittleFS.begin()) {
Serial.println(“An error has occurred while mounting LittleFS”);
}
else{
Serial.println(“LittleFS mounted successfully”);
}
}
// Read File from LittleFS
String readFile(fs::FS &fs, const char * path){
Serial.printf(“Reading file: %s\r\n”, path);
File file = fs.open(path, “r”);
if(!file || file.isDirectory()){
Serial.println(“- failed to open file for reading”);
return String();
}
String fileContent;
while(file.available()){
fileContent = file.readStringUntil(‘\n’);
break;
}
return fileContent;
}
// Write file to LittleFS
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf(“Writing file: %s\r\n”, path);
File file = fs.open(path, “w”);
if(!file){
Serial.println(“- failed to open file for writing”);
return;
}
if(file.print(message)){
Serial.println(“- file written”);
} else {
Serial.println(“- frite failed”);
}
}
// Initialize WiFi
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.begin(ssid, password);
Serial.print(“Connecting to WiFi ..”);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(‘.’);
delay(1000);
}
Serial.println(WiFi.localIP());
}
String getCurrentInputValues(){
values[“textValue”] = input1;
values[“numberValue”] = input2;
String jsonString = JSON.stringify(values);
return jsonString;
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
initWiFi();
initFS();
// Load values saved in LittleFS
input1 = readFile(LittleFS, input1Path);
input2 = readFile(LittleFS, input2Path);
// Web Server Root URL
server.on(“/”, HTTP_GET, [](AsyncWebServerRequest *request){
request->send(LittleFS, “/index.html”, “text/html”);
});
server.serveStatic(“/”, LittleFS, “/”);
server.on(“/values”, HTTP_GET, [](AsyncWebServerRequest *request){
String json = getCurrentInputValues();
request->send(200, “application/json”, json);
json = String();
});
server.on(“/”, HTTP_POST, [](AsyncWebServerRequest *request) {
int params = request->params();
for(int i=0;i<params;i++){
AsyncWebParameter* p = request->getParam(i);
if(p->isPost()){
// HTTP POST input1 value
if (p->name() == PARAM_INPUT_1) {
input1 = p->value().c_str();
Serial.print(“Input 1 set to: “);
Serial.println(input1);
// Write file to save value
writeFile(LittleFS, input1Path, input1.c_str());
}
// HTTP POST input2 value
if (p->name() == PARAM_INPUT_2) {
input2 = p->value().c_str();
Serial.print(“Input 2 set to: “);
Serial.println(input2);
// Write file to save value
writeFile(LittleFS, input2Path, input2.c_str());
}
//Serial.printf(“POST[%s]: %s\n”, p->name().c_str(), p->value().c_str());
}
}
request->send(LittleFS, “/index.html”, “text/html”);
});
server.begin();
}
void loop() {
/*Serial.println(readFile(LittleFS, input1Path));
Serial.println(readFile(LittleFS, input2Path));
delay(10000);*/
}
Nevermind got it to work. Not sure what I did. Its possible I might have forgotten to upload the filesystem image before.
Nevermind got it to work. Not sure what I did. Its possible I might have forgotten to upload the filesystem image before.
Nevermind got it to work. Not sure what I did. Its possible I might have forgotten to upload the filesystem image before.