Skip to content

WebConfig: Perform OTA update after factory reset and setting WiFi SSID and password #662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Firmware/RTK_Everywhere/HTTP_Client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ void httpClientUpdate()
// Connect to the HTTP server
case HTTP_CLIENT_CONNECTING_2_SERVER: {
// Allocate the httpSecureClient structure
networkUseDefaultInterface();
httpSecureClient = new NetworkClientSecure();
if (!httpSecureClient)
{
Expand Down
1 change: 1 addition & 0 deletions Firmware/RTK_Everywhere/MQTT_Client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ void mqttClientUpdate()
// Connect to the MQTT broker
case MQTT_CLIENT_CONNECTING_2_BROKER: {
// Allocate the mqttSecureClient structure
networkUseDefaultInterface();
mqttSecureClient = new NetworkClientSecure();
if (!mqttSecureClient)
{
Expand Down
19 changes: 19 additions & 0 deletions Firmware/RTK_Everywhere/Network.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2451,6 +2451,25 @@ void networkUpdate()
}
}

//----------------------------------------
// Set the default network interface
//----------------------------------------
void networkUseDefaultInterface()
{
NetIndex_t index;
bool isDefault;

// Get the network index
index = networkGetCurrentInterfaceIndex();
if (index < NETWORK_OFFLINE)
{
// Get the default network interface
isDefault = networkInterfaceTable[index].netif->isDefault();
if (!isDefault)
networkInterfaceTable[index].netif->setDefault();
}
}

//----------------------------------------
// Add a network user
//----------------------------------------
Expand Down
1 change: 1 addition & 0 deletions Firmware/RTK_Everywhere/NtripClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ void ntripClientUpdate()
if (connected)
{
// Allocate the ntripClient structure
networkUseDefaultInterface();
ntripClient = new NetworkClient();
if (!ntripClient)
{
Expand Down
10 changes: 10 additions & 0 deletions Firmware/RTK_Everywhere/States.ino
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ void stateUpdate()
// Allow for 750ms before we parse buffer for all data to arrive
if (millis() - timeSinceLastIncomingSetting > 750)
{
bool changed;

currentlyParsingData =
true; // Disallow new data to flow from websocket while we are parsing the current data

Expand All @@ -453,17 +455,25 @@ void stateUpdate()
systemWrite(incomingSettings[x]);
systemPrintln();

webServerSettingsClone();
parseIncomingSettings();
settings.gnssConfiguredOnce = false; // On the next boot, reapply all settings
settings.gnssConfiguredBase = false;
settings.gnssConfiguredRover = false;
recordSystemSettings(); // Record these settings to unit
changed = webServerSettingsCheckAndFree();

// Clear buffer
incomingSettingsSpot = 0;
memset(incomingSettings, 0, AP_CONFIG_SETTING_SIZE);

currentlyParsingData = false; // Allow new data from websocket
if (changed)
{
// Restart the web server if the WiFi parameters changed
webServerStop();
changeState(STATE_WEB_CONFIG_NOT_STARTED); // Return to rover mode to avoid being in fixed base mode
}
}
}

Expand Down
1 change: 1 addition & 0 deletions Firmware/RTK_Everywhere/TcpClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ bool tcpClientStart()
NetworkClient *client;

// Allocate the TCP client
networkUseDefaultInterface();
client = new NetworkClient();
if (client)
{
Expand Down
34 changes: 32 additions & 2 deletions Firmware/RTK_Everywhere/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static const char *const webServerStateNames[] = {
static const int webServerStateEntries = sizeof(webServerStateNames) / sizeof(webServerStateNames[0]);

static uint8_t webServerState;
static struct Settings * webServerPreviousSettings;

// Once connected to the access point for WiFi Config, the ESP32 sends current setting values in one long string to
// websocket After user clicks 'save', data is validated via main.js and a long string of values is returned.
Expand Down Expand Up @@ -1125,6 +1126,35 @@ void webServerSetState(uint8_t newState)
reportFatalError("Web Server: Invalid web config state");
}

//----------------------------------------
// Check the web server settings and free the settings structure
//----------------------------------------
bool webServerSettingsCheckAndFree()
{
bool changed;

changed = false;
if (webServerPreviousSettings)
{
changed = wifiCheckSettings(webServerPreviousSettings);
rtkFree(webServerPreviousSettings, "WebServer previous settings");
webServerPreviousSettings = nullptr;
}
return changed;
}

//----------------------------------------
// Allocate the settings structure and clone the settings
//----------------------------------------
void webServerSettingsClone()
{
webServerPreviousSettings = (struct Settings *) rtkMalloc(sizeof(settings), "WebServer previous settings");
if (webServerPreviousSettings == nullptr)
systemPrintln("ERROR: Web Server failed to allocate previous settings!\r\n");
else
memcpy(webServerPreviousSettings, &settings, sizeof(settings));
}

//----------------------------------------
// Start the Web Server state machine
//----------------------------------------
Expand All @@ -1144,9 +1174,9 @@ void webServerStart()
systemPrintln("Web Server: Starting");

// Start the network
if (settings.wifiConfigOverAP == false)
if ((settings.wifiConfigOverAP == false) || networkHasInternet())
networkConsumerAdd(NETCONSUMER_WEB_CONFIG, NETWORK_ANY, __FILE__, __LINE__);
else
if (settings.wifiConfigOverAP)
networkSoftApConsumerAdd(NETCONSUMER_WEB_CONFIG, __FILE__, __LINE__);
webServerSetState(WEBSERVER_STATE_WAIT_FOR_NETWORK);
}
Expand Down
41 changes: 36 additions & 5 deletions Firmware/RTK_Everywhere/WiFi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,29 @@ void menuWiFi()
clearBuffer(); // Empty buffer of any newline chars
}

//*********************************************************************
// Check for setting differences
bool wifiCheckSettings(struct Settings * newSettings)
{
bool changed;

// Determine if any of the WiFi station SSID or password changes
changed = (memcmp(newSettings->wifiNetworks[0].ssid, settings.wifiNetworks[0].ssid, SSID_LENGTH) != 0)
|| (memcmp(newSettings->wifiNetworks[0].password, settings.wifiNetworks[0].password, PASSWORD_LENGTH) != 0)
|| (memcmp(newSettings->wifiNetworks[1].ssid, settings.wifiNetworks[1].ssid, SSID_LENGTH) != 0)
|| (memcmp(newSettings->wifiNetworks[1].password, settings.wifiNetworks[1].password, PASSWORD_LENGTH) != 0)
|| (memcmp(newSettings->wifiNetworks[2].ssid, settings.wifiNetworks[2].ssid, SSID_LENGTH) != 0)
|| (memcmp(newSettings->wifiNetworks[2].password, settings.wifiNetworks[2].password, PASSWORD_LENGTH) != 0)
|| (memcmp(newSettings->wifiNetworks[3].ssid, settings.wifiNetworks[3].ssid, SSID_LENGTH) != 0)
|| (memcmp(newSettings->wifiNetworks[3].password, settings.wifiNetworks[3].password, PASSWORD_LENGTH) != 0);
if (changed)
{
// Update the WiFi settings
wifiUpdateSettings();
}
return changed;
}

//*********************************************************************
// Display the soft AP details
void wifiDisplayNetworkData()
Expand Down Expand Up @@ -756,15 +779,15 @@ bool wifiStationEnabled(const char ** reason)
// Verify that at least one SSID value is set
if (wifiStationSsidSet == false)
{
*reason = "SSID not available";
*reason = ", SSID not available";
break;
}

// Determine if Wifi is begin restarted
if (wifiStationRestart)
{
wifiStationRestart = false;
*reason = "restart requested";
*reason = ", restart requested";
break;
}

Expand All @@ -778,19 +801,19 @@ bool wifiStationEnabled(const char ** reason)
// Build the reason
if (reasonBuffer)
{
sprintf(reasonBuffer,"is lower priority than %s", networkGetCurrentInterfaceName());
sprintf(reasonBuffer,", is lower priority than %s", networkGetCurrentInterfaceName());
*reason = reasonBuffer;
}

// Allocation failed
else
*reason = "is lower priority";
*reason = ", is lower priority";
break;
}

// WiFi should start and continue running
enabled = true;
*reason = "is enabled";
*reason = ", is enabled";
} while (0);
return enabled;
}
Expand Down Expand Up @@ -1056,6 +1079,14 @@ void wifiStationUpdate()
case WIFI_STATION_STATE_STABLE:
break;
}

// Periodically display the WiFi state
if (PERIODIC_DISPLAY(PD_WIFI_STATE))
{
systemPrintf("WiFi station state: %s%s\r\n",
wifiStationStateName[wifiStationState], reason);
PERIODIC_CLEAR(PD_WIFI_STATE);
}
}

//*********************************************************************
Expand Down
13 changes: 12 additions & 1 deletion Firmware/RTK_Everywhere/menuFirmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void firmwareVersionFormat(uint8_t major, uint8_t minor, char *buffer, int buffe
char prefix;

// Construct the full or release candidate version number
prefix = ENABLE_DEVELOPER ? 'd' : 'v';
prefix = (ENABLE_DEVELOPER || (major >= 99)) ? 'd' : 'v';
if (enableRCFirmware && (bufferLength >= 21))
// 123456789012345678901
// pxxx.yyy-dd-mmm-yyyy0
Expand Down Expand Up @@ -922,6 +922,17 @@ void otaUpdate()
break;
}
}

// Periodically display the state
if (PERIODIC_DISPLAY(PD_OTA_STATE))
{
char line[30];
const char * state;

PERIODIC_CLEAR(PD_OTA_STATE);
state = otaStateNameGet(otaState, line);
systemPrintf("OTA Firmware Update state: %s\r\n", state);
}
}

//----------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions Firmware/RTK_Everywhere/menuSystem.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,9 @@ void menuPeriodicPrint()
systemPrint("59) WebServer state: ");
systemPrintf("%s\r\n", PERIODIC_SETTING(PD_WEB_SERVER_STATE) ? "Enabled" : "Disabled");

systemPrint("60) OTA firmware update state: ");
systemPrintf("%s\r\n", PERIODIC_SETTING(PD_OTA_STATE) ? "Enabled" : "Disabled");

systemPrintln("------- Tasks ------");
systemPrint("70) btReadTask state: ");
systemPrintf("%s\r\n", PERIODIC_SETTING(PD_TASK_BLUETOOTH_READ) ? "Enabled" : "Disabled");
Expand Down Expand Up @@ -1339,6 +1342,8 @@ void menuPeriodicPrint()
PERIODIC_TOGGLE(PD_UDP_SERVER_BROADCAST_DATA);
else if (incoming == 59)
PERIODIC_TOGGLE(PD_WEB_SERVER_STATE);
else if (incoming == 60)
PERIODIC_TOGGLE(PD_OTA_STATE);

else if (incoming == 70)
PERIODIC_TOGGLE(PD_TASK_BLUETOOTH_READ);
Expand Down
5 changes: 4 additions & 1 deletion Firmware/RTK_Everywhere/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ enum PeriodDisplayValues
PD_GNSS_DATA_RX_BYTE_COUNT, // 38

PD_WEB_SERVER_STATE, // 39

PD_OTA_STATE, // 40
// Add new values before this line
};

Expand Down Expand Up @@ -396,11 +398,12 @@ typedef enum
} BluetoothRadioType_e;

#define SSID_LENGTH 50
#define PASSWORD_LENGTH 50

typedef struct WiFiNetwork
{
char ssid[SSID_LENGTH];
char password[50];
char password[PASSWORD_LENGTH];
} WiFiNetwork;

#define MAX_WIFI_NETWORKS 4
Expand Down