Created
January 14, 2018 18:56
-
-
Save lucabuka/4601e0fa137f7b6873c47d220f82b23f to your computer and use it in GitHub Desktop.
Test program to check WIFI CLOSE + RECONNECT on DOIT ESP32 DEVKIT V1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file esp32_reconnect_demo.ino | |
* @brief Test program to check WIFI CLOSE + RECONNECT on DOIT ESP32 DEVKIT V1 | |
* | |
* The program connect to the WiFI network and than to a NTP server | |
* It prints in the loop "getEpochTime()" response from NTP server. | |
* If the WiFI connection is lost, it reconnects. | |
* After printing 15 times "getEpochTime()", it DESCONNECT WiFi | |
* to test reconnect capabilities. | |
* | |
* WHY ? | |
* ----- | |
* | |
* There are issues with ESP32 closed source libs on WiFI reconnect() - see: | |
* https://github.com/espressif/arduino-esp32/issues/653 | |
* | |
* The solution proposed above is to call "WiFi.disconnect(true)" to close | |
* the WiFI connection and than call WiFI.begin() again. | |
* Unfortunately the call to "WiFi.disconnect(true)" PUT THE DOIT DEVKIT V1 | |
* board in a...weird state: | |
* Any subsequent connect will return an AUTH_FAIL | |
* This error survives to a power off cycle and the only way to | |
* fix it is to "reset" the board with the "esptool.py" util: | |
* python /home/user/Arduino/hardware/espressif/esp32/tools/esptool.py \ | |
* --chip esp32 --port /dev/ttyUSB0 --baud 921600 \ | |
* --before default_reset --after hard_reset erase_flash | |
* | |
* The solution proposed in the follwing post works: | |
* https://github.com/espressif/esp-idf/issues/1366 | |
* | |
* WiFI off: | |
* --------- | |
* WiFi.mode(WIFI_OFF); | |
* | |
* WiFI on: | |
* -------- | |
* WiFi.begin(ssid, password); | |
* WiFi.mode(WIFI_STA); | |
* WiFi.reconnect(); | |
* delay(1000); | |
* while (WiFi.status() != WL_CONNECTED) { | |
* | |
* | |
* Situation at 2017-01-11 | |
* ----------------------- | |
* DOIT ESP32 DEVKIT V1: | |
* - Reconnect WORKS OK after the prg calls WiFi.mode(WIFI_OFF) | |
* - Reconnect WORKS OK after a router failure (after turning the | |
* router off and back on, the board reconnects ok) | |
* | |
*/ | |
#include <Arduino.h> | |
#include <WiFiUdp.h> | |
#include <NTPClient.h> | |
#define WIFI_SSID "your_net_ssid" | |
#define WIFI_PASSWD "your_pass" | |
#include <WiFi.h> | |
WiFiUDP ntpUDP; | |
NTPClient timeClient(ntpUDP, "pool.ntp.org", 0, 60000); | |
void setup() { | |
esp_log_level_set("*", ESP_LOG_VERBOSE); | |
Serial.begin(115200); | |
WiFi.setAutoReconnect(false); | |
WiFi.begin(WIFI_SSID, WIFI_PASSWD); | |
WiFi.waitForConnectResult(); | |
timeClient.begin(); | |
delay(3000); | |
Serial.println("Setup completed"); | |
} | |
void loop() { | |
static int loopcnt = 0; | |
bool wifi_ok; | |
wifi_ok = wifiIsActive(); // Check wifi status | |
if( wifi_ok ) { | |
timeClient.forceUpdate(); | |
printf("[%d] ssid=%s\n", timeClient.getEpochTime(), WiFi.SSID().c_str()); | |
loopcnt ++; | |
} | |
// After 15 loops DISCONNECT WIFI | |
if(wifi_ok && loopcnt >= 15){ | |
loopcnt = 0; | |
Serial.println("Closing Connection after 15 times (wifi off)..."); | |
WiFi.mode(WIFI_OFF); | |
delay(3000); | |
Serial.print("WiFI Status: "); | |
Serial.println(WiFi.status()); | |
} | |
delay(3000); | |
} | |
/** | |
* @brief Test WiFI connection and, if not connected, try to reconnect | |
* | |
* The reconnect() steps are: | |
* WiFi.mode(WIFI_OFF); | |
* delay(2000); | |
* WiFi.begin(WIFI_SSID, WIFI_PASSWD); | |
* WiFi.mode(WIFI_STA); | |
* WiFi.reconnect(); | |
* delay(1000); | |
* | |
* BUT the "delay()" is implemented with a simple state machine | |
* to have a non-blocking reconnect function | |
* | |
* This function have to be called in a loop(). | |
* Example: | |
* loop() { | |
* if( wifiIsActive() { | |
* // WIFI connected: do something | |
* } else { | |
* // WIFI NOT connected: print warning | |
* } | |
* | |
* // do other stuff | |
* | |
* } | |
* | |
* @param void | |
* | |
* @return 0,1 | |
* @return 0 = NO, WiFI is NOT Active | |
* @return 1 = YES, WiFI is Active | |
* | |
*/ | |
bool wifiIsActive(void){ | |
static char internal_status = 0; | |
static unsigned long last_status_change = 0; | |
if (! WiFi.isConnected()) { | |
Serial.println("Attempting to reconnect..."); | |
switch(internal_status){ | |
case 0: | |
Serial.println("SET MODE -> WIFI_OFF..."); | |
WiFi.mode(WIFI_OFF); | |
last_status_change = millis(); | |
internal_status = 1; // Next state | |
break; | |
case 1: | |
if(millis() - last_status_change > 2000) { | |
Serial.println("BEGIN() + SET MODE -> WIFI_STA + RECONNECT()..."); | |
WiFi.begin(WIFI_SSID, WIFI_PASSWD); | |
WiFi.mode(WIFI_STA); | |
WiFi.reconnect(); | |
last_status_change = millis(); | |
internal_status = 2; // Next state | |
} | |
break; | |
case 2: | |
if(millis() - last_status_change > 1000) { | |
Serial.printf("CONNECTION RESULT: [%d]\n", WiFi.waitForConnectResult()); | |
internal_status = 3; | |
} | |
break; | |
case 3: | |
internal_status = 0; // get ready to start over | |
if(! WiFi.isConnected()){ | |
return(0); // NOT Active | |
} | |
return(1); // Active | |
break; | |
default: | |
Serial.println("SOFTWARE ERROR - wifiIsActive() switch unreachable line = default"); | |
break; | |
} | |
return(0); // NOT Active | |
} else { | |
return(1); // Active | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment