From 513a4c505665d83418dc191d9ee906c6557fcbed Mon Sep 17 00:00:00 2001 From: Pyramond Date: Tue, 17 Jun 2025 09:42:43 +0200 Subject: [PATCH] Split into header/cpp --- web.cpp | 328 ++++++++++++++++++++++++++++++++++++++++++++++++++++ web.h | 350 +++----------------------------------------------------- 2 files changed, 341 insertions(+), 337 deletions(-) create mode 100644 web.cpp diff --git a/web.cpp b/web.cpp new file mode 100644 index 0000000..25dff81 --- /dev/null +++ b/web.cpp @@ -0,0 +1,328 @@ +#include "web.h" +#include +#include +#include + +// Utilisateurs du réseau +const char *ssid = "morpion-SAE"; +const char *password = "12345678"; +NetworkServer server(80); + +void setupWeb() { + Serial.println(); + Serial.println("Configuring access point..."); + if (!WiFi.softAP(ssid, password)) { + log_e("Soft AP creation failed."); + while (1); + } + IPAddress myIP = WiFi.softAPIP(); + Serial.print("AP IP address: "); + Serial.println(myIP); + server.begin(); + + Serial.println("Server started"); + + display.clearDisplay(); + display.setCursor(0, 0); + display.println("Connectez vous a :"); + display.print("Nom : "); + display.println(ssid); + display.print("Mdp : "); + display.println(password); + display.println(); + display.print("Puis accedez a \nl'adresse "); + display.print(myIP); + display.display(); +} + +void webGame(NetworkClient &c){ + c.println(R"rawliteral( + + + + + + + Morpion Duo/Solo + + + +

Morpion

+ +
Joueur 1 (Bleu) à jouer
+
+
+
+
+
+
+
+
+
+
+
+ + + + +)rawliteral"); +} + +void webSolo(NetworkClient &c) { + c.println(""); + c.println(""); + c.println(""); + c.println(""); + c.println(""); + c.println(""); + c.println("
"); + c.println("Pas encore commence !"); + c.println("
"); + c.println(""); + c.println(""); +} + +void loopWeb() { + NetworkClient client = server.accept(); + if (client) { + Serial.println("New Client."); + String currentLine = ""; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + Serial.write(c); + if (c == '\n') { + if (currentLine.length() == 0) { + client.println("HTTP/1.1 200 OK"); + client.println("Content-type:text/html"); + client.println(); + webGame(client); + client.println(); + break; + } else { + currentLine = ""; + } + } else if (c != '\r') { + currentLine += c; + } + } + } + client.stop(); + Serial.println("Client Disconnected."); + } +} diff --git a/web.h b/web.h index 95bf6c2..8d008d8 100644 --- a/web.h +++ b/web.h @@ -1,344 +1,20 @@ +#ifndef WEBSERVER_H +#define WEBSERVER_H + #include #include #include -const char *ssid = "morpion-SAE"; -const char *password = "12345678"; -NetworkServer server(80); -NetworkClient client = server.accept(); +// SSID & mot de passe du réseau WiFi +extern const char *ssid; +extern const char *password; -void setupWeb(){ - Serial.println(); - Serial.println("Configuring access point..."); - if (!WiFi.softAP(ssid, password)) { - log_e("Soft AP creation failed."); - while (1); - } - IPAddress myIP = WiFi.softAPIP(); - Serial.print("AP IP address: "); - Serial.println(myIP); - server.begin(); +// Serveur et client web +extern NetworkServer server; - Serial.println("Server started"); +void setupWeb(); +void loopWeb(); +void webGame(NetworkClient &c); +void webSolo(NetworkClient &c); - display.clearDisplay(); - display.setCursor(0, 0); - display.println("Connectez vous a :"); - display.print("Nom : "); - display.println(ssid); - display.print("Mdp : "); - display.println(password); - display.println(); - display.print("Puis accedez a \nl'adresse "); - display.print(myIP); - display.display(); -} - - - - - -void webGame(NetworkClient &c){ - c.println(R"rawliteral( - - - - - - - Morpion Duo/Solo - - - -

Morpion

- -
Joueur 1 (Bleu) à jouer
-
-
-
-
-
-
-
-
-
-
-
- - - - -)rawliteral"); -} - - - - -void webSolo(NetworkClient &c){ - c.println(""); - c.println(""); - c.println(""); - c.println(""); - c.println(""); - - c.println(""); - c.println("
"); - c.println("Pas encore commence !"); - c.println("
"); - - c.println(""); - c.println(""); -} - - - - -void loopWeb(){ - NetworkClient client = server.accept(); - if (client) { - Serial.println("New Client."); - String currentLine = ""; - while (client.connected()) { - if (client.available()) { // if there's bytes to read from the client, - char c = client.read(); // read a byte, then - Serial.write(c); - if (c == '\n') { // if the byte is a newline character - // if the current line is blank, you got two newline characters in a row. - // that's the end of the client HTTP request, so send a response: - if (currentLine.length() == 0) { - // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) and a content-type so the client knows what's coming, then a blank line: - client.println("HTTP/1.1 200 OK"); - client.println("Content-type:text/html"); - client.println(); - - // the content of the HTTP response follows the header: - webGame(client); - client.println(); - break; - } else { // if you got a newline, then clear currentLine: - currentLine = ""; - } - } else if (c != '\r') { // if you got anything else but a carriage return character, - currentLine += c; // add it to the end of the currentLine - } - } - } - client.stop(); - Serial.println("Client Disconnected."); - } -} \ No newline at end of file +#endif // WEBSERVER_H