Skip to content

Commit c4ba1ec

Browse files
committed
Increased WiFi robustness: Explicit connects/reconnects for proper
handling of sockets/connections.
1 parent 3376b6a commit c4ba1ec

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

src/Homie/Boot/BootNormal.cpp

+34-29
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ BootNormal::BootNormal()
2323
, _mqttTopicLevels(nullptr)
2424
, _mqttTopicLevelsCount(0) {
2525
//FIXME: getSketchMD5 not implemented / boot loop on ESP32
26-
//TODO: Remove Update.h from BootNormal.cpp, change status codes to https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/ota.html
26+
//Remove Update.h from BootNormal.cpp, change status codes to https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/ota.html
2727
strcpy(_fwChecksum, "00000000000000000000000000000000");
2828
_fwChecksum[sizeof(_fwChecksum) - 1] = '\0';
2929
}
@@ -48,7 +48,7 @@ BootNormal::BootNormal()
4848
, _mqttTopicLevels(nullptr)
4949
, _mqttTopicLevelsCount(0) {
5050
strlcpy(_fwChecksum, ESP.getSketchMD5().c_str(), sizeof(_fwChecksum));
51-
_fwChecksum[sizeof(_fwChecksum) - 1] = '\0';
51+
_fwChecksum[sizeof(_fwChecksum) - 1] = '\0';
5252
}
5353
#endif // ESP32
5454

@@ -182,9 +182,9 @@ void BootNormal::loop() {
182182

183183
if (_statsTimer.check()) {
184184
char statsIntervalStr[3 + 1];
185-
itoa(Interface::get().getConfig().get().deviceStatsInterval, statsIntervalStr, 10);
185+
itoa(Interface::get().getConfig().get().deviceStatsInterval+5, statsIntervalStr, 10);
186186
Interface::get().getLogger() << F("〽 Sending statistics...") << endl;
187-
Interface::get().getLogger() << F(" • Interval: ") << statsIntervalStr << F("s") << endl;
187+
Interface::get().getLogger() << F(" • Interval: ") << statsIntervalStr << F("s (") << Interface::get().getConfig().get().deviceStatsInterval << F("s including 5s grace time)") << endl;
188188
uint16_t intervalPacketId = Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$stats/interval")), 1, true, statsIntervalStr);
189189

190190
uint8_t quality = Helpers::rssiToPercentage(WiFi.RSSI());
@@ -328,8 +328,13 @@ void BootNormal::_wifiConnect() {
328328
WiFi.begin(Interface::get().getConfig().get().wifi.ssid, Interface::get().getConfig().get().wifi.password);
329329
}
330330

331+
#ifdef ESP32
332+
WiFi.setAutoConnect(false);
333+
WiFi.setAutoReconnect(false);
334+
#elif defined(ESP8266)
331335
WiFi.setAutoConnect(true);
332336
WiFi.setAutoReconnect(true);
337+
#endif // ESP32
333338
}
334339
}
335340

@@ -349,17 +354,17 @@ void BootNormal::_onWifiGotIp(WiFiEvent_t event, WiFiEventInfo_t info) {
349354
}
350355
#elif defined(ESP8266)
351356
void BootNormal::_onWifiGotIp(const WiFiEventStationModeGotIP& event) {
352-
if (Interface::get().led.enabled) Interface::get().getBlinker().stop();
353-
Interface::get().getLogger() << F("✔ Wi-Fi connected, IP: ") << event.ip << endl;
354-
Interface::get().getLogger() << F("Triggering WIFI_CONNECTED event...") << endl;
355-
Interface::get().event.type = HomieEventType::WIFI_CONNECTED;
356-
Interface::get().event.ip = event.ip;
357-
Interface::get().event.mask = event.mask;
358-
Interface::get().event.gateway = event.gw;
359-
Interface::get().eventHandler(Interface::get().event);
360-
MDNS.begin(Interface::get().getConfig().get().deviceId);
361-
362-
_mqttConnect();
357+
if (Interface::get().led.enabled) Interface::get().getBlinker().stop();
358+
Interface::get().getLogger() << F("✔ Wi-Fi connected, IP: ") << event.ip << endl;
359+
Interface::get().getLogger() << F("Triggering WIFI_CONNECTED event...") << endl;
360+
Interface::get().event.type = HomieEventType::WIFI_CONNECTED;
361+
Interface::get().event.ip = event.ip;
362+
Interface::get().event.mask = event.mask;
363+
Interface::get().event.gateway = event.gw;
364+
Interface::get().eventHandler(Interface::get().event);
365+
MDNS.begin(Interface::get().getConfig().get().deviceId);
366+
367+
_mqttConnect();
363368
}
364369
#endif // ESP32
365370

@@ -368,26 +373,26 @@ void BootNormal::_onWifiDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
368373
Interface::get().ready = false;
369374
if (Interface::get().led.enabled) Interface::get().getBlinker().start(LED_WIFI_DELAY);
370375
_statsTimer.reset();
371-
Interface::get().getLogger() << F("✖ Wi-Fi disconnected") << endl;
376+
Interface::get().getLogger() << F("✖ Wi-Fi disconnected, reason: ") << info.disconnected.reason << endl;
372377
Interface::get().getLogger() << F("Triggering WIFI_DISCONNECTED event...") << endl;
373378
Interface::get().event.type = HomieEventType::WIFI_DISCONNECTED;
374-
//Interface::get().event.wifiReason = info.disconnected.reason;
379+
Interface::get().event.wifiReason = info.disconnected.reason;
375380
Interface::get().eventHandler(Interface::get().event);
376381

377382
_wifiConnect();
378383
}
379384
#elif defined(ESP8266)
380385
void BootNormal::_onWifiDisconnected(const WiFiEventStationModeDisconnected& event) {
381-
Interface::get().ready = false;
382-
if (Interface::get().led.enabled) Interface::get().getBlinker().start(LED_WIFI_DELAY);
383-
_statsTimer.reset();
384-
Interface::get().getLogger() << F("✖ Wi-Fi disconnected") << endl;
385-
Interface::get().getLogger() << F("Triggering WIFI_DISCONNECTED event...") << endl;
386-
Interface::get().event.type = HomieEventType::WIFI_DISCONNECTED;
387-
Interface::get().event.wifiReason = event.reason;
388-
Interface::get().eventHandler(Interface::get().event);
389-
390-
_wifiConnect();
386+
Interface::get().ready = false;
387+
if (Interface::get().led.enabled) Interface::get().getBlinker().start(LED_WIFI_DELAY);
388+
_statsTimer.reset();
389+
Interface::get().getLogger() << F("✖ Wi-Fi disconnected, reason: ") << event.reason << endl;
390+
Interface::get().getLogger() << F("Triggering WIFI_DISCONNECTED event...") << endl;
391+
Interface::get().event.type = HomieEventType::WIFI_DISCONNECTED;
392+
Interface::get().event.wifiReason = event.reason;
393+
Interface::get().eventHandler(Interface::get().event);
394+
395+
_wifiConnect();
391396
}
392397
#endif // ESP32
393398

@@ -447,7 +452,7 @@ void BootNormal::_advertise() {
447452
break;
448453
case AdvertisementProgress::GlobalStep::PUB_STATS_INTERVAL:
449454
char statsIntervalStr[3 + 1];
450-
itoa(Interface::get().getConfig().get().deviceStatsInterval, statsIntervalStr, 10);
455+
itoa(Interface::get().getConfig().get().deviceStatsInterval+5, statsIntervalStr, 10);
451456
packetId = Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$stats/interval")), 1, true, statsIntervalStr);
452457
if (packetId != 0) _advertisementProgress.globalStep = AdvertisementProgress::GlobalStep::PUB_FW_NAME;
453458
break;
@@ -756,7 +761,7 @@ void BootNormal::_onMqttDisconnected(AsyncMqttClientDisconnectReason reason) {
756761
_advertisementProgress.currentPropertyIndex = 0;
757762
if (!_mqttDisconnectNotified) {
758763
_statsTimer.reset();
759-
Interface::get().getLogger() << F("✖ MQTT disconnected") << endl;
764+
Interface::get().getLogger() << F("✖ MQTT disconnected, reason: ") << (int8_t)reason<< endl;
760765
Interface::get().getLogger() << F("Triggering MQTT_DISCONNECTED event...") << endl;
761766
Interface::get().event.type = HomieEventType::MQTT_DISCONNECTED;
762767
Interface::get().event.mqttReason = reason;

0 commit comments

Comments
 (0)