• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Module 3 section 4.1 – failed to open file for reading

Q&A Forum › Category: ESP8266 › Module 3 section 4.1 – failed to open file for reading
0 Vote Up Vote Down
Daniel Cardin asked 1 year ago

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);*/
}


Question Tags: debugging, input, server
4 Answers
0 Vote Up Vote Down
Daniel Cardin answered 1 year ago

Nevermind got it to work. Not sure what I did. Its possible I might have forgotten to upload the filesystem image before. 

0 Vote Up Vote Down
Daniel Cardin answered 1 year ago

Nevermind got it to work. Not sure what I did. Its possible I might have forgotten to upload the filesystem image before. 

0 Vote Up Vote Down
Daniel Cardin answered 1 year ago

Nevermind got it to work. Not sure what I did. Its possible I might have forgotten to upload the filesystem image before. 

0 Vote Up Vote Down
Sara Santos Staff answered 1 year ago

Hi.
I’m glad you found the issue.
When you get the webpage not found error, it’s usually because the filesystem image was not uploaded.
 
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.