|
2 | 2 |
|
3 | 3 | #include <Arduino.h> // needed for PlatformIO |
4 | 4 | #include <Mesh.h> |
| 5 | +#ifdef WIFI_SSID |
| 6 | + #include <WiFi.h> |
| 7 | +#endif |
5 | 8 |
|
6 | 9 | #define CMD_APP_START 1 |
7 | 10 | #define CMD_SEND_TXT_MSG 2 |
|
141 | 144 | #define AUTO_ADD_ROOM_SERVER (1 << 3) // 0x08 - auto-add Room Server (ADV_TYPE_ROOM) |
142 | 145 | #define AUTO_ADD_SENSOR (1 << 4) // 0x10 - auto-add Sensor (ADV_TYPE_SENSOR) |
143 | 146 |
|
| 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 | + |
144 | 166 | void MyMesh::writeOKFrame() { |
145 | 167 | uint8_t buf[1]; |
146 | 168 | buf[0] = RESP_CODE_OK; |
@@ -895,6 +917,15 @@ void MyMesh::begin(bool has_display) { |
895 | 917 |
|
896 | 918 | // load persisted prefs |
897 | 919 | _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 |
898 | 929 |
|
899 | 930 | // sanitise bad pref values |
900 | 931 | _prefs.rx_delay_base = constrain(_prefs.rx_delay_base, 0, 20.0f); |
@@ -1911,6 +1942,42 @@ void MyMesh::enterCLIRescue() { |
1911 | 1942 | Serial.println("========= CLI Rescue ========="); |
1912 | 1943 | } |
1913 | 1944 |
|
| 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 | + |
1914 | 1981 | void MyMesh::checkCLIRescueCmd() { |
1915 | 1982 | int len = strlen(cli_command); |
1916 | 1983 | while (Serial.available() && len < sizeof(cli_command)-1) { |
@@ -2115,6 +2182,32 @@ void MyMesh::loop() { |
2115 | 2182 |
|
2116 | 2183 | if (_cli_rescue) { |
2117 | 2184 | 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 |
2118 | 2211 | } else { |
2119 | 2212 | checkSerialInterface(); |
2120 | 2213 | } |
|
0 commit comments