Directory for web sever esp32

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 :
image

The directory of file:

How I store HTML file (I store HTML file in data file ) :

your ESP does not have access to your PC... does it?
there is likely no such path as "C:/Users/lengh/OneDrive/Desktop/Bms/data/index.html" in your SPIFF

it's just index.html that got copied over

I think so. Cause i use / index.html but the website still blank as i use the ip address from esp32 to access the website. How could i set up the esp32 to access my pc?

you don't want the ESP to access your PC hard drive.. (it can't really)

you want to upload the content of the data directory into the SPIFF file system of your ESP manually (I assume you've done that - did you ??)

try this code

#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);

void setup() {

  Serial.begin(115200);

  // Initialize SPIFFS
  if (!SPIFFS.begin()) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    while (true) yield();
  }

  // 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, "/index.html", "text/html" );
  });

  server.begin();
}

void loop() {}

and see if your file (from the ESP file system) gets served

Oh . I think i not add any content to Spiff.Is this how u add it ( follow link)?

you said you have an ESP32, right?

see Install ESP32 Filesystem Uploader in Arduino IDE | Random Nerd Tutorials

(At the moment, this is not compatible with Arduino 2.0.)

Oh , thank you i will check it out

Arduino 2.0 mean Arduino IDE ver 2.0 right ?

yes indeed, IDE version.

if you want data upload, use 1.8.19 and the ESP32 data upload tool

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.