diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index 4d50f306..515063e8 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -19,28 +19,35 @@ uint8_t _wifi_ap_mode = WIFI_AP_FALLBACK; // PRIVATE // ----------------------------------------------------------------------------- -void _wifiCheckAP() { +void _wifiUpdateSoftAP() { + if (WiFi.softAPgetStationNum() == 0) { + #if USE_PASSWORD + jw.setSoftAP(getSetting("hostname").c_str(), getAdminPass().c_str()); + #else + jw.setSoftAP(getSetting("hostname").c_str()); + #endif + } +} - if ((WIFI_AP_FALLBACK == _wifi_ap_mode) && - (jw.connected()) && - ((WiFi.getMode() & WIFI_AP) > 0) && - (WiFi.softAPgetStationNum() == 0) +void _wifiCheckAP() { + if ( + (WIFI_AP_FALLBACK == _wifi_ap_mode) + && ((WiFi.getMode() & WIFI_AP) > 0) + && jw.connected() + && (WiFi.softAPgetStationNum() == 0) ) { - jw.enableAP(false); + jw.enableAP(false); } - } void _wifiConfigure() { jw.setHostname(getSetting("hostname").c_str()); - #if USE_PASSWORD - jw.setSoftAP(getSetting("hostname").c_str(), getAdminPass().c_str()); - #else - jw.setSoftAP(getSetting("hostname").c_str()); - #endif + _wifiUpdateSoftAP(); + jw.setConnectTimeout(WIFI_CONNECT_TIMEOUT); wifiReconnectCheck(); + jw.enableAPFallback(WIFI_FALLBACK_APMODE); jw.cleanNetworks(); @@ -370,6 +377,7 @@ void _wifiDebugCallback(justwifi_messages_t code, char * parameter) { } if (code == MESSAGE_ACCESSPOINT_DESTROYED) { + _wifiUpdateSoftAP(); DEBUG_MSG_P(PSTR("[WIFI] Access point destroyed\n")); } @@ -424,6 +432,11 @@ void _wifiInitCommands() { terminalOK(); }); + terminalRegisterCommand(F("WIFI.STA"), [](Embedis* e) { + wifiStartSTA(); + terminalOK(); + }); + terminalRegisterCommand(F("WIFI.AP"), [](Embedis* e) { wifiStartAP(); terminalOK(); @@ -495,6 +508,32 @@ void _wifiWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& // INFO // ----------------------------------------------------------------------------- +// backported WiFiAPClass methods + +String _wifiSoftAPSSID() { + struct softap_config config; + wifi_softap_get_config(&config); + + char* name = reinterpret_cast(config.ssid); + char ssid[sizeof(config.ssid) + 1]; + memcpy(ssid, name, sizeof(config.ssid)); + ssid[sizeof(config.ssid)] = '\0'; + + return String(ssid); +} + +String _wifiSoftAPPSK() { + struct softap_config config; + wifi_softap_get_config(&config); + + char* pass = reinterpret_cast(config.password); + char psk[sizeof(config.password) + 1]; + memcpy(psk, pass, sizeof(config.password)); + psk[sizeof(config.password)] = '\0'; + + return String(psk); +} + void wifiDebug(WiFiMode_t modes) { #if DEBUG_SUPPORT @@ -519,8 +558,8 @@ void wifiDebug(WiFiMode_t modes) { if (((modes & WIFI_AP) > 0) && ((WiFi.getMode() & WIFI_AP) > 0)) { DEBUG_MSG_P(PSTR("[WIFI] -------------------------------------- MODE AP\n")); - DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), getSetting("hostname").c_str()); - DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getAdminPass().c_str()); + DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), _wifiSoftAPSSID().c_str()); + DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), _wifiSoftAPPSK().c_str()); DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str()); DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str()); footer = true; @@ -569,6 +608,12 @@ void wifiDisconnect() { jw.disconnect(); } +void wifiStartSTA() { + jw.disconnect(); + jw.enableSTA(true); + jw.enableAP(false); +} + void wifiStartAP(bool only) { if (only) { jw.enableSTA(false); diff --git a/code/platformio.ini b/code/platformio.ini index 672455f7..ccde8f0b 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -243,6 +243,11 @@ upload_flags = ${common.ota_upload_flags} board = ${common.board_4m} build_flags = ${common.build_flags_4m1m} -DNODEMCU_LOLIN -DDEBUG_FAUXMO=Serial -DNOWSAUTH +[env:nodemcu-lolin-252] +platform = ${common.arduino_core_2_5_2} +board = ${common.board_4m} +build_flags = ${common.build_flags_4m1m} -DNODEMCU_LOLIN -DDEBUG_FAUXMO=Serial -DNOWSAUTH + [env:nodemcu-lolin-ssl] platform = ${common.platform_latest} board = ${common.board_4m}