Skip to content

Commit df9280d

Browse files
committed
feat: Add companion WiFi CLI
1 parent 856df24 commit df9280d

5 files changed

Lines changed: 113 additions & 4 deletions

File tree

examples/companion_radio/DataStore.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ void DataStore::loadPrefsInt(const char *filename, NodePrefs& _prefs, double& no
230230
file.read((uint8_t *)&_prefs.gps_interval, sizeof(_prefs.gps_interval)); // 86
231231
file.read((uint8_t *)&_prefs.autoadd_config, sizeof(_prefs.autoadd_config)); // 87
232232
file.read((uint8_t *)&_prefs.autoadd_max_hops, sizeof(_prefs.autoadd_max_hops)); // 88
233-
file.read((uint8_t *)&_prefs.rx_boosted_gain, sizeof(_prefs.rx_boosted_gain)); // 89
233+
file.read((uint8_t *)&_prefs.rx_boosted_gain, sizeof(_prefs.rx_boosted_gain)); // 89
234+
file.read((uint8_t *)_prefs.wifi_ssid, sizeof(_prefs.wifi_ssid)); // 90
235+
file.read((uint8_t *)_prefs.wifi_pwd, sizeof(_prefs.wifi_pwd)); // 123
234236

235237
file.close();
236238
}
@@ -268,7 +270,9 @@ void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_
268270
file.write((uint8_t *)&_prefs.gps_interval, sizeof(_prefs.gps_interval)); // 86
269271
file.write((uint8_t *)&_prefs.autoadd_config, sizeof(_prefs.autoadd_config)); // 87
270272
file.write((uint8_t *)&_prefs.autoadd_max_hops, sizeof(_prefs.autoadd_max_hops)); // 88
271-
file.write((uint8_t *)&_prefs.rx_boosted_gain, sizeof(_prefs.rx_boosted_gain)); // 89
273+
file.write((uint8_t *)&_prefs.rx_boosted_gain, sizeof(_prefs.rx_boosted_gain)); // 89
274+
file.write((uint8_t *)_prefs.wifi_ssid, sizeof(_prefs.wifi_ssid)); // 90
275+
file.write((uint8_t *)_prefs.wifi_pwd, sizeof(_prefs.wifi_pwd)); // 123
272276

273277
file.close();
274278
}

examples/companion_radio/MyMesh.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#include <Arduino.h> // needed for PlatformIO
44
#include <Mesh.h>
5+
#ifdef WIFI_SSID
6+
#include <WiFi.h>
7+
#endif
58

69
#define CMD_APP_START 1
710
#define CMD_SEND_TXT_MSG 2
@@ -141,6 +144,25 @@
141144
#define AUTO_ADD_ROOM_SERVER (1 << 3) // 0x08 - auto-add Room Server (ADV_TYPE_ROOM)
142145
#define AUTO_ADD_SENSOR (1 << 4) // 0x10 - auto-add Sensor (ADV_TYPE_SENSOR)
143146

147+
#ifdef WIFI_SSID
148+
static void restartWifiClient(const NodePrefs& prefs) {
149+
const char* ssid = prefs.wifi_ssid;
150+
const char* pwd = prefs.wifi_pwd;
151+
152+
if (ssid[0] == 0) {
153+
WiFi.disconnect();
154+
return;
155+
}
156+
157+
WiFi.disconnect();
158+
if (pwd[0] == 0) {
159+
WiFi.begin(ssid);
160+
} else {
161+
WiFi.begin(ssid, pwd);
162+
}
163+
}
164+
#endif
165+
144166
void MyMesh::writeOKFrame() {
145167
uint8_t buf[1];
146168
buf[0] = RESP_CODE_OK;
@@ -895,6 +917,15 @@ void MyMesh::begin(bool has_display) {
895917

896918
// load persisted prefs
897919
_store->loadPrefs(_prefs, sensors.node_lat, sensors.node_lon);
920+
_prefs.wifi_ssid[sizeof(_prefs.wifi_ssid) - 1] = 0;
921+
_prefs.wifi_pwd[sizeof(_prefs.wifi_pwd) - 1] = 0;
922+
#ifdef WIFI_SSID
923+
if (_prefs.wifi_ssid[0] == 0 && _prefs.wifi_pwd[0] == 0) {
924+
StrHelper::strncpy(_prefs.wifi_ssid, WIFI_SSID, sizeof(_prefs.wifi_ssid));
925+
StrHelper::strncpy(_prefs.wifi_pwd, WIFI_PWD, sizeof(_prefs.wifi_pwd));
926+
savePrefs();
927+
}
928+
#endif
898929

899930
// sanitise bad pref values
900931
_prefs.rx_delay_base = constrain(_prefs.rx_delay_base, 0, 20.0f);
@@ -1911,6 +1942,42 @@ void MyMesh::enterCLIRescue() {
19111942
Serial.println("========= CLI Rescue =========");
19121943
}
19131944

1945+
bool MyMesh::handleWifiGetConfig(const char* config) {
1946+
#ifdef WIFI_SSID
1947+
if (memcmp(config, "wifi.ssid", 9) == 0) {
1948+
Serial.printf(" > %s\n", _prefs.wifi_ssid);
1949+
return true;
1950+
} else if (memcmp(config, "wifi.pwd", 8) == 0) {
1951+
Serial.printf(" > %s\n", _prefs.wifi_pwd);
1952+
return true;
1953+
}
1954+
#else
1955+
(void)config;
1956+
#endif
1957+
return false;
1958+
}
1959+
1960+
bool MyMesh::handleWifiSetConfig(const char* config) {
1961+
#ifdef WIFI_SSID
1962+
if (memcmp(config, "wifi.ssid ", 10) == 0) {
1963+
StrHelper::strncpy(_prefs.wifi_ssid, &config[10], sizeof(_prefs.wifi_ssid));
1964+
savePrefs();
1965+
restartWifiClient(_prefs);
1966+
Serial.printf(" > wifi.ssid is now %s\n", _prefs.wifi_ssid);
1967+
return true;
1968+
} else if (memcmp(config, "wifi.pwd ", 9) == 0) {
1969+
StrHelper::strncpy(_prefs.wifi_pwd, &config[9], sizeof(_prefs.wifi_pwd));
1970+
savePrefs();
1971+
restartWifiClient(_prefs);
1972+
Serial.printf(" > wifi.pwd is now %s\n", _prefs.wifi_pwd);
1973+
return true;
1974+
}
1975+
#else
1976+
(void)config;
1977+
#endif
1978+
return false;
1979+
}
1980+
19141981
void MyMesh::checkCLIRescueCmd() {
19151982
int len = strlen(cli_command);
19161983
while (Serial.available() && len < sizeof(cli_command)-1) {
@@ -2115,6 +2182,32 @@ void MyMesh::loop() {
21152182

21162183
if (_cli_rescue) {
21172184
checkCLIRescueCmd();
2185+
#ifdef WIFI_SSID
2186+
} else if (Serial.available()) {
2187+
int len = strlen(cli_command);
2188+
while (Serial.available() && len < sizeof(cli_command)-1) {
2189+
char c = Serial.read();
2190+
if (c != '\n') {
2191+
cli_command[len++] = c;
2192+
cli_command[len] = 0;
2193+
}
2194+
Serial.print(c);
2195+
}
2196+
if (len == sizeof(cli_command)-1) {
2197+
cli_command[sizeof(cli_command)-1] = '\r';
2198+
}
2199+
if (len > 0 && cli_command[len - 1] == '\r') {
2200+
cli_command[len - 1] = 0;
2201+
if (memcmp(cli_command, "get ", 4) == 0) {
2202+
const char* config = &cli_command[4];
2203+
handleWifiGetConfig(config);
2204+
} else if (memcmp(cli_command, "set ", 4) == 0) {
2205+
const char* config = &cli_command[4];
2206+
handleWifiSetConfig(config);
2207+
}
2208+
cli_command[0] = 0;
2209+
}
2210+
#endif
21182211
} else {
21192212
checkSerialInterface();
21202213
}

examples/companion_radio/MyMesh.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
191191
return _store->putBlobByKey(key, key_len, src_buf, len);
192192
}
193193

194+
bool handleWifiGetConfig(const char* config);
195+
bool handleWifiSetConfig(const char* config);
194196
void checkCLIRescueCmd();
195197
void checkSerialInterface();
196198
bool isValidClientRepeatFreq(uint32_t f) const;

examples/companion_radio/NodePrefs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ struct NodePrefs { // persisted to file
3232
uint8_t client_repeat;
3333
uint8_t path_hash_mode; // which path mode to use when sending
3434
uint8_t autoadd_max_hops; // 0 = no limit, 1 = direct (0 hops), N = up to N-1 hops (max 64)
35-
};
35+
char wifi_ssid[33];
36+
char wifi_pwd[65];
37+
};

examples/companion_radio/main.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,15 @@ void setup() {
195195

196196
#ifdef WIFI_SSID
197197
board.setInhibitSleep(true); // prevent sleep when WiFi is active
198-
WiFi.begin(WIFI_SSID, WIFI_PWD);
198+
const char* wifi_ssid = the_mesh.getNodePrefs()->wifi_ssid;
199+
const char* wifi_pwd = the_mesh.getNodePrefs()->wifi_pwd;
200+
if (wifi_ssid[0] != 0) {
201+
if (wifi_pwd[0] == 0) {
202+
WiFi.begin(wifi_ssid);
203+
} else {
204+
WiFi.begin(wifi_ssid, wifi_pwd);
205+
}
206+
}
199207
serial_interface.begin(TCP_PORT);
200208
#elif defined(BLE_PIN_CODE)
201209
serial_interface.begin(BLE_NAME_PREFIX, the_mesh.getNodePrefs()->node_name, the_mesh.getBLEPin());

0 commit comments

Comments
 (0)