2020-09-02 22:34:49 +03:00
|
|
|
#include "Utils/WiFiUtils.h"
|
|
|
|
|
|
2020-09-17 17:27:44 +03:00
|
|
|
|
2020-09-02 22:34:49 +03:00
|
|
|
|
|
|
|
|
void startSTAMode() {
|
|
|
|
|
setLedStatus(LED_SLOW);
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("I","module","STA Mode");
|
2020-09-02 22:34:49 +03:00
|
|
|
|
|
|
|
|
String ssid = jsonReadStr(configSetupJson, "routerssid");
|
|
|
|
|
String passwd = jsonReadStr(configSetupJson, "routerpass");
|
|
|
|
|
|
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
|
if (ssid == "" && passwd == "") {
|
|
|
|
|
WiFi.begin();
|
|
|
|
|
} else {
|
|
|
|
|
if (WiFi.begin(ssid.c_str(), passwd.c_str()) == WL_CONNECT_FAILED) {
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("[E]","module","failed on start");
|
2020-09-02 22:34:49 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool keepConnecting = true;
|
|
|
|
|
uint8_t tries = 20;
|
|
|
|
|
sint8_t connRes;
|
|
|
|
|
do {
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
|
connRes = WiFi.waitForConnectResult(1000);
|
|
|
|
|
#else
|
|
|
|
|
byte connRes = WiFi.waitForConnectResult();
|
|
|
|
|
#endif
|
|
|
|
|
switch (connRes) {
|
|
|
|
|
case WL_NO_SSID_AVAIL: {
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("[E]","module","no network");
|
2020-09-02 22:34:49 +03:00
|
|
|
keepConnecting = false;
|
|
|
|
|
} break;
|
|
|
|
|
case WL_CONNECTED: {
|
|
|
|
|
String hostIpStr = WiFi.localIP().toString();
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("I","module","http://" + hostIpStr);
|
2020-09-02 22:34:49 +03:00
|
|
|
jsonWriteStr(configSetupJson, "ip", hostIpStr);
|
|
|
|
|
keepConnecting = false;
|
|
|
|
|
} break;
|
|
|
|
|
case WL_CONNECT_FAILED: {
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("[E]","module","check credentials");
|
2020-09-02 22:34:49 +03:00
|
|
|
jsonWriteInt(configOptionJson, "pass_status", 1);
|
|
|
|
|
keepConnecting = false;
|
|
|
|
|
} break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} while (keepConnecting && tries--);
|
|
|
|
|
|
|
|
|
|
if (isNetworkActive()) {
|
|
|
|
|
MqttClient::init();
|
|
|
|
|
setLedStatus(LED_OFF);
|
|
|
|
|
} else {
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("[E]","module","failed: " + String(connRes, DEC));
|
2020-09-02 22:34:49 +03:00
|
|
|
startAPMode();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool startAPMode() {
|
|
|
|
|
setLedStatus(LED_ON);
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("I","module","AP Mode");
|
2020-09-02 22:34:49 +03:00
|
|
|
|
|
|
|
|
String ssid = jsonReadStr(configSetupJson, "apssid");
|
|
|
|
|
String passwd = jsonReadStr(configSetupJson, "appass");
|
|
|
|
|
|
|
|
|
|
WiFi.mode(WIFI_AP);
|
|
|
|
|
|
|
|
|
|
WiFi.softAP(ssid.c_str(), passwd.c_str());
|
|
|
|
|
String hostIpStr = WiFi.softAPIP().toString();
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("I","module","Host IP: " + hostIpStr);
|
2020-09-02 22:34:49 +03:00
|
|
|
jsonWriteStr(configSetupJson, "ip", hostIpStr);
|
|
|
|
|
|
|
|
|
|
ts.add(
|
|
|
|
|
WIFI_SCAN, 10 * 1000,
|
|
|
|
|
[&](void*) {
|
|
|
|
|
String sta_ssid = jsonReadStr(configSetupJson, "routerssid");
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("I","module","scanning for " + sta_ssid);
|
2020-09-02 22:34:49 +03:00
|
|
|
if (scanWiFi(sta_ssid)) {
|
|
|
|
|
ts.remove(WIFI_SCAN);
|
|
|
|
|
startSTAMode();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
nullptr, true);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean scanWiFi(String ssid) {
|
|
|
|
|
bool res = false;
|
|
|
|
|
int8_t n = WiFi.scanComplete();
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("I","module","scan result: " + String(n, DEC));
|
2020-09-02 22:34:49 +03:00
|
|
|
if (n == -2) {
|
|
|
|
|
// не было запущено, запускаем
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("I","module","start scanning");
|
2020-09-02 22:34:49 +03:00
|
|
|
// async, show_hidden
|
|
|
|
|
WiFi.scanNetworks(true, false);
|
|
|
|
|
} else if (n == -1) {
|
|
|
|
|
// все еще выполняется
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("I","module","scanning in progress");
|
2020-09-02 22:34:49 +03:00
|
|
|
} else if (n == 0) {
|
|
|
|
|
// не найдена ни одна сеть
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("I","module","no networks found");
|
2020-09-02 22:34:49 +03:00
|
|
|
WiFi.scanNetworks(true, false);
|
|
|
|
|
} else if (n > 0) {
|
|
|
|
|
for (int8_t i = 0; i < n; i++) {
|
|
|
|
|
if (WiFi.SSID(i) == ssid) {
|
|
|
|
|
res = true;
|
|
|
|
|
}
|
2020-09-17 17:27:44 +03:00
|
|
|
SerialPrint("I","module",(res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
|
2020-09-02 22:34:49 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean isNetworkActive() {
|
|
|
|
|
return WiFi.status() == WL_CONNECTED;
|
|
|
|
|
}
|