mirror of
https://github.com/MarekZegare4/MeshCore-Solo.git
synced 2026-11-02 19:02:00 +00:00
Add wifi.enabled pref and CLI; skip WiFi when disabled or SSID blank
This commit is contained in:
@@ -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);
|
sprintf(reply, "> %s", _prefs.wifi_ssid[0] ? _prefs.wifi_ssid : WIFI_SSID);
|
||||||
return true;
|
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) {
|
if (strcmp(command, "get wifi.status") == 0) {
|
||||||
strcpy(reply, WiFi.status() == WL_CONNECTED ? "> connected" : "> disconnected");
|
strcpy(reply, WiFi.status() == WL_CONNECTED ? "> connected" : "> disconnected");
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ public:
|
|||||||
#ifdef WIFI_SSID
|
#ifdef WIFI_SSID
|
||||||
char wifi_ssid[33] = {0}; // if empty, the compile-time WIFI_SSID is used
|
char wifi_ssid[33] = {0}; // if empty, the compile-time WIFI_SSID is used
|
||||||
char wifi_pwd[64] = {0};
|
char wifi_pwd[64] = {0};
|
||||||
|
uint8_t wifi_enabled = 1; // 0 = never bring up WiFi (credentials may still be baked in)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -171,6 +172,7 @@ private:
|
|||||||
void structure() override {
|
void structure() override {
|
||||||
def("ssid", _parent->wifi_ssid, sizeof(_parent->wifi_ssid));
|
def("ssid", _parent->wifi_ssid, sizeof(_parent->wifi_ssid));
|
||||||
def("pwd", _parent->wifi_pwd, sizeof(_parent->wifi_pwd));
|
def("pwd", _parent->wifi_pwd, sizeof(_parent->wifi_pwd));
|
||||||
|
def("enabled", _parent->wifi_enabled);
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
WiFiPrefs(NodePrefs* parent) : _parent(parent) { }
|
WiFiPrefs(NodePrefs* parent) : _parent(parent) { }
|
||||||
|
|||||||
@@ -127,6 +127,7 @@ void halt() {
|
|||||||
char wifi_ssid[33] = WIFI_SSID; // replaced by stored prefs at boot, if set
|
char wifi_ssid[33] = WIFI_SSID; // replaced by stored prefs at boot, if set
|
||||||
char wifi_pwd[64] = WIFI_PWD;
|
char wifi_pwd[64] = WIFI_PWD;
|
||||||
bool wifi_was_connected = false;
|
bool wifi_was_connected = false;
|
||||||
|
bool wifi_enabled = false; // set at boot from prefs; false also when the effective SSID is blank
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
@@ -208,6 +209,18 @@ void setup() {
|
|||||||
|
|
||||||
// add wifi interface
|
// add wifi interface
|
||||||
#ifdef WIFI_SSID
|
#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)
|
#if defined(ESP32)
|
||||||
board.setInhibitSleep(true); // prevent sleep when WiFi is active
|
board.setInhibitSleep(true); // prevent sleep when WiFi is active
|
||||||
WiFi.setAutoReconnect(true);
|
WiFi.setAutoReconnect(true);
|
||||||
@@ -223,15 +236,6 @@ void setup() {
|
|||||||
});
|
});
|
||||||
#endif
|
#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);
|
WIFI_DEBUG_PRINTLN("connecting to %s", wifi_ssid);
|
||||||
|
|
||||||
#if defined(RP2040_PLATFORM)
|
#if defined(RP2040_PLATFORM)
|
||||||
@@ -246,6 +250,9 @@ void setup() {
|
|||||||
#endif
|
#endif
|
||||||
wifi_interface.begin(TCP_PORT);
|
wifi_interface.begin(TCP_PORT);
|
||||||
interface_manager.addInterface(InterfaceType::WiFi, &wifi_interface);
|
interface_manager.addInterface(InterfaceType::WiFi, &wifi_interface);
|
||||||
|
} else {
|
||||||
|
WIFI_DEBUG_PRINTLN("wifi disabled");
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// add usb interface
|
// add usb interface
|
||||||
@@ -301,6 +308,7 @@ void loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WIFI_SSID
|
#ifdef WIFI_SSID
|
||||||
|
if (wifi_enabled) {
|
||||||
// RP2040 has no WiFi event callbacks, so poll the link state instead
|
// RP2040 has no WiFi event callbacks, so poll the link state instead
|
||||||
#if defined(RP2040_PLATFORM)
|
#if defined(RP2040_PLATFORM)
|
||||||
wifi_needs_reconnect = (WiFi.status() != WL_CONNECTED);
|
wifi_needs_reconnect = (WiFi.status() != WL_CONNECTED);
|
||||||
@@ -327,5 +335,6 @@ void loop() {
|
|||||||
#endif
|
#endif
|
||||||
last_wifi_reconnect_attempt = millis();
|
last_wifi_reconnect_attempt = millis();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user