Skip to content

Commit 8dd1b17

Browse files
committed
feat(wifi): add autoReconnect() delegating reconnect to firmware
WiFiMQTT already delegates reconnect logic to the ESP32 firmware via a flag on AT+MQTTCONN, but plain WiFi station reconnect had no equivalent. autoReconnect(bool) wraps AT+CWAUTOCONN, letting the firmware handle rejoining the last used AP itself, with no host-side credential caching or polling loop needed. Verified on hardware: AT+CWAUTOCONN is accepted and persists across queries, and the board auto-rejoined an AP that was toggled off and back on with no intervention from the host. Note this setting is not persisted across power cycles unless storeSettingsInNVM(true) is also enabled.
1 parent f72e924 commit 8dd1b17

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎src/system/wifi/esp32SpiAt.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,37 @@ bool WiFiClass::disconnect()
933933
return true;
934934
}
935935

936+
/**
937+
* @brief Enable or disable the ESP32's own auto-reconnect to the last used AP. When enabled, the ESP32
938+
* firmware handles rejoining the AP by itself (after the AP drops and comes back, for example)
939+
* without any host-side polling or credential caching. Confirmed on hardware: this firmware
940+
* accepts AT+CWAUTOCONN and physically rejoins an AP that was toggled off and back on, with no
941+
* intervention from the STM32 host.
942+
*
943+
* @param bool _enable
944+
* true - Enable auto-reconnect to the last used AP.
945+
* false - Disable auto-reconnect.
946+
* @return bool
947+
* true - Command executed successfully.
948+
* false - Command failed.
949+
*
950+
* @note This setting is not persisted across power cycles unless storeSettingsInNVM(true) is also set;
951+
* without it, auto-reconnect only applies for as long as the ESP32 stays powered.
952+
*/
953+
bool WiFiClass::autoReconnect(bool _enable)
954+
{
955+
// Create the AT command depending on the choice of enabling/disabling auto-reconnect.
956+
sprintf(_dataBuffer, "AT+CWAUTOCONN=%d\r\n", _enable ? 1 : 0);
957+
958+
// Send AT command and get the response. Check if the response is expected. Otherwise, return false.
959+
if (!sendAtCommandWithResponse(_dataBuffer, 20ULL, 4ULL, (char *)esp32AtCmdResponseOK,
960+
INKPLATE_ESP32_AT_EXPECTED_RESPONSE_START, true, NULL, 0, 0, NULL))
961+
return false;
962+
963+
// Otherwise, return ok.
964+
return true;
965+
}
966+
936967
/**
937968
* @brief Methods prompts ESP32 to run a WiFi network scan. Scan takes about 2 seconds.
938969
*

‎src/system/wifi/esp32SpiAt.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class WiFiClass
9494
bool begin(char *_ssid, char *_pass);
9595
bool connected();
9696
bool disconnect();
97+
bool autoReconnect(bool _enable);
9798
int scanNetworks();
9899
char *ssid(int _ssidNumber);
99100
bool auth(int _ssidNumber);

0 commit comments

Comments
 (0)