Add WiFi reconnection logic to companion radio WiFi interface

The WiFi companion firmware previously had no reconnection handling after
the initial WiFi.begin() call. On ESP32 Arduino core 2.x, the built-in
auto-reconnect is unreliable, so if WiFi drops it never comes back.

This adds a periodic health check (every 60s) in checkRecvFrame() that
detects WiFi disconnection and calls WiFi.reconnect(). The check only
activates after WiFi has successfully connected at least once, so it
won't interfere with the initial connection attempt on startup.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matt Swann 2026-04-12 20:23:42 -07:00
parent 748f9cfdda
commit ec2dd475eb
2 changed files with 13 additions and 0 deletions

View file

@ -53,6 +53,15 @@ void SerialWifiInterface::resetReceivedFrameHeader() {
}
size_t SerialWifiInterface::checkRecvFrame(uint8_t dest[]) {
// periodic WiFi health check (every 60s, only after first successful connection)
if (WiFi.status() == WL_CONNECTED) {
_wifi_was_connected = true;
} else if (_wifi_was_connected && millis() >= _wifi_check_time) {
WIFI_DEBUG_PRINTLN("WiFi disconnected, attempting reconnect...");
WiFi.reconnect();
_wifi_check_time = millis() + 60000;
}
// check if new client connected
auto newClient = server.available();
if (newClient) {

View file

@ -8,6 +8,8 @@ class SerialWifiInterface : public BaseSerialInterface {
bool _isEnabled;
unsigned long _last_write;
unsigned long adv_restart_time;
bool _wifi_was_connected;
unsigned long _wifi_check_time;
WiFiServer server;
WiFiClient client;
@ -39,6 +41,8 @@ public:
deviceConnected = false;
_isEnabled = false;
_last_write = 0;
_wifi_was_connected = false;
_wifi_check_time = 0;
send_queue_len = recv_queue_len = 0;
received_frame_header.type = 0;
received_frame_header.length = 0;