Skip to content

Commit

Permalink
Merge pull request #1333 from TD-er/bugfix/displayNamesInLog
Browse files Browse the repository at this point in the history
[wifi] Do not rely on WiFi.status() and better log of status
  • Loading branch information
TD-er authored Apr 29, 2018
2 parents 4ac330e + 9084a55 commit 45afaf8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
7 changes: 4 additions & 3 deletions src/ESPEasy-Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ enum Command {
// Forward declarations.
Command commandStringToEnum(const char * cmd);
bool WiFiConnected(uint32_t timeout_ms);
bool WiFiConnected();
bool hostReachable(const IPAddress& ip);
bool hostReachable(const String& hostname);
void formatMAC(const uint8_t* mac, char (&strMAC)[20]);
Expand Down Expand Up @@ -825,7 +826,7 @@ struct ControllerSettingsStruct
if (!UseDNS) {
return true;
}
if (WiFi.status() != WL_CONNECTED) return false;
if (!WiFiConnected()) return false;
IPAddress tmpIP;
if (WiFi.hostByName(HostName, tmpIP)) {
for (byte x = 0; x < 4; x++) {
Expand Down Expand Up @@ -1205,11 +1206,11 @@ Ticker connectionCheck;
bool reconnectChecker = false;
void connectionCheckHandler()
{
if (reconnectChecker == false && WiFi.status() != WL_CONNECTED){
if (reconnectChecker == false && !WiFiConnected()){
reconnectChecker = true;
WiFi.reconnect();
}
else if (WiFi.status() == WL_CONNECTED && reconnectChecker == true){
else if (WiFiConnected() && reconnectChecker == true){
reconnectChecker = false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ESPEasy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ void setup()
#endif

#ifndef ESP32
connectionCheck.attach(240, connectionCheckHandler);
connectionCheck.attach(30, connectionCheckHandler);
#endif
}

Expand Down Expand Up @@ -395,7 +395,7 @@ void loop()
if (wifiStatus >= ESPEASY_WIFI_CONNECTED) processConnect();
if (wifiStatus >= ESPEASY_WIFI_GOT_IP) processGotIP();
if (wifiStatus == ESPEASY_WIFI_DISCONNECTED) processDisconnect();
} else if (WiFi.status() != WL_CONNECTED) {
} else if (!WiFiConnected()) {
// Somehow the WiFi has entered a limbo state.
// FIXME TD-er: This may happen on WiFi config with AP_STA mode active.
// addLog(LOG_LEVEL_ERROR, F("Wifi status out sync"));
Expand Down
64 changes: 49 additions & 15 deletions src/ESPEasyWifi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,17 @@ bool useStaticIP() {
return (Settings.IP[0] != 0 && Settings.IP[0] != 255);
}

bool WiFiConnected() {
#ifdef ESP32
return WiFi.status() == WL_CONNECTED;
#else
// For ESP82xx, do not rely on WiFi.status() with event based wifi.
return wifi_station_get_connect_status() == STATION_GOT_IP;
#endif
}

void WiFiConnectRelaxed() {
if (WiFi.status() == WL_CONNECTED)
if (WiFiConnected())
return; //already connected, need to disconnect first
if (prepareWiFi()) {
if (selectValidWiFiSettings()) {
Expand Down Expand Up @@ -666,20 +674,22 @@ String formatScanResult(int i, const String& separator) {
return result;
}

void logConnectionStatus() {
const uint8_t arduino_corelib_wifistatus = WiFi.status();
String log;
#ifndef ESP32
const uint8_t sdk_wifistatus = wifi_station_get_connect_status();
if (arduino_corelib_wifistatus != sdk_wifistatus) {
log = F("WIFI : SDK station status differs from Arduino status. SDK-status: ");
log += sdk_wifistatus;
log += F(" Arduino status: ");
log += arduino_corelib_wifistatus;
addLog(LOG_LEVEL_ERROR, log);
#ifndef ESP32
String SDKwifiStatusToString(uint8_t sdk_wifistatus) {
switch (sdk_wifistatus) {
case STATION_IDLE: return F("STATION_IDLE");
case STATION_CONNECTING: return F("STATION_CONNECTING");
case STATION_WRONG_PASSWORD: return F("STATION_WRONG_PASSWORD");
case STATION_NO_AP_FOUND: return F("STATION_NO_AP_FOUND");
case STATION_CONNECT_FAIL: return F("STATION_CONNECT_FAIL");
case STATION_GOT_IP: return F("STATION_GOT_IP");
}
#endif
log = F("WIFI : Arduino wifi status: ");
return F("Unknown");
}
#endif

String ArduinoWifiStatusToString(uint8_t arduino_corelib_wifistatus) {
String log;
switch (arduino_corelib_wifistatus) {
case WL_IDLE_STATUS: log += F("WL_IDLE_STATUS"); break;
case WL_NO_SSID_AVAIL: log += F("WL_NO_SSID_AVAIL"); break;
Expand All @@ -690,14 +700,38 @@ void logConnectionStatus() {
case WL_DISCONNECTED: log += F("WL_DISCONNECTED"); break;
default: log += arduino_corelib_wifistatus; break;
}
log += F(" ESPeasy internal wifi status: ");
return log;
}

String ESPeasyWifiStatusToString() {
String log;
switch (wifiStatus) {
case ESPEASY_WIFI_DISCONNECTED: log += F("ESPEASY_WIFI_DISCONNECTED"); break;
case ESPEASY_WIFI_CONNECTED: log += F("ESPEASY_WIFI_CONNECTED"); break;
case ESPEASY_WIFI_GOT_IP: log += F("ESPEASY_WIFI_GOT_IP"); break;
case ESPEASY_WIFI_SERVICES_INITIALIZED: log += F("ESPEASY_WIFI_SERVICES_INITIALIZED"); break;
default: log += wifiStatus;
}
return log;
}

void logConnectionStatus() {
const uint8_t arduino_corelib_wifistatus = WiFi.status();
String log;
#ifndef ESP32
const uint8_t sdk_wifistatus = wifi_station_get_connect_status();
if ((arduino_corelib_wifistatus == WL_CONNECTED) != (sdk_wifistatus == STATION_GOT_IP)) {
log = F("WIFI : SDK station status differs from Arduino status. SDK-status: ");
log += SDKwifiStatusToString(sdk_wifistatus);
log += F(" Arduino status: ");
log += ArduinoWifiStatusToString(arduino_corelib_wifistatus);
addLog(LOG_LEVEL_ERROR, log);
}
#endif
log = F("WIFI : Arduino wifi status: ");
log += ArduinoWifiStatusToString(arduino_corelib_wifistatus);
log += F(" ESPeasy internal wifi status: ");
log += ESPeasyWifiStatusToString();
addLog(LOG_LEVEL_DEBUG_MORE, log);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Networking.ino
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ bool WiFiConnected(uint32_t timeout_ms) {
// Apparently something needs network, perform check to see if it is ready now.
// if (!tryConnectWiFi())
// return false;
while (WiFi.status() != WL_CONNECTED) {
while (!WiFiConnected()) {
if (timeOutReached(timer)) {
return false;
}
Expand All @@ -617,7 +617,7 @@ bool WiFiConnected(uint32_t timeout_ms) {
}

bool hostReachable(const IPAddress& ip) {
if (WiFi.status() != WL_CONNECTED) return false;
if (!WiFiConnected()) return false;
// Only do 1 ping at a time to return early
byte retry = 3;
while (retry > 0) {
Expand All @@ -643,7 +643,7 @@ bool hostReachable(const IPAddress& ip) {
}

bool hostReachable(const String& hostname) {
if (WiFi.status() != WL_CONNECTED) return false;
if (!WiFiConnected()) return false;
IPAddress remote_addr;
if (WiFi.hostByName(hostname.c_str(), remote_addr)) {
return hostReachable(remote_addr);
Expand Down

0 comments on commit 45afaf8

Please sign in to comment.