Add wifi.enabled pref and CLI; skip WiFi when disabled or SSID blank

This commit is contained in:
Michael Graff
2026-09-06 21:56:25 -05:00
parent 98cf4f3332
commit eb2ab10de0
3 changed files with 64 additions and 43 deletions
+10
View File
@@ -2187,6 +2187,16 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp, char*
sprintf(reply, "> %s", _prefs.wifi_ssid[0] ? _prefs.wifi_ssid : WIFI_SSID);
return true;
}
if (memcmp(command, "set wifi.enabled ", 17) == 0) {
_prefs.wifi_enabled = atoi(&command[17]) ? 1 : 0;
savePrefs();
sprintf(reply, "> wifi.enabled is now %d (reboot to apply)", _prefs.wifi_enabled);
return true;
}
if (strcmp(command, "get wifi.enabled") == 0) {
sprintf(reply, "> %d", _prefs.wifi_enabled);
return true;
}
if (strcmp(command, "get wifi.status") == 0) {
strcpy(reply, WiFi.status() == WL_CONNECTED ? "> connected" : "> disconnected");
return true;
+2
View File
@@ -47,6 +47,7 @@ public:
#ifdef WIFI_SSID
char wifi_ssid[33] = {0}; // if empty, the compile-time WIFI_SSID is used
char wifi_pwd[64] = {0};
uint8_t wifi_enabled = 1; // 0 = never bring up WiFi (credentials may still be baked in)
#endif
private:
@@ -171,6 +172,7 @@ private:
void structure() override {
def("ssid", _parent->wifi_ssid, sizeof(_parent->wifi_ssid));
def("pwd", _parent->wifi_pwd, sizeof(_parent->wifi_pwd));
def("enabled", _parent->wifi_enabled);
}
public:
WiFiPrefs(NodePrefs* parent) : _parent(parent) { }
+18 -9
View File
@@ -127,6 +127,7 @@ void halt() {
char wifi_ssid[33] = WIFI_SSID; // replaced by stored prefs at boot, if set
char wifi_pwd[64] = WIFI_PWD;
bool wifi_was_connected = false;
bool wifi_enabled = false; // set at boot from prefs; false also when the effective SSID is blank
#endif
void setup() {
@@ -208,6 +209,18 @@ void setup() {
// add wifi interface
#ifdef WIFI_SSID
// stored credentials win over the build-time ones ('set wifi.ssid <x>' over USB serial).
// they are taken as a pair, so 'set wifi.ssid' alone gives an empty password, not a
// silent fallback to the build-time password of a different network. Copied out of prefs
// so 'set wifi.*' edits only take effect on reboot, as their replies promise.
// (No NULL-for-open-network: the RP2040 core does strlen() on the password unguarded.)
if (the_mesh.getNodePrefs()->wifi_ssid[0]) {
strcpy(wifi_ssid, the_mesh.getNodePrefs()->wifi_ssid);
strcpy(wifi_pwd, the_mesh.getNodePrefs()->wifi_pwd);
}
// 'set wifi.enabled 0' or a build with blank credentials leaves the radio off entirely
wifi_enabled = the_mesh.getNodePrefs()->wifi_enabled && wifi_ssid[0];
if (wifi_enabled) {
#if defined(ESP32)
board.setInhibitSleep(true); // prevent sleep when WiFi is active
WiFi.setAutoReconnect(true);
@@ -223,15 +236,6 @@ void setup() {
});
#endif
// stored credentials win over the build-time ones ('set wifi.ssid <x>' over USB serial).
// they are taken as a pair, so 'set wifi.ssid' alone gives an empty password, not a
// silent fallback to the build-time password of a different network. Copied out of prefs
// so 'set wifi.*' edits only take effect on reboot, as their replies promise.
// (No NULL-for-open-network: the RP2040 core does strlen() on the password unguarded.)
if (the_mesh.getNodePrefs()->wifi_ssid[0]) {
strcpy(wifi_ssid, the_mesh.getNodePrefs()->wifi_ssid);
strcpy(wifi_pwd, the_mesh.getNodePrefs()->wifi_pwd);
}
WIFI_DEBUG_PRINTLN("connecting to %s", wifi_ssid);
#if defined(RP2040_PLATFORM)
@@ -246,6 +250,9 @@ void setup() {
#endif
wifi_interface.begin(TCP_PORT);
interface_manager.addInterface(InterfaceType::WiFi, &wifi_interface);
} else {
WIFI_DEBUG_PRINTLN("wifi disabled");
}
#endif
// add usb interface
@@ -301,6 +308,7 @@ void loop() {
}
#ifdef WIFI_SSID
if (wifi_enabled) {
// RP2040 has no WiFi event callbacks, so poll the link state instead
#if defined(RP2040_PLATFORM)
wifi_needs_reconnect = (WiFi.status() != WL_CONNECTED);
@@ -327,5 +335,6 @@ void loop() {
#endif
last_wifi_reconnect_attempt = millis();
}
}
#endif
}