diff --git a/esp32-electrical-switch.ino b/esp32-timed-switch.ino similarity index 92% rename from esp32-electrical-switch.ino rename to esp32-timed-switch.ino index f165e6e..ad27b44 100644 --- a/esp32-electrical-switch.ino +++ b/esp32-timed-switch.ino @@ -18,6 +18,7 @@ Preferences preferences; // WIFI const char* ssid = "Wokwi-GUEST"; const char* password = ""; +const uint8_t wifi_loop_max = 10; // NTP bool sntp_initialized = false; @@ -33,6 +34,7 @@ uint16_t timer_interval_in_secs = timer_interval / 1000000; ; // SCHEDULER +bool brun_scheduler = true; uint8_t current_hour = HOUR_DEFAULT; uint8_t current_minute = MINUTE_DEFAULT; uint8_t current_second = 0; @@ -93,8 +95,10 @@ void timeavailable(struct timeval *t) // TIMER Callback void IRAM_ATTR onTimer(){ - - Serial.println("onTimer::run"); + brun_scheduler = true; +} +void run_scheduler(){ + // Serial.println("onTimer::run"); // Get the current time via NTP, or downgrade if ( sntp_initialized ){ struct tm timeinfo; @@ -104,10 +108,11 @@ void IRAM_ATTR onTimer(){ current_hour = timeinfo.tm_hour; current_minute = timeinfo.tm_min; current_second = timeinfo.tm_sec; + // If no NTP clock }else{ - Serial.println("onTimer::NO NTP"); + // Serial.println("onTimer::NO NTP"); current_second += timer_interval_in_secs; if(current_second >= 60) { current_second = 0; @@ -178,18 +183,18 @@ void setup(){ preferences.putBytes(RELAY_2_SCHEDULE, scheduler_2_default, sizeof(scheduler_2_default)); /// ... more to come } - /* Huh... This crashes wokwi 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.end(); // WIFI Serial.println("Setup::WIFI"); Serial.printf("Connecting to %s ", ssid); WiFi.begin(ssid, password); - while (WiFi.status() != WL_CONNECTED) { + uint8_t wifi_loop_count = 0; + while (WiFi.status() != WL_CONNECTED or wifi_loop_count < wifi_loop_max ) { delay(500); + wifi_loop_count++; Serial.print("."); } Serial.print("Connect to IP Address: "); @@ -211,7 +216,7 @@ void setup(){ // SCHEDULER Serial.println("Setup::SCHEDULER"); - next_event_ts = get_timestamp( HOUR_DEFAULT, MINUTE_DEFAULT + EVENT_INC_MINUTE, 0 ); + next_event_ts = get_timestamp( HOUR_DEFAULT, MINUTE_DEFAULT, 10 ); // HTTP @@ -221,8 +226,11 @@ void setup(){ void loop(){ Serial.println("Loop::Enter"); + if( brun_scheduler == true ){ + brun_scheduler = false; + run_scheduler(); + } delay(1000); - -nc } +} diff --git a/preferences.ino b/sources/preferences.ino similarity index 100% rename from preferences.ino rename to sources/preferences.ino