diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 4c939b2..3b84cb7 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -74,10 +74,13 @@ void Span::poll() { if(!strlen(category)){ Serial.print("\n** FATAL ERROR: Cannot run homeSpan.poll() without an initial call to homeSpan.begin()!\n** PROGRAM HALTED **\n\n"); - while(1); - - } else if(WiFi.status()!=WL_CONNECTED){ - + while(1); + } + + if(!isInitialized){ + + Serial.print("\n"); + nvs_flash_init(); // initialize non-volatile-storage partition in flash HAPClient::init(); // read NVS and load HAP settings initWifi(); // initialize WiFi @@ -93,6 +96,12 @@ void Span::poll() { Serial.print(displayName); Serial.print(" is READY!\n\n"); + isInitialized=true; + } + + if(WiFi.status()!=WL_CONNECTED){ + Serial.print("*** LOST WIFI CONNECTION! ***\n\n"); + initWifi(); } char cBuf[17]="?"; diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 1d34fd3..d25d06a 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -52,6 +52,7 @@ struct Span{ char *modelName; // model name of this device - broadcast as Bonjour field "md" char category[3]=""; // category ID of primary accessory - broadcast as Bonjour field "ci" (HAP Section 13) unsigned long snapTime; // current time (in millis) snapped before entering Service loops() or updates() + boolean isInitialized=false; // flag indicating HomeSpan has been initialized char *defaultSetupCode=DEFAULT_SETUP_CODE; // Setup Code used for pairing uint8_t statusPin=DEFAULT_STATUS_PIN; // pin for status LED