diff --git a/esp32-timed-switch.ino b/esp32-timed-switch.ino index 01c7902..dd34585 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 @@ -169,7 +169,6 @@ void run_scheduler(){ } } } - } // If not expected to run exit @@ -184,7 +183,7 @@ void run_scheduler(){ Serial.println("Reconfigure the Relays!"); // Run the relays reconfiguration - for ( int i = 0; i < 2; i++ ) { + for ( int i = 0; i < 8; i++ ) { // Get a pointer to the current array int* scheduler = scheduler_list[i]; unsigned int pin = pin_list[i]; @@ -210,15 +209,43 @@ 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)); + +const char* getPreferenceName( int plug_id){ + switch (plug_id) { + case 0: + return RELAY_1_SCHEDULE; + break; + case 1: + return RELAY_2_SCHEDULE; + break; + case 2: + return RELAY_3_SCHEDULE; + break; + case 3: + return RELAY_4_SCHEDULE; + break; + case 4: + return RELAY_5_SCHEDULE; + break; + case 5: + return RELAY_6_SCHEDULE; + break; + case 6: + return RELAY_7_SCHEDULE; + break; + case 7: + return RELAY_8_SCHEDULE; + break; + } } +void saveSchedule( int plug_id, byte schedule[24]){ + const char* preference_name = getPreferenceName( plug_id ); + preferences.putBytes(preference_name, schedule, sizeof(schedule)); +} void sendSchedule(WiFiClient &client, int schedule[24]) { client.write((const uint8_t*)schedule, 24 * sizeof(int)); - } // -------------------------------------------- @@ -330,17 +357,19 @@ void loop(){ 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) { + String plug_idStr = request.substring(startIdIndex, endIdIndex); + int plug_id = plug_idStr.toInt() - 1; // Adjust for 0 index + Serial.print("plug_id " ); + Serial.println(plug_id); + if (plug_id >= 0 && plug_id < 8) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); - sendSchedule(client, scheduler_list[plugId]); + // @todo THIS IS NOT OK + // we need to use the preferences + sendSchedule(client, scheduler_list[plug_id]); } else { client.println("HTTP/1.1 404 Not Found"); client.println("Connection: close"); @@ -363,35 +392,35 @@ void loop(){ int nextSlashIndex = slashIndex + 19; // Extract the plug ID - String plugIdStr = request.substring(nextSlashIndex, nextSlashIndex + 1); - Serial.print("plugIdStr "); - Serial.println(plugIdStr); - int plugId = plugIdStr.toInt() - 1; - delay(500); + String plug_idStr = request.substring(nextSlashIndex, nextSlashIndex + 1); + Serial.print("plug_idStr "); + Serial.println(plug_idStr); + int plug_id = plug_idStr.toInt() - 1; + + // Force a new line + client.readStringUntil('\n'); // Read the next line which should have the POST body/content String preBody ; while ( preBody = client.readStringUntil('\n') ){ - - Serial.print("Read preBody: "); - Serial.println(preBody); - if( prebody == ""){ + Serial.print("Read preBody: '"); + Serial.print(preBody); + Serial.println("'"); + if( preBody.isEmpty() ){ break; } - } - String postBody = client.readString(); + Serial.println("Done reading. Going for binary."); + delay(500); + char postBody[24]; + client.readBytes(postBody, 24); 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); + if (plug_id >= 0 && plug_id < 8) { + saveSchedule(plug_id, byte( atoi(postBody))); + Serial.println("Schedule updated for plug " + plug_idStr); } else { Serial.println("Invalid plug ID"); }