2021-12-22 23:33:47 +01:00
|
|
|
#include "Utils/WiFiUtils.h"
|
|
|
|
|
|
|
|
|
|
void routerConnect() {
|
|
|
|
|
WiFi.setAutoConnect(false);
|
|
|
|
|
WiFi.persistent(false);
|
|
|
|
|
|
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
|
byte tries = 40;
|
|
|
|
|
|
|
|
|
|
String _ssid = jsonReadStr(settingsFlashJson, "routerssid");
|
|
|
|
|
String _password = jsonReadStr(settingsFlashJson, "routerpass");
|
|
|
|
|
|
|
|
|
|
if (_ssid == "" && _password == "") {
|
|
|
|
|
WiFi.begin();
|
|
|
|
|
} else {
|
|
|
|
|
WiFi.begin(_ssid.c_str(), _password.c_str());
|
|
|
|
|
#ifdef ESP32
|
|
|
|
|
WiFi.setTxPower(WIFI_POWER_19_5dBm);
|
|
|
|
|
#else
|
|
|
|
|
WiFi.setOutputPower(20.5);
|
|
|
|
|
#endif
|
2021-12-23 00:33:45 +01:00
|
|
|
SerialPrint("i", "WIFI", "ssid: " + _ssid);
|
|
|
|
|
SerialPrint("i", "WIFI", "pass: " + _password);
|
2021-12-22 23:33:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (--tries && WiFi.status() != WL_CONNECTED) {
|
|
|
|
|
if (WiFi.status() == WL_CONNECT_FAILED) {
|
|
|
|
|
SerialPrint("E", "WIFI", "password is not correct");
|
|
|
|
|
tries = 1;
|
2022-02-08 16:47:17 +01:00
|
|
|
jsonWriteInt(errorsHeapJson, "passer", 1);
|
2021-12-22 23:33:47 +01:00
|
|
|
}
|
|
|
|
|
Serial.print(".");
|
|
|
|
|
delay(1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (WiFi.status() != WL_CONNECTED) {
|
|
|
|
|
Serial.println("");
|
|
|
|
|
startAPMode();
|
|
|
|
|
} else {
|
|
|
|
|
Serial.println("");
|
2021-12-23 00:33:45 +01:00
|
|
|
SerialPrint("i", "WIFI", "http://" + WiFi.localIP().toString());
|
2021-12-22 23:33:47 +01:00
|
|
|
jsonWriteStr(settingsFlashJson, "ip", WiFi.localIP().toString());
|
|
|
|
|
|
2022-02-11 00:22:59 +01:00
|
|
|
mqttInit();
|
2021-12-22 23:33:47 +01:00
|
|
|
}
|
2021-12-23 00:33:45 +01:00
|
|
|
SerialPrint("i", F("WIFI"), F("Network Init"));
|
2021-12-22 23:33:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool startAPMode() {
|
2021-12-23 00:33:45 +01:00
|
|
|
SerialPrint("i", "WIFI", "AP Mode");
|
2021-12-22 23:33:47 +01:00
|
|
|
|
|
|
|
|
WiFi.disconnect();
|
|
|
|
|
WiFi.mode(WIFI_AP);
|
|
|
|
|
|
|
|
|
|
String _ssidAP = jsonReadStr(settingsFlashJson, "apssid");
|
|
|
|
|
String _passwordAP = jsonReadStr(settingsFlashJson, "appass");
|
|
|
|
|
|
|
|
|
|
WiFi.softAP(_ssidAP.c_str(), _passwordAP.c_str());
|
|
|
|
|
IPAddress myIP = WiFi.softAPIP();
|
|
|
|
|
|
2021-12-23 00:33:45 +01:00
|
|
|
SerialPrint("i", "WIFI", "AP IP: " + myIP.toString());
|
2021-12-22 23:33:47 +01:00
|
|
|
jsonWriteStr(settingsFlashJson, "ip", myIP.toString());
|
|
|
|
|
|
2022-02-08 16:47:17 +01:00
|
|
|
if (jsonReadInt(errorsHeapJson, "passer") != 1) {
|
2022-02-06 17:54:02 +01:00
|
|
|
ts.add(
|
|
|
|
|
WIFI_SCAN, 10 * 1000, [&](void*) {
|
|
|
|
|
String sta_ssid = jsonReadStr(settingsFlashJson, "routerssid");
|
2021-12-22 23:33:47 +01:00
|
|
|
|
2022-02-06 17:54:02 +01:00
|
|
|
SerialPrint("i", "WIFI", "scanning for " + sta_ssid);
|
2021-12-22 23:33:47 +01:00
|
|
|
|
2022-02-06 17:54:02 +01:00
|
|
|
if (RouterFind(sta_ssid)) {
|
|
|
|
|
ts.remove(WIFI_SCAN);
|
|
|
|
|
WiFi.scanDelete();
|
|
|
|
|
routerConnect();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
nullptr, true);
|
|
|
|
|
}
|
2021-12-22 23:33:47 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean RouterFind(String ssid) {
|
|
|
|
|
bool res = false;
|
|
|
|
|
int n = WiFi.scanComplete();
|
2021-12-23 00:33:45 +01:00
|
|
|
SerialPrint("i", "WIFI", "scan result: " + String(n, DEC));
|
2021-12-22 23:33:47 +01:00
|
|
|
|
|
|
|
|
if (n == -2) { //Сканирование не было запущено, запускаем
|
2021-12-23 00:33:45 +01:00
|
|
|
SerialPrint("i", "WIFI", "start scanning");
|
2021-12-22 23:33:47 +01:00
|
|
|
WiFi.scanNetworks(true, false); // async, show_hidden
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (n == -1) { //Сканирование все еще выполняется
|
2021-12-23 00:33:45 +01:00
|
|
|
SerialPrint("i", "WIFI", "scanning in progress");
|
2021-12-22 23:33:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (n == 0) { //ни одна сеть не найдена
|
2021-12-23 00:33:45 +01:00
|
|
|
SerialPrint("i", "WIFI", "no networks found");
|
2021-12-22 23:33:47 +01:00
|
|
|
WiFi.scanNetworks(true, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (n > 0) {
|
|
|
|
|
for (int8_t i = 0; i < n; i++) {
|
|
|
|
|
if (WiFi.SSID(i) == ssid) {
|
|
|
|
|
res = true;
|
|
|
|
|
}
|
2022-02-05 02:13:56 +01:00
|
|
|
// SerialPrint("i", "WIFI", (res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
|
2022-02-08 16:47:17 +01:00
|
|
|
jsonWriteStr_(ssidListHeapJson, String(i), WiFi.SSID(i));
|
2022-02-06 17:54:02 +01:00
|
|
|
|
2022-02-05 02:13:56 +01:00
|
|
|
// String(WiFi.RSSI(i)
|
2021-12-22 23:33:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-02-08 16:47:17 +01:00
|
|
|
SerialPrint("i", "WIFI", ssidListHeapJson);
|
2021-12-22 23:33:47 +01:00
|
|
|
WiFi.scanDelete();
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean isNetworkActive() {
|
|
|
|
|
return WiFi.status() == WL_CONNECTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t RSSIquality() {
|
|
|
|
|
uint8_t res = 0;
|
|
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
|
int rssi = WiFi.RSSI();
|
|
|
|
|
if (rssi >= -50) {
|
|
|
|
|
res = 6; //"Excellent";
|
|
|
|
|
} else if (rssi < -50 && rssi >= -60) {
|
|
|
|
|
res = 5; //"Very good";
|
|
|
|
|
} else if (rssi < -60 && rssi >= -70) {
|
|
|
|
|
res = 4; //"Good";
|
|
|
|
|
} else if (rssi < -70 && rssi >= -80) {
|
|
|
|
|
res = 3; //"Low";
|
|
|
|
|
} else if (rssi < -80 && rssi > -100) {
|
|
|
|
|
res = 2; //"Very low";
|
|
|
|
|
} else if (rssi <= -100) {
|
|
|
|
|
res = 1; //"No signal";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|