Browse Source

fix ESP32 (also tested in hardware)

pull/487/head
lumapu 2 years ago
parent
commit
d11d3134f6
  1. 2
      src/defines.h
  2. 2
      src/publisher/pubMqtt.h
  3. 71
      src/wifi/ahoywifi.cpp
  4. 6
      src/wifi/ahoywifi.h

2
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 {

2
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"));

71
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
//-----------------------------------------------------------------------------

6
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;

Loading…
Cancel
Save