diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 914c385..a96e10f 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -122,11 +122,11 @@ void Span::poll() { HAPClient::init(); // read NVS and load HAP settings - if(strlen(network.wifiData.ssid)>0){ - initWifi(); - } else { - Serial.print("*** WIFI CREDENTIALS DATA NOT FOUND -- PLEASE CONFIGURE BY TYPING 'W ' OR PRESS CONTROL BUTTON FOR 3 SECONDS TO START ACCESS POINT.\n\n"); + if(!strlen(network.wifiData.ssid)){ + Serial.print("*** WIFI CREDENTIALS DATA NOT FOUND. YOU MAY CONFIGURE BY TYPING 'W '.\n\n"); statusLED.start(LED_WIFI_NEEDED); + } else { + homeSpan.statusLED.start(LED_WIFI_CONNECTING); } controlButton.reset(); @@ -137,8 +137,8 @@ void Span::poll() { } // isInitialized - if(strlen(network.wifiData.ssid)>0 && WiFi.status()!=WL_CONNECTED){ - initWifi(); + if(strlen(network.wifiData.ssid)>0){ + checkConnect(); } char cBuf[17]="?"; @@ -308,7 +308,54 @@ void Span::commandMode(){ ////////////////////////////////////// -void Span::initWifi(){ +void Span::checkConnect(){ + + if(connected){ + if(WiFi.status()==WL_CONNECTED) + return; + + Serial.print("\n\n*** WiFi Connection Lost!\n"); // losing and re-establishing connection has not been tested + connected=false; + waitTime=60000; + alarmConnect=0; + homeSpan.statusLED.start(LED_WIFI_CONNECTING); + } + + if(WiFi.status()!=WL_CONNECTED){ + if(millis()' to re-configure WiFi, or 'X ' to erase WiFi credentials. Will try connecting again in 60 seconds.\n\n"); + waitTime=60000; + } else { + Serial.print("Trying to connect to "); + Serial.print(network.wifiData.ssid); + Serial.print(". Waiting "); + Serial.print(waitTime/1000); + Serial.print(" second(s) for response...\n"); + WiFi.begin(network.wifiData.ssid,network.wifiData.pwd); + } + + alarmConnect=millis()+waitTime; + + return; + } + + connected=true; + + Serial.print("Successfully connected to "); + Serial.print(network.wifiData.ssid); + Serial.print("! IP Address: "); + Serial.print(WiFi.localIP()); + Serial.print("\n"); char id[18]; // create string version of Accessory ID for MDNS broadcast memcpy(id,HAPClient::accessory.ID,17); // copy ID bytes @@ -319,54 +366,6 @@ void Span::initWifi(){ int nChars=snprintf(NULL,0,"%s-%.2s%.2s%.2s%.2s%.2s%.2s",hostNameBase,id,id+3,id+6,id+9,id+12,id+15); char hostName[nChars+1]; sprintf(hostName,"%s-%.2s%.2s%.2s%.2s%.2s%.2s",hostNameBase,id,id+3,id+6,id+9,id+12,id+15); - - statusLED.start(LED_WIFI_CONNECTING); - controlButton.reset(); - - int nTries=0; - - Serial.print("Attempting connection to: "); - Serial.print(network.wifiData.ssid); - Serial.print(". Type 'X ' or press Control Button for 3 seconds at any time to terminate search and delete WiFi credentials."); - - while(WiFi.status()!=WL_CONNECTED){ - - if(nTries++ == 0) - Serial.print("\nConnecting.."); - - if(WiFi.begin(network.wifiData.ssid,network.wifiData.pwd)!=WL_CONNECTED){ - int delayTime; - char buf[8]=""; - if(nTries<=10){ - delayTime=2000; - Serial.print("."); - } else { - nTries=0; - delayTime=60000; - Serial.print(" Can't connect! Will re-try in "); - Serial.print(delayTime/1000); - Serial.print(" seconds..."); - } - long sTime=millis(); - - while(millis()-sTime" diff --git a/src/Network.h b/src/Network.h index 09ea642..44107f7 100644 --- a/src/Network.h +++ b/src/Network.h @@ -50,7 +50,6 @@ struct Network { int numSSID; WiFiClient client; // client used for HTTP calls - unsigned long timer; // length of time of trying to connect to WiFi unsigned long alarmTimeOut; // alarm time after which access point is shut down and HomeSpan is re-started int apStatus; // tracks access point status (0=timed-out, -1=cancel, 1=save) diff --git a/src/src.ino b/src/src.ino index 16a6ecb..9c1967b 100644 --- a/src/src.ino +++ b/src/src.ino @@ -10,7 +10,7 @@ void setup() { homeSpan.setLogLevel(2); - homeSpan.begin(); + homeSpan.begin(Category::Lighting,"HomeSpanTest"); new SpanAccessory(); // Begin by creating a new Accessory using SpanAccessory(), which takes no arguments