Files
IoTManager/src/Utils/WiFiUtils.cpp

119 lines
3.4 KiB
C++
Raw Normal View History

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 18:04:06 +03:00
SerialPrint("I","WIFI","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 18:04:06 +03:00
SerialPrint("[E]","WIFI","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 18:04:06 +03:00
SerialPrint("[E]","WIFI","no network");
2020-09-02 22:34:49 +03:00
keepConnecting = false;
} break;
case WL_CONNECTED: {
String hostIpStr = WiFi.localIP().toString();
2020-09-17 18:04:06 +03:00
SerialPrint("I","WIFI","http://" + hostIpStr);
2020-09-02 22:34:49 +03:00
jsonWriteStr(configSetupJson, "ip", hostIpStr);
keepConnecting = false;
} break;
case WL_CONNECT_FAILED: {
2020-09-17 18:04:06 +03:00
SerialPrint("[E]","WIFI","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 18:04:06 +03:00
SerialPrint("[E]","WIFI","failed: " + String(connRes, DEC));
2020-09-02 22:34:49 +03:00
startAPMode();
};
}
bool startAPMode() {
setLedStatus(LED_ON);
2020-09-17 18:04:06 +03:00
SerialPrint("I","WIFI","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 18:04:06 +03:00
SerialPrint("I","WIFI","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 18:04:06 +03:00
SerialPrint("I","WIFI","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 18:04:06 +03:00
SerialPrint("I","WIFI","scan result: " + String(n, DEC));
2020-09-02 22:34:49 +03:00
if (n == -2) {
// не было запущено, запускаем
2020-09-17 18:04:06 +03:00
SerialPrint("I","WIFI","start scanning");
2020-09-02 22:34:49 +03:00
// async, show_hidden
WiFi.scanNetworks(true, false);
} else if (n == -1) {
// все еще выполняется
2020-09-17 18:04:06 +03:00
SerialPrint("I","WIFI","scanning in progress");
2020-09-02 22:34:49 +03:00
} else if (n == 0) {
// не найдена ни одна сеть
2020-09-17 18:04:06 +03:00
SerialPrint("I","WIFI","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 18:04:06 +03:00
SerialPrint("I","WIFI",(res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
2020-09-02 22:34:49 +03:00
}
}
return res;
}
boolean isNetworkActive() {
return WiFi.status() == WL_CONNECTED;
}