Skip to content

Commit f332c0c

Browse files
committed
Add wifiConnect useAPSTAMode and wasInAPmode
1 parent 6c24829 commit f332c0c

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

Firmware/RTK_Everywhere/WiFi.ino

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,16 +486,40 @@ bool wifiIsConnected()
486486
// Attempts a connection to all provided SSIDs
487487
// Returns true if successful
488488
// Gives up if no SSID detected or connection times out
489+
// If useAPSTAMode is true, do an extra check and go from WIFI_AP mode to WIFI_AP_STA mode
489490
bool wifiConnect(unsigned long timeout)
491+
{
492+
return wifiConnect(timeout, false, nullptr);
493+
}
494+
bool wifiConnect(unsigned long timeout, bool useAPSTAMode, bool *wasInAPmode)
490495
{
491496
if (wifiIsConnected())
492497
return (true); // Nothing to do
493498

494499
displayWiFiConnect();
495500

496-
// Before we can issue esp_wifi_() commands WiFi must be started
497-
if (WiFi.getMode() != WIFI_STA)
498-
WiFi.mode(WIFI_STA);
501+
// If otaUpdate or otaCheckVersion wants to use WIFI_AP_STA mode
502+
if (useAPSTAMode && (wasInAPmode != nullptr))
503+
{
504+
*wasInAPmode = (WiFi.getMode() == WIFI_AP);
505+
506+
if (*wasInAPmode)
507+
{
508+
systemPrintln("wifiConnect: changing from WIFI_AP to WIFI_AP_STA");
509+
WiFi.mode(WIFI_AP_STA); // Change mode from WIFI_AP to WIFI_AP_STA
510+
}
511+
else
512+
{
513+
systemPrintln("wifiConnect: was not in WIFI_AP mode. Going to WIFI_STA");
514+
WiFi.mode(WIFI_STA); // Must have been off - or already in STA mode?
515+
}
516+
}
517+
else
518+
{
519+
// Before we can issue esp_wifi_() commands WiFi must be started
520+
if (WiFi.getMode() != WIFI_STA)
521+
WiFi.mode(WIFI_STA);
522+
}
499523

500524
// Verify that the necessary protocols are set
501525
uint8_t protocols = 0;

Firmware/RTK_Everywhere/menuCommands.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ bool updateSettingWithValue(const char *settingName, const char *settingValueStr
757757
sendStringToWebsocket((char *)"gettingNewFirmware,1,");
758758

759759
apConfigFirmwareUpdateInProcess = true;
760-
otaUpdate();
760+
otaUpdate(); // otaUpdate will call wifiConnect if needed. Also does previouslyConnected check
761761

762762
// We get here if WiFi failed to connect
763763
sendStringToWebsocket((char *)"gettingNewFirmware,ERROR,");

Firmware/RTK_Everywhere/menuFirmware.ino

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,9 @@ void menuFirmware()
160160

161161
else if ((incoming == 'u') && newOTAFirmwareAvailable)
162162
{
163-
bool previouslyConnected = wifiIsConnected();
164-
165-
otaUpdate();
163+
otaUpdate(); // otaUpdate will call wifiConnect if needed. Also does previouslyConnected check
166164

167165
// We get here if WiFi failed or the server was not available
168-
169-
if (previouslyConnected == false)
170-
WIFI_STOP();
171166
}
172167

173168
else if (incoming == 'x')
@@ -476,7 +471,9 @@ bool otaCheckVersion(char *versionAvailable, uint8_t versionAvailableLength)
476471
#ifdef COMPILE_WIFI
477472
bool previouslyConnected = wifiIsConnected();
478473

479-
if (wifiConnect(10000) == true)
474+
bool wasInAPmode;
475+
476+
if (wifiConnect(10000, true, &wasInAPmode) == true) // Use WIFI_AP_STA if already in WIFI_AP mode
480477
{
481478
char versionString[21];
482479
getFirmwareVersion(versionString, sizeof(versionString), enableRCFirmware);
@@ -512,6 +509,11 @@ bool otaCheckVersion(char *versionAvailable, uint8_t versionAvailableLength)
512509
systemPrintln("WiFi not available.");
513510
}
514511

512+
// If we were in WIFI_AP mode, return to WIFI_AP mode
513+
// There may be some overlap with systemState STATE_WIFI_CONFIG ? Not sure...
514+
if (wasInAPmode)
515+
WiFi.mode(WIFI_AP);
516+
515517
if (systemState != STATE_WIFI_CONFIG)
516518
{
517519
// WIFI_STOP() turns off the entire radio including the webserver. We need to turn off Station mode only.
@@ -573,9 +575,15 @@ void otaUpdate()
573575
#ifdef COMPILE_WIFI
574576
bool previouslyConnected = wifiIsConnected();
575577

576-
if (wifiConnect(10000) == true)
578+
bool wasInAPmode;
579+
580+
if (wifiConnect(10000, true, &wasInAPmode) == true) // Use WIFI_AP_STA if already in WIFI_AP mode
577581
overTheAirUpdate();
578582

583+
// Update failed. If we were in WIFI_AP mode, return to WIFI_AP mode
584+
if (wasInAPmode)
585+
WiFi.mode(WIFI_AP);
586+
579587
// Update failed. If WiFi was originally off, turn it off again
580588
if (previouslyConnected == false)
581589
WIFI_STOP();

0 commit comments

Comments
 (0)