My sketch compiles and properly displays a web page on a browser that navigates to the server on the esp8266. The problem is clearly in retrieving the web page from flash memory since the server properly serves the Hello World string when that line is not commented out. My sketch is:
#include <Arduino.h>
#include <ESP8266WiFi.h> // Enables the ESP8266 to connect to the local network (via WiFi)
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <FS.h>
#include <LittleFS.h>
// Create AsyncWebServer object
AsyncWebServer server(80);
void handleRoot(); // function prototypes for HTTP handlers
void handleNotFound();
const char *location = "rc";
const char *ssid = "ssid";
const char *wifi_password = "password";
void setup_wifi()
{
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
void setup()
{
// Setup wifi
Serial.begin(115200);
setup_wifi();
Serial.print("\n\n");
Serial.print("Location: ");
Serial.println(location);
// Initialze LittleFS
if (!LittleFS.begin())
{
Serial.println("An error has occurred while mounting LittleFS");
return;
}
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(LittleFS, "/index.html", String(), false); });
// server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
// { request->send(LittleFS, "/index.html", "text/plain"); });
// server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
// { request->send(200, "text/plain", "hello world!||"); });
server.on("/app.js", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(LittleFS, "/app.js", String(), false); });
server.begin();
Serial.println("HTTP server started");
}
void loop()
{
}
The platformio.ini file is:
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
monitor_speed = 115200
board_build.filesystem = littlefs
board_build.ldscript = eagle.flash.4m3m.ld
The index.html and the app.js file are in the data directory on the same level as the src directory.
There are no compilation or linking errors.
Why is the code not retrieving and sending the index.html file?