Improve Connect Callback

This commit is contained in:
fvanroie 2020-02-07 23:55:08 +01:00
parent 3e49cfc68e
commit 781c2b42ce

View File

@ -17,7 +17,7 @@
#else #else
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
static WiFiEventHandler wifiEventHandler[3]; static WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;
#endif #endif
#include "DNSserver.h" #include "DNSserver.h"
@ -60,15 +60,18 @@ String wifiGetMacAddress(int start, const char * seperator)
void wifiConnected(IPAddress ipaddress) void wifiConnected(IPAddress ipaddress)
{ {
bool isConnected = WiFi.status() == WL_CONNECTED;
char buffer[64]; char buffer[64];
sprintf_P(buffer, PSTR("WIFI: Received IP address %s"), ipaddress.toString().c_str()); sprintf_P(buffer, PSTR("WIFI: Received IP address %s"), ipaddress.toString().c_str());
debugPrintln(buffer); debugPrintln(buffer);
sprintf_P(buffer, PSTR("WIFI: Connected = %s"), WiFi.status() == WL_CONNECTED ? PSTR("yes") : PSTR("no")); sprintf_P(buffer, PSTR("WIFI: Connected = %s"), isConnected ? PSTR("yes") : PSTR("no"));
debugPrintln(buffer); debugPrintln(buffer);
httpReconnect(); if(isConnected) {
// mqttReconnect(); httpReconnect();
haspReconnect(); // mqttReconnect();
haspReconnect();
}
} }
void wifiDisconnected(const char * ssid, uint8_t reason) void wifiDisconnected(const char * ssid, uint8_t reason)
@ -157,9 +160,9 @@ void wifiSetup(JsonObject settings)
debugPrintln(buffer); debugPrintln(buffer);
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
wifiEventHandler[0] = WiFi.onStationModeGotIP(wifiSTAGotIP); // As soon WiFi is connected, start NTP Client // wifiEventHandler[0] = WiFi.onStationModeConnected(wifiSTAConnected);
wifiEventHandler[1] = WiFi.onStationModeDisconnected(wifiSTADisconnected); gotIpEventHandler = WiFi.onStationModeGotIP(wifiSTAGotIP); // As soon WiFi is connected, start NTP Client
wifiEventHandler[2] = WiFi.onStationModeConnected(wifiSTAConnected); disconnectedEventHandler = WiFi.onStationModeDisconnected(wifiSTADisconnected);
#endif #endif
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
WiFi.onEvent(wifi_callback); WiFi.onEvent(wifi_callback);