mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-24 22:36:10 +02:00
fix ESP32 (also tested in hardware)
This commit is contained in:
parent
7c6ab6792b
commit
d11d3134f6
4 changed files with 60 additions and 19 deletions
|
@ -13,7 +13,7 @@
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
#define VERSION_MAJOR 0
|
#define VERSION_MAJOR 0
|
||||||
#define VERSION_MINOR 5
|
#define VERSION_MINOR 5
|
||||||
#define VERSION_PATCH 46
|
#define VERSION_PATCH 47
|
||||||
|
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
|
@ -227,7 +227,7 @@ class PubMqtt {
|
||||||
}
|
}
|
||||||
|
|
||||||
void onDisconnect(espMqttClientTypes::DisconnectReason reason) {
|
void onDisconnect(espMqttClientTypes::DisconnectReason reason) {
|
||||||
DBGPRINT(F("MQTT disconnected, reason: "));
|
DPRINT(DBG_INFO, F("MQTT disconnected, reason: "));
|
||||||
switch (reason) {
|
switch (reason) {
|
||||||
case espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED:
|
case espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED:
|
||||||
DBGPRINTLN(F("TCP disconnect"));
|
DBGPRINTLN(F("TCP disconnect"));
|
||||||
|
|
|
@ -38,8 +38,12 @@ void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp) {
|
||||||
ah::ip2Char(mConfig->sys.ip.ip, ipSta);
|
ah::ip2Char(mConfig->sys.ip.ip, ipSta);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(ESP8266)
|
||||||
wifiConnectHandler = WiFi.onStationModeGotIP(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1));
|
wifiConnectHandler = WiFi.onStationModeGotIP(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1));
|
||||||
wifiDisconnectHandler = WiFi.onStationModeDisconnected(std::bind(&ahoywifi::onDisconnect, 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;
|
*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 defined(ESP8266)
|
||||||
if(!mConnected) {
|
void ahoywifi::onConnect(const WiFiEventStationModeGotIP& event) {
|
||||||
mConnected = true;
|
if(!mConnected) {
|
||||||
DBGPRINTLN(F("\n[WiFi] Connected"));
|
mConnected = true;
|
||||||
WiFi.mode(WIFI_STA);
|
DBGPRINTLN(F("\n[WiFi] Connected"));
|
||||||
WiFi.begin();
|
WiFi.mode(WIFI_STA);
|
||||||
DBGPRINTLN(F("[WiFi] AP disabled"));
|
DBGPRINTLN(F("[WiFi] AP disabled"));
|
||||||
mDns.stop();
|
mDns.stop();
|
||||||
|
|
||||||
welcome(WiFi.localIP().toString() + F(" (Station)"));
|
welcome(WiFi.localIP().toString() + F(" (Station)"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
void ahoywifi::onDisconnect(const WiFiEventStationModeDisconnected& event) {
|
void ahoywifi::onDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||||
if(mConnected) {
|
if(mConnected) {
|
||||||
mConnected = false;
|
mConnected = false;
|
||||||
DPRINTLN(DBG_INFO, "[WiFi] Connection Lost");
|
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
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
|
@ -30,16 +30,22 @@ class ahoywifi {
|
||||||
void setupAp(void);
|
void setupAp(void);
|
||||||
void setupStation(void);
|
void setupStation(void);
|
||||||
void sendNTPpacket(IPAddress& address);
|
void sendNTPpacket(IPAddress& address);
|
||||||
|
#if defined(ESP8266)
|
||||||
void onConnect(const WiFiEventStationModeGotIP& event);
|
void onConnect(const WiFiEventStationModeGotIP& event);
|
||||||
void onDisconnect(const WiFiEventStationModeDisconnected& event);
|
void onDisconnect(const WiFiEventStationModeDisconnected& event);
|
||||||
|
#else
|
||||||
|
void onWiFiEvent(WiFiEvent_t event);
|
||||||
|
#endif
|
||||||
void welcome(String msg);
|
void welcome(String msg);
|
||||||
|
|
||||||
settings_t *mConfig;
|
settings_t *mConfig;
|
||||||
|
|
||||||
DNSServer mDns;
|
DNSServer mDns;
|
||||||
WiFiUDP mUdp; // for time server
|
WiFiUDP mUdp; // for time server
|
||||||
|
#if defined(ESP8266)
|
||||||
WiFiEventHandler wifiConnectHandler;
|
WiFiEventHandler wifiConnectHandler;
|
||||||
WiFiEventHandler wifiDisconnectHandler;
|
WiFiEventHandler wifiDisconnectHandler;
|
||||||
|
#endif
|
||||||
|
|
||||||
bool mConnected, mInitNtp;
|
bool mConnected, mInitNtp;
|
||||||
uint8_t mCnt;
|
uint8_t mCnt;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue