I try to make a web server from esp32 . I try to direct html file in the esp32 with both /index.html and C:/Users/lengh/OneDrive/Desktop/Bms/data/index.html . However, when i open with IP address the esp32 provide , the website appear as error. When direct the html file should i use
/index.html
or
C:/Users/lengh/OneDrive/Desktop/Bms/data/index.html
for directory the html file ?
The directory code snap shot:
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "C:/Users/lengh/OneDrive/Desktop/Bms/data/index.html");
});
full code
#include <string.h>
#include <Arduino.h>
#include "temperature.h"
#include "charge.h"
#include "current.h"
#include <Wire.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
#include "SPIFFS.h"
// Replace with your network credentials
const char* ssid = "TP_link";
const char* password = "hi";
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
String temperature_text;
String percentage_text;
temperature bms;
charge battery ;
current battery_2;
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// Initialize SPIFFS
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "C:/Users/lengh/OneDrive/Desktop/Bms/data/index.html");
});
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", temperature_text.c_str());
});
server.on("/percentage", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", percentage_text.c_str() );
});
server.begin();
battery.begin();
bms.begin();
battery_2.begin();
}
void loop() {
String temperature_text = String(temperature_battery,3) ;
String percentage_text = String(percentage_value) ;
bms.calculate_temperature();
battery.pin_multiplexer();
battery.check_voltage();
battery.percentage();
battery_2.check_current();
}
Website error image :
The directory of file:
How I store HTML file (I store HTML file in data file ) :