Skip to content

Commit b08bb9c

Browse files
authored
Merge pull request #350 from LeeLeahy2/auto-firmware-update
Add OTA support for Ethernet
2 parents 610944a + 0283e3c commit b08bb9c

File tree

2 files changed

+69
-56
lines changed

2 files changed

+69
-56
lines changed

Firmware/RTK_Everywhere/menuFirmware.ino

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ menuFirmware.ino
1111
#ifdef COMPILE_OTA_AUTO
1212

1313
static const char *const otaStateNames[] = {"OTA_STATE_OFF",
14-
"OTA_STATE_START_WIFI",
15-
"OTA_STATE_WAIT_FOR_WIFI",
14+
"OTA_STATE_START_NETWORK",
15+
"OTA_STATE_WAIT_FOR_NETWORK",
1616
"OTA_STATE_GET_FIRMWARE_VERSION",
1717
"OTA_STATE_CHECK_FIRMWARE_VERSION",
1818
"OTA_STATE_UPDATE_FIRMWARE"};
@@ -468,74 +468,87 @@ const char *otaGetUrl()
468468
bool otaCheckVersion(char *versionAvailable, uint8_t versionAvailableLength)
469469
{
470470
bool gotVersion = false;
471-
#ifdef COMPILE_WIFI
471+
#ifdef COMPILE_NETWORK
472472
bool previouslyConnected = wifiIsConnected();
473473

474474
bool wasInAPmode;
475475

476-
if (wifiConnect(settings.wifiConnectTimeoutMs, true, &wasInAPmode) == true) // Use WIFI_AP_STA if already in WIFI_AP mode
476+
uint8_t networkType = networkGetActiveType();
477+
if ((networkType == NETWORK_TYPE_WIFI)
478+
&& (wifiNetworkCount() == 0))
479+
{
480+
systemPrintln("Error: Please enter at least one SSID before getting keys");
481+
}
482+
else
477483
{
478-
char versionString[21];
479-
getFirmwareVersion(versionString, sizeof(versionString), enableRCFirmware);
480-
systemPrintf("Current firmware version: %s\r\n", versionString);
484+
if ((networkType != NETWORK_TYPE_WIFI)
485+
|| (wifiConnect(settings.wifiConnectTimeoutMs, true, &wasInAPmode) == true)) // Use WIFI_AP_STA if already in WIFI_AP mode
486+
{
487+
char versionString[21];
488+
getFirmwareVersion(versionString, sizeof(versionString), enableRCFirmware);
489+
systemPrintf("Current firmware version: %s\r\n", versionString);
481490

482-
const char *url = otaGetUrl();
483-
systemPrintf("Checking to see if an update is available from %s\r\n", url);
491+
const char *url = otaGetUrl();
492+
systemPrintf("Checking to see if an update is available from %s\r\n", url);
484493

485-
ESP32OTAPull ota;
494+
ESP32OTAPull ota;
486495

487-
int response = ota.CheckForOTAUpdate(url, versionString, ESP32OTAPull::DONT_DO_UPDATE);
496+
int response = ota.CheckForOTAUpdate(url, versionString, ESP32OTAPull::DONT_DO_UPDATE);
488497

489-
// We don't care if the library thinks the available firmware is newer, we just need a successful JSON parse
490-
if (response == ESP32OTAPull::UPDATE_AVAILABLE || response == ESP32OTAPull::NO_UPDATE_AVAILABLE)
491-
{
492-
gotVersion = true;
498+
// We don't care if the library thinks the available firmware is newer, we just need a successful JSON parse
499+
if (response == ESP32OTAPull::UPDATE_AVAILABLE || response == ESP32OTAPull::NO_UPDATE_AVAILABLE)
500+
{
501+
gotVersion = true;
493502

494-
// Call getVersion after original inquiry
495-
String otaVersion = ota.GetVersion();
496-
otaVersion.toCharArray(versionAvailable, versionAvailableLength);
497-
}
498-
else if (response == ESP32OTAPull::HTTP_FAILED)
499-
{
500-
systemPrintln("Firmware server not available");
503+
// Call getVersion after original inquiry
504+
String otaVersion = ota.GetVersion();
505+
otaVersion.toCharArray(versionAvailable, versionAvailableLength);
506+
}
507+
else if (response == ESP32OTAPull::HTTP_FAILED)
508+
{
509+
systemPrintln("Firmware server not available");
510+
}
511+
else
512+
{
513+
systemPrintln("OTA failed");
514+
}
515+
516+
if (networkType == NETWORK_TYPE_WIFI)
517+
{
518+
// If we were in WIFI_AP mode, return to WIFI_AP mode
519+
// There may be some overlap with systemState STATE_WIFI_CONFIG ? Not sure...
520+
if (wasInAPmode)
521+
WiFi.mode(WIFI_AP);
522+
523+
if (systemState != STATE_WIFI_CONFIG)
524+
{
525+
// WIFI_STOP() turns off the entire radio including the webserver. We need to turn off Station mode only.
526+
// For now, unit exits AP mode via reset so if we are in AP config mode, leave WiFi Station running.
527+
528+
// If WiFi was originally off, turn it off again
529+
if (previouslyConnected == false)
530+
WIFI_STOP();
531+
}
532+
}
501533
}
502534
else
503535
{
504-
systemPrintln("OTA failed");
536+
systemPrintln("Network not available for OTA!");
505537
}
506538
}
507-
else
508-
{
509-
systemPrintln("WiFi not available.");
510-
}
511-
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-
517-
if (systemState != STATE_WIFI_CONFIG)
518-
{
519-
// WIFI_STOP() turns off the entire radio including the webserver. We need to turn off Station mode only.
520-
// For now, unit exits AP mode via reset so if we are in AP config mode, leave WiFi Station running.
521-
522-
// If WiFi was originally off, turn it off again
523-
if (previouslyConnected == false)
524-
WIFI_STOP();
525-
}
526539

527540
if (gotVersion == true)
528541
log_d("Available OTA firmware version: %s\r\n", versionAvailable);
529542

530-
#endif // COMPILE_WIFI
543+
#endif // COMPILE_NETWORK
531544
return (gotVersion);
532545
}
533546

534547
// Force updates firmware using OTA pull
535548
// Exits by either updating firmware and resetting, or failing to connect
536549
void overTheAirUpdate()
537550
{
538-
#ifdef COMPILE_WIFI
551+
#ifdef COMPILE_NETWORK
539552
char versionString[9];
540553
formatFirmwareVersion(0, 0, versionString, sizeof(versionString), false);
541554

@@ -566,13 +579,13 @@ void overTheAirUpdate()
566579
systemPrintln("OTA Update: Firmware server not available");
567580
else
568581
systemPrintln("OTA Update: OTA failed");
569-
#endif
582+
#endif // COMPILE_NETWORK
570583
}
571584

572585
// Start WiFi and perform the over-the-air update
573586
void otaUpdate()
574587
{
575-
#ifdef COMPILE_WIFI
588+
#ifdef COMPILE_NETWORK
576589
bool previouslyConnected = wifiIsConnected();
577590

578591
bool wasInAPmode;
@@ -588,7 +601,7 @@ void otaUpdate()
588601
if (previouslyConnected == false)
589602
WIFI_STOP();
590603

591-
#endif // COMPILE_WIFI
604+
#endif // COMPILE_NETWORK
592605
}
593606

594607
// Called while the OTA Pull update is happening
@@ -633,7 +646,7 @@ void otaDisplayPercentage(int bytesWritten, int totalLength, bool alwaysDisplay)
633646

634647
const char *otaPullErrorText(int code)
635648
{
636-
#ifdef COMPILE_WIFI
649+
#ifdef COMPILE_NETWORK
637650
switch (code)
638651
{
639652
case ESP32OTAPull::UPDATE_AVAILABLE:
@@ -657,7 +670,7 @@ const char *otaPullErrorText(int code)
657670
return "Unexpected HTTP response code";
658671
break;
659672
}
660-
#endif // COMPILE_WIFI
673+
#endif // COMPILE_NETWORK
661674
return "Unknown error";
662675
}
663676

@@ -871,7 +884,7 @@ void otaAutoUpdate()
871884
if ((millis() - otaLastUpdateCheck) >= checkIntervalMillis)
872885
{
873886
otaLastUpdateCheck = millis();
874-
otaSetState(OTA_STATE_START_WIFI);
887+
otaSetState(OTA_STATE_START_NETWORK);
875888
}
876889
}
877890

@@ -892,20 +905,20 @@ void otaAutoUpdate()
892905
break;
893906

894907
// Start the WiFi network
895-
case OTA_STATE_START_WIFI:
908+
case OTA_STATE_START_NETWORK:
896909
if (settings.debugFirmwareUpdate)
897910
systemPrintln("Firmware update starting WiFi");
898-
if (!networkUserOpen(NETWORK_USER_OTA_AUTO_UPDATE, NETWORK_TYPE_WIFI))
911+
if (!networkUserOpen(NETWORK_USER_OTA_AUTO_UPDATE, NETWORK_TYPE_ACTIVE))
899912
{
900913
systemPrintln("Firmware update failed, unable to start WiFi");
901914
otaAutoUpdateStop();
902915
}
903916
else
904-
otaSetState(OTA_STATE_WAIT_FOR_WIFI);
917+
otaSetState(OTA_STATE_WAIT_FOR_NETWORK);
905918
break;
906919

907920
// Wait for connection to the access point
908-
case OTA_STATE_WAIT_FOR_WIFI:
921+
case OTA_STATE_WAIT_FOR_NETWORK:
909922
// Determine if the network has failed
910923
if (networkIsShuttingDown(NETWORK_USER_OTA_AUTO_UPDATE))
911924
otaAutoUpdateStop();

Firmware/RTK_Everywhere/settings.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,8 +955,8 @@ const ubxCmd ubxCommands[] = {
955955
enum OtaState
956956
{
957957
OTA_STATE_OFF = 0,
958-
OTA_STATE_START_WIFI,
959-
OTA_STATE_WAIT_FOR_WIFI,
958+
OTA_STATE_START_NETWORK,
959+
OTA_STATE_WAIT_FOR_NETWORK,
960960
OTA_STATE_GET_FIRMWARE_VERSION,
961961
OTA_STATE_CHECK_FIRMWARE_VERSION,
962962
OTA_STATE_UPDATE_FIRMWARE,

0 commit comments

Comments
 (0)