From 5d40ad3acbf1b20926402a828d77890bcac39326 Mon Sep 17 00:00:00 2001 From: open Date: Thu, 18 Jan 2024 19:32:12 +0100 Subject: [PATCH] fix: HTTP server should work --- esp32-timed-switch.ino | 103 +++++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 44 deletions(-) diff --git a/esp32-timed-switch.ino b/esp32-timed-switch.ino index d5f9339..f9c8e45 100644 --- a/esp32-timed-switch.ino +++ b/esp32-timed-switch.ino @@ -13,7 +13,7 @@ // -------------------------------------------- -const char* html_app="ESP32 timed Switch

Scheduler

"; +const char* html_app="ESP32 timed Switch

Scheduler

"; // PREFERENCES @@ -22,10 +22,10 @@ Preferences preferences; #define RO_MODE true // WIFI -// const char* ssid = "/tmp/lab"; -// const char* password = "bitte2cucung"; -const char* ssid = "Wokwi-GUEST"; -const char* password = ""; +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 @@ -50,9 +50,21 @@ uint32_t current_ts = 0; uint32_t next_event_ts = 0; int scheduler_1[24] ; // = {A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60,A60}; int scheduler_2[24] ; // = {A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00}; +int scheduler_3[24] ; // = {A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00}; +int scheduler_4[24] ; // = {A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00}; +int scheduler_5[24] ; // = {A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00}; +int scheduler_6[24] ; // = {A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00}; +int scheduler_7[24] ; // = {A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00}; +int scheduler_8[24] ; // = {A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00,A00}; int* scheduler_list[] = { scheduler_1, - scheduler_2 + scheduler_2, + scheduler_3, + scheduler_4, + scheduler_5, + scheduler_6, + scheduler_7, + scheduler_8, }; // PINS @@ -66,7 +78,13 @@ unsigned int PIN_7 = 32; unsigned int PIN_8 = 35; unsigned int pin_list[2] = { PIN_1, - PIN_2 + PIN_2, + PIN_3, + PIN_4, + PIN_5, + PIN_6, + PIN_7, + PIN_8 }; // HTTP @@ -156,7 +174,7 @@ void run_scheduler(){ // If not expected to run exit current_ts = get_timestamp( current_hour, current_minute, current_second ); - Serial.printf("onTimer::check event %d %d\n", current_ts, next_event_ts); + // Serial.printf("onTimer::check event %d %d\n", current_ts, next_event_ts); if( event_keep_waiting( current_ts, next_event_ts ) ){ return; } @@ -186,7 +204,6 @@ void run_scheduler(){ } Serial.println(buffer); // Get the expected status } - Serial.println("Reconfiguration over"); return; } @@ -271,6 +288,7 @@ void setup(){ Serial.println(WiFi.localIP()); // Todo + } @@ -285,6 +303,7 @@ void loop(){ client = http_server.available(); if (!client) { + // Serial.println("Server not available... skip..."); return; } @@ -293,15 +312,17 @@ void loop(){ // GET request if (request.indexOf("GET /api/schedule/") >= 0) { - int apiIndex = request.indexOf("/api/schedule/"); - int startIdIndex = apiIndex + strlen("/api/schedule/"); - int endIdIndex = request.indexOf(" ", startIdIndex); + int apiIndex = 4; // request.indexOf("/api/schedule/"); + int startIdIndex = 18; // apiIndex + strlen("/api/schedule/"); + int endIdIndex = 19; // request.indexOf(" ", startIdIndex); + String plugIdStr = request.substring(startIdIndex, endIdIndex); int plugId = plugIdStr.toInt() - 1; // Adjust for 0 index - + Serial.print("plugId " ); + Serial.println(plugId); if (plugId >= 0 && plugId < 8) { client.println("HTTP/1.1 200 OK"); - client.println("Content-Type: text/plain"); + client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); @@ -314,43 +335,37 @@ void loop(){ // GET homepage } else if (request.indexOf("GET /") >= 0) { client.println("HTTP/1.1 200 OK"); - client.println("Content-Type: text/plain"); + client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); client.println(html_app); - } + } else { + + // POST request + int slashIndex = request.indexOf("POST /api/schedule/"); + if (slashIndex != -1) { + Serial.println("slashIndex " + slashIndex); + int nextSlashIndex = slashIndex + 17; + + // Extract the plug ID + String plugIdStr = request.substring(slashIndex + 1, nextSlashIndex); + Serial.println("plugIdStr " + plugIdStr); + int plugId = plugIdStr.toInt(); + delay(500); + // Read the next line which should have the POST body/content + String postBody = client.readStringUntil('\n'); + + // Update the schedule for the specified plug + if (plugId >= 0 && plugId < 8) { + // deserializeSchedule(&client, scheduler_list[plugId]); + Serial.println("Schedule updated for plug " + plugIdStr); + } else { + Serial.println("Invalid plug ID"); + } - // POST request - int slashIndex = request.indexOf("POST /api/schedule/"); - if (slashIndex != -1) { - int nextSlashIndex = request.indexOf("/", slashIndex + 14); - if (nextSlashIndex == -1) { - nextSlashIndex = request.indexOf(" ", slashIndex + 14); } - - // Extract the plug ID - String plugIdStr = request.substring(slashIndex + 14, nextSlashIndex); - int plugId = plugIdStr.toInt(); - - // Read the next line which should have the POST body/content - String postBody = client.readStringUntil('\n'); - - // Update the schedule for the specified plug - if (plugId >= 0 && plugId < 8) { - // 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); - client.stop();