ESP8266 Intelligent Distribution Network Reconnect after Power Off
#includebool autoConfig( ){ WiFi.begin(); for (int i = 0; i <20; i++) {int wstatus = WiFi.status(); if (wstatus == WL_CONNECTED) {Serial.println("AutoConfig Success"); Serial .printf("SSID:%s ", WiFi.SSID().c_str()); Serial.printf("PSW:%s ", WiFi.psk().c_str()) ; WiFi.printDiag(Serial); return true; //break;} else {Serial.print("AutoConfig Waiting......"); Serial.println(wstatus); delay(1000);}} Serial. println("AutoConfig Faild!" ); return false; //WiFi.printDiag(Serial);}void smartConfig(){ WiFi.mode(WIFI_STA); Serial.println(" Wait for Smartconfig"); WiFi. beginSmartConfig(); while (1) {Serial.print("."); if (WiFi.smartConfigDone()) {Serial.println("SmartConfig Success"); Serial.printf("SSID:%s ", WiFi.SSID().c_str()); Serial.printf("PSW:%s ", WiFi.psk().c_str()); WiFi.setAuto Connect(true); // Set up automatic connection break;} delay(1000); // This place must be delayed, otherwise it will easily crash and restart}}void setup(){ Serial.begin(115200); if (! autoConfig()) {Serial.println("Start module"); smartConfig(); }}void loop(){ delay(1000); Serial.println("loop");}
Reference link