Skip to content

Commit 6bc6428

Browse files
committed
WebServer: Restart the web server when the WiFi parameters change
1 parent 099359f commit 6bc6428

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

Firmware/RTK_Everywhere/States.ino

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ void stateUpdate()
445445
// Allow for 750ms before we parse buffer for all data to arrive
446446
if (millis() - timeSinceLastIncomingSetting > 750)
447447
{
448+
bool changed;
449+
448450
currentlyParsingData =
449451
true; // Disallow new data to flow from websocket while we are parsing the current data
450452

@@ -453,17 +455,25 @@ void stateUpdate()
453455
systemWrite(incomingSettings[x]);
454456
systemPrintln();
455457

458+
webServerSettingsClone();
456459
parseIncomingSettings();
457460
settings.gnssConfiguredOnce = false; // On the next boot, reapply all settings
458461
settings.gnssConfiguredBase = false;
459462
settings.gnssConfiguredRover = false;
460463
recordSystemSettings(); // Record these settings to unit
464+
changed = webServerSettingsCheckAndFree();
461465

462466
// Clear buffer
463467
incomingSettingsSpot = 0;
464468
memset(incomingSettings, 0, AP_CONFIG_SETTING_SIZE);
465469

466470
currentlyParsingData = false; // Allow new data from websocket
471+
if (changed)
472+
{
473+
// Restart the web server if the WiFi parameters changed
474+
webServerStop();
475+
changeState(STATE_WEB_CONFIG_NOT_STARTED); // Return to rover mode to avoid being in fixed base mode
476+
}
467477
}
468478
}
469479

Firmware/RTK_Everywhere/WebServer.ino

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static const char *const webServerStateNames[] = {
5252
static const int webServerStateEntries = sizeof(webServerStateNames) / sizeof(webServerStateNames[0]);
5353

5454
static uint8_t webServerState;
55+
static struct Settings * webServerPreviousSettings;
5556

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

1129+
//----------------------------------------
1130+
// Check the web server settings and free the settings structure
1131+
//----------------------------------------
1132+
bool webServerSettingsCheckAndFree()
1133+
{
1134+
bool changed;
1135+
1136+
changed = false;
1137+
if (webServerPreviousSettings)
1138+
{
1139+
changed = wifiCheckSettings(webServerPreviousSettings);
1140+
rtkFree(webServerPreviousSettings, "WebServer previous settings");
1141+
webServerPreviousSettings = nullptr;
1142+
}
1143+
return changed;
1144+
}
1145+
1146+
//----------------------------------------
1147+
// Allocate the settings structure and clone the settings
1148+
//----------------------------------------
1149+
void webServerSettingsClone()
1150+
{
1151+
webServerPreviousSettings = (struct Settings *) rtkMalloc(sizeof(settings), "WebServer previous settings");
1152+
if (webServerPreviousSettings == nullptr)
1153+
systemPrintln("ERROR: Web Server failed to allocate previous settings!\r\n");
1154+
else
1155+
memcpy(webServerPreviousSettings, &settings, sizeof(settings));
1156+
}
1157+
11281158
//----------------------------------------
11291159
// Start the Web Server state machine
11301160
//----------------------------------------

Firmware/RTK_Everywhere/WiFi.ino

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,29 @@ void menuWiFi()
460460
clearBuffer(); // Empty buffer of any newline chars
461461
}
462462

463+
//*********************************************************************
464+
// Check for setting differences
465+
bool wifiCheckSettings(struct Settings * newSettings)
466+
{
467+
bool changed;
468+
469+
// Determine if any of the WiFi station SSID or password changes
470+
changed = (memcmp(newSettings->wifiNetworks[0].ssid, settings.wifiNetworks[0].ssid, SSID_LENGTH) != 0)
471+
|| (memcmp(newSettings->wifiNetworks[0].password, settings.wifiNetworks[0].password, PASSWORD_LENGTH) != 0)
472+
|| (memcmp(newSettings->wifiNetworks[1].ssid, settings.wifiNetworks[1].ssid, SSID_LENGTH) != 0)
473+
|| (memcmp(newSettings->wifiNetworks[1].password, settings.wifiNetworks[1].password, PASSWORD_LENGTH) != 0)
474+
|| (memcmp(newSettings->wifiNetworks[2].ssid, settings.wifiNetworks[2].ssid, SSID_LENGTH) != 0)
475+
|| (memcmp(newSettings->wifiNetworks[2].password, settings.wifiNetworks[2].password, PASSWORD_LENGTH) != 0)
476+
|| (memcmp(newSettings->wifiNetworks[3].ssid, settings.wifiNetworks[3].ssid, SSID_LENGTH) != 0)
477+
|| (memcmp(newSettings->wifiNetworks[3].password, settings.wifiNetworks[3].password, PASSWORD_LENGTH) != 0);
478+
if (changed)
479+
{
480+
// Update the WiFi settings
481+
wifiUpdateSettings();
482+
}
483+
return changed;
484+
}
485+
463486
//*********************************************************************
464487
// Display the soft AP details
465488
void wifiDisplayNetworkData()

0 commit comments

Comments
 (0)