Skip to content

Instantly share code, notes, and snippets.

@mateo08c
Created March 9, 2025 13:37
Show Gist options
  • Save mateo08c/344de04977614d8d47330f2d632b493b to your computer and use it in GitHub Desktop.
Save mateo08c/344de04977614d8d47330f2d632b493b to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ESPFS.h>
#include <detail/mimetable.h>
AsyncWebServer server(80);
auto ssid = "";
auto password = "";
// Function to connect to WiFi
void connectToWiFi() {
Serial.println("Connexion au WiFi...");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
const IPAddress ip(192, 168, 1, 130);
const IPAddress gateway(192, 168, 1, 254);
const IPAddress subnet(255, 255, 255, 0);
const IPAddress dns(192, 168, 1, 254);
WiFi.config(ip, gateway, subnet, dns);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println();
Serial.println("WiFi connecté!");
Serial.print("Adresse IP: ");
Serial.println(WiFi.localIP());
} else {
Serial.println();
Serial.println("Échec de connexion WiFi. Passage en mode AP.");
// Fallback to AP mode
WiFi.mode(WIFI_AP);
WiFi.softAP("esp-captive");
Serial.println("Mode point d'accès activé");
Serial.print("Adresse IP du point d'accès: ");
Serial.println(WiFi.softAPIP());
}
}
void listFiles(FS &fs, const char *dirname) {
Serial.println("Liste des fichiers:");
File root = fs.open(dirname, "r");
if (!root) {
Serial.println("Erreur d'ouverture du répertoire.");
return;
}
if (root.isDirectory()) {
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print("Répertoire: ");
Serial.println(file.name());
listFiles(fs, file.name()); // Appel récursif pour lister les sous-répertoires
} else {
Serial.print("Fichier: ");
Serial.println(file.name());
}
file = root.openNextFile();
}
}
}
bool handleStaticFile(AsyncWebServerRequest *request) {
String path = request->url();
// If root is requested, default to index.html
if (path.endsWith("/")) path += "index.html";
// Prepend /www/ to match your file structure
path = "/www" + path;
// Check if file exists
if (!ESPFS.exists(path)) {
Serial.print("File not found: ");
Serial.println(path);
return false;
}
// Determine content type
const String contentType = mime::getContentType(path);
// Serve the file with correct MIME type
request->send(ESPFS, path, contentType);
Serial.print("Served file: ");
Serial.print(path);
Serial.print(" with content type: ");
Serial.println(contentType);
return true;
}
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Démarrage de l'ESP8266...");
// Initialize ESPFS
if (!ESPFS.begin()) {
Serial.println("Erreur de montage du système de fichiers ESPFS!");
return;
}
// Connect to WiFi
connectToWiFi();
// Serve static files with explicit content type handling
server.serveStatic("/", ESPFS, "/www/")
.setDefaultFile("index.html")
.setCacheControl("max-age=3600"); // 1-hour cache
// Custom handler for more detailed file serving
server.onNotFound([](AsyncWebServerRequest *request) {
if (!handleStaticFile(request)) {
request->send(404, "text/plain", "Not Found");
}
});
// Start server
server.begin();
Serial.println("Serveur HTTP démarré");
}
void loop() {
// Check WiFi connection status and reconnect if needed
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Connexion WiFi perdue. Tentative de reconnexion...");
connectToWiFi();
}
delay(10000); // Check every 10 seconds
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment