From d11d3134f69de240d3f1e6f63c657dc69d973480 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 3 Dec 2022 02:44:25 +0100 Subject: [PATCH] fix ESP32 (also tested in hardware) --- src/defines.h | 2 +- src/publisher/pubMqtt.h | 2 +- src/wifi/ahoywifi.cpp | 71 ++++++++++++++++++++++++++++++----------- src/wifi/ahoywifi.h | 6 ++++ 4 files changed, 61 insertions(+), 20 deletions(-) diff --git a/src/defines.h b/src/defines.h index 09665137..229d583b 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 5 -#define VERSION_PATCH 46 +#define VERSION_PATCH 47 //------------------------------------- typedef struct { diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index 43ef277a..0e3e8417 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -227,7 +227,7 @@ class PubMqtt { } void onDisconnect(espMqttClientTypes::DisconnectReason reason) { - DBGPRINT(F("MQTT disconnected, reason: ")); + DPRINT(DBG_INFO, F("MQTT disconnected, reason: ")); switch (reason) { case espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED: DBGPRINTLN(F("TCP disconnect")); diff --git a/src/wifi/ahoywifi.cpp b/src/wifi/ahoywifi.cpp index b6fbce73..82370031 100644 --- a/src/wifi/ahoywifi.cpp +++ b/src/wifi/ahoywifi.cpp @@ -38,8 +38,12 @@ void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp) { ah::ip2Char(mConfig->sys.ip.ip, ipSta); #endif + #if defined(ESP8266) wifiConnectHandler = WiFi.onStationModeGotIP(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1)); wifiDisconnectHandler = WiFi.onStationModeDisconnected(std::bind(&ahoywifi::onDisconnect, this, std::placeholders::_1)); + #else + WiFi.onEvent(std::bind(&ahoywifi::onWiFiEvent, this, std::placeholders::_1)); + #endif } @@ -146,7 +150,7 @@ void ahoywifi::getNtpTime(void) { *mUtcTimestamp = date; - DPRINTLN(DBG_INFO, F("[NTP]: ") + ah::getDateTimeStr(*mUtcTimestamp) + F(" UTC")); + DPRINTLN(DBG_INFO, "[NTP]: " + ah::getDateTimeStr(*mUtcTimestamp) + " UTC"); } @@ -202,27 +206,58 @@ void ahoywifi::sendNTPpacket(IPAddress& address) { //----------------------------------------------------------------------------- -void ahoywifi::onConnect(const WiFiEventStationModeGotIP& event) { - if(!mConnected) { - mConnected = true; - DBGPRINTLN(F("\n[WiFi] Connected")); - WiFi.mode(WIFI_STA); - WiFi.begin(); - DBGPRINTLN(F("[WiFi] AP disabled")); - mDns.stop(); - - welcome(WiFi.localIP().toString() + F(" (Station)")); +#if defined(ESP8266) + void ahoywifi::onConnect(const WiFiEventStationModeGotIP& event) { + if(!mConnected) { + mConnected = true; + DBGPRINTLN(F("\n[WiFi] Connected")); + WiFi.mode(WIFI_STA); + DBGPRINTLN(F("[WiFi] AP disabled")); + mDns.stop(); + + welcome(WiFi.localIP().toString() + F(" (Station)")); + } } -} -//----------------------------------------------------------------------------- -void ahoywifi::onDisconnect(const WiFiEventStationModeDisconnected& event) { - if(mConnected) { - mConnected = false; - DPRINTLN(DBG_INFO, "[WiFi] Connection Lost"); + //------------------------------------------------------------------------- + void ahoywifi::onDisconnect(const WiFiEventStationModeDisconnected& event) { + if(mConnected) { + mConnected = false; + DPRINTLN(DBG_INFO, "[WiFi] Connection Lost"); + } } -} + +#else + //------------------------------------------------------------------------- + void ahoywifi::onWiFiEvent(WiFiEvent_t event) { + switch(event) { + case SYSTEM_EVENT_STA_GOT_IP: + if(!mConnected) { + delay(1000); + mConnected = true; + DBGPRINTLN(F("\n[WiFi] Connected")); + welcome(WiFi.localIP().toString() + F(" (Station)")); + WiFi.mode(WIFI_STA); + WiFi.begin(); + DBGPRINTLN(F("[WiFi] AP disabled")); + mDns.stop(); + + } + break; + + case SYSTEM_EVENT_STA_DISCONNECTED: + if(mConnected) { + mConnected = false; + DPRINTLN(DBG_INFO, "[WiFi] Connection Lost"); + } + break; + + default: + break; + } + } +#endif //----------------------------------------------------------------------------- diff --git a/src/wifi/ahoywifi.h b/src/wifi/ahoywifi.h index 1cda6ab4..767265c5 100644 --- a/src/wifi/ahoywifi.h +++ b/src/wifi/ahoywifi.h @@ -30,16 +30,22 @@ class ahoywifi { void setupAp(void); void setupStation(void); void sendNTPpacket(IPAddress& address); + #if defined(ESP8266) void onConnect(const WiFiEventStationModeGotIP& event); void onDisconnect(const WiFiEventStationModeDisconnected& event); + #else + void onWiFiEvent(WiFiEvent_t event); + #endif void welcome(String msg); settings_t *mConfig; DNSServer mDns; WiFiUDP mUdp; // for time server + #if defined(ESP8266) WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiDisconnectHandler; + #endif bool mConnected, mInitNtp; uint8_t mCnt;