diff --git a/esp32-timed-switch.ino b/esp32-timed-switch.ino index f9c8e45..01c7902 100644 --- a/esp32-timed-switch.ino +++ b/esp32-timed-switch.ino @@ -76,7 +76,7 @@ unsigned int PIN_5 = 20; unsigned int PIN_6 = 33; unsigned int PIN_7 = 32; unsigned int PIN_8 = 35; -unsigned int pin_list[2] = { +unsigned int pin_list[8] = { PIN_1, PIN_2, PIN_3, @@ -210,6 +210,7 @@ void run_scheduler(){ // HTTP +// void deserializeSchedule(WiFiClient &client, int schedule[24]) { void deserializeSchedule(WiFiClient &client, int schedule[24]) { client.readBytes((char*)schedule, 24 * sizeof(int)); } @@ -241,13 +242,24 @@ void setup(){ if (pref_init == false) { Serial.println("preferences not exist"); preferences.putBool("test", true); - preferences.putBytes(RELAY_1_SCHEDULE, scheduler_1_default, sizeof(scheduler_1_default)); - preferences.putBytes(RELAY_2_SCHEDULE, scheduler_2_default, sizeof(scheduler_2_default)); - /// ... more to come + preferences.putBytes(RELAY_1_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_1_default)); + preferences.putBytes(RELAY_2_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); + preferences.putBytes(RELAY_3_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); + preferences.putBytes(RELAY_4_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); + preferences.putBytes(RELAY_5_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); + preferences.putBytes(RELAY_6_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); + preferences.putBytes(RELAY_7_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); + preferences.putBytes(RELAY_8_SCHEDULE, scheduler_default_deactivate, sizeof(scheduler_2_default)); } bool test = preferences.getBool("test"); preferences.getBytes(RELAY_1_SCHEDULE, scheduler_1, preferences.getBytesLength(RELAY_1_SCHEDULE)); preferences.getBytes(RELAY_2_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_2_SCHEDULE)); + preferences.getBytes(RELAY_3_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_3_SCHEDULE)); + preferences.getBytes(RELAY_4_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_4_SCHEDULE)); + preferences.getBytes(RELAY_5_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_5_SCHEDULE)); + preferences.getBytes(RELAY_6_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_6_SCHEDULE)); + preferences.getBytes(RELAY_7_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_7_SCHEDULE)); + preferences.getBytes(RELAY_8_SCHEDULE, scheduler_2, preferences.getBytesLength(RELAY_8_SCHEDULE)); preferences.end(); Serial.println("Setup::PREFERENCES::end"); @@ -308,7 +320,9 @@ void loop(){ } String request = client.readStringUntil('\r'); - client.flush(); + + Serial.print("request: "); + Serial.println(request); // GET request if (request.indexOf("GET /api/schedule/") >= 0) { @@ -340,29 +354,51 @@ void loop(){ 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; + Serial.print("slashIndex: "); + Serial.println(slashIndex); + int nextSlashIndex = slashIndex + 19; // Extract the plug ID - String plugIdStr = request.substring(slashIndex + 1, nextSlashIndex); - Serial.println("plugIdStr " + plugIdStr); - int plugId = plugIdStr.toInt(); + String plugIdStr = request.substring(nextSlashIndex, nextSlashIndex + 1); + Serial.print("plugIdStr "); + Serial.println(plugIdStr); + int plugId = plugIdStr.toInt() - 1; delay(500); + // Read the next line which should have the POST body/content - String postBody = client.readStringUntil('\n'); + String preBody ; + while ( preBody = client.readStringUntil('\n') ){ + + Serial.print("Read preBody: "); + Serial.println(preBody); + if( prebody == ""){ + break; + } + + } + String postBody = client.readString(); + Serial.print("Read postBody: "); + Serial.println(postBody); // Update the schedule for the specified plug if (plugId >= 0 && plugId < 8) { // deserializeSchedule(&client, scheduler_list[plugId]); + char *buffer ; + // size_t count = client.readBytes(buffer, 24 * sizeof(int)); + + 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(); } } diff --git a/scheduler.h b/scheduler.h index c70d601..90c836b 100644 --- a/scheduler.h +++ b/scheduler.h @@ -39,6 +39,32 @@ unsigned int scheduler_1_default[24] = { EVERY_2_INV, EVERY_2, }; +unsigned int scheduler_default_deactivate[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, +}; unsigned int scheduler_2_default[24] = { EVERY_2, EVERY_2_INV,