Initial commit
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
#include <SPIFFS.h>
|
||||
#include <WiFi.h>
|
||||
#include <WebServer.h>
|
||||
|
||||
const char* ssid = "Box-gut-2.4G";
|
||||
const char* password = "Rut@b@g@93";
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
connectToWiFi();
|
||||
setupSPIFFS();
|
||||
setupWebServer();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
server.handleClient();
|
||||
}
|
||||
|
||||
void connectToWiFi() {
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
Serial.print("Connexion en cours au réseau Wi-Fi...");
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("\nConnecté au réseau Wi-Fi");
|
||||
}
|
||||
|
||||
void setupSPIFFS() {
|
||||
if (SPIFFS.begin()) {
|
||||
Serial.println("Système de fichiers SPIFFS monté avec succès");
|
||||
} else {
|
||||
Serial.println("Erreur lors du montage du système de fichiers SPIFFS");
|
||||
}
|
||||
}
|
||||
|
||||
void setupWebServer() {
|
||||
server.onNotFound(handleNotFound);
|
||||
server.begin();
|
||||
|
||||
Serial.println("Serveur Web démarré");
|
||||
Serial.print("Adresse IP actuelle: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void handleNotFound() {
|
||||
String uri = server.uri();
|
||||
int request = uri.indexOf("/");
|
||||
String message = uri.substring(request + 1);
|
||||
|
||||
if (message.length() == 0) {
|
||||
message = "index";
|
||||
}
|
||||
|
||||
if (request != -1) {
|
||||
serveFile(message);
|
||||
}
|
||||
}
|
||||
|
||||
void serveFile(const String& fileName) {
|
||||
File file = SPIFFS.open("/" + fileName + ".html", "r");
|
||||
if (file) {
|
||||
server.streamFile(file, "text/html");
|
||||
file.close();
|
||||
} else {
|
||||
server.send(404, "text/plain", "Fichier " + fileName + " non trouvé");
|
||||
Serial.print("Not found");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user