From ccab9ddb12fd3c3cf1f27cfa0ab2e044397f3b29 Mon Sep 17 00:00:00 2001 From: alban Date: Thu, 7 Dec 2023 19:52:59 +0100 Subject: [PATCH] feat: http server --- esp32-timed-switch.ino | 48 ++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/esp32-timed-switch.ino b/esp32-timed-switch.ino index 890e873..d5f9339 100644 --- a/esp32-timed-switch.ino +++ b/esp32-timed-switch.ino @@ -13,14 +13,19 @@ // -------------------------------------------- -// PREFRENCES +const char* html_app="ESP32 timed Switch

Scheduler

"; + + +// PREFERENCES Preferences preferences; #define RW_MODE false #define RO_MODE true // WIFI -const char* ssid = "/tmp/lab"; -const char* password = "bitte2cucung"; +// const char* ssid = "/tmp/lab"; +// const char* password = "bitte2cucung"; +const char* ssid = "Wokwi-GUEST"; +const char* password = ""; const uint8_t wifi_loop_max = 10; // NTP @@ -65,7 +70,7 @@ unsigned int pin_list[2] = { }; // HTTP -WiFiClient http_client; +WiFiClient client; WiFiServer http_server(3000); String request; @@ -188,7 +193,7 @@ void run_scheduler(){ // HTTP -void deserializeSchedule(String content, int schedule[24]) { +void deserializeSchedule(WiFiClient &client, int schedule[24]) { client.readBytes((char*)schedule, 24 * sizeof(int)); } @@ -269,18 +274,6 @@ void setup(){ } -void html() { - http_client.println("HTTP/1.1 200 OK"); - http_client.println("Content-Type: text/html"); - http_client.println("Connection: close"); - http_client.println(); - - http_client.println(""); - http_client.println(""); - http_client.println(""); - http_client.println(""); - -} void loop(){ @@ -290,8 +283,8 @@ void loop(){ } - http_client = http_server.available(); - if (!http_client) { + client = http_server.available(); + if (!client) { return; } @@ -318,6 +311,13 @@ void loop(){ client.println("Connection: close"); client.println(); } + // GET homepage + } else if (request.indexOf("GET /") >= 0) { + client.println("HTTP/1.1 200 OK"); + client.println("Content-Type: text/plain"); + client.println("Connection: close"); + client.println(); + client.println(html_app); } // POST request @@ -337,15 +337,21 @@ void loop(){ // Update the schedule for the specified plug if (plugId >= 0 && plugId < 8) { - deserializeSchedule(postBody, scheduler_list[plugId]); + // deserializeSchedule(&client, scheduler_list[plugId]); Serial.println("Schedule updated for plug " + plugIdStr); } else { Serial.println("Invalid plug ID"); } + } + client.println("HTTP/1.1 200 OK"); + client.println("Content-Type: text/plain"); + client.println("Connection: close"); + client.println(); + client.println(html_app); - http_client.stop(); + client.stop(); }