mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
wifi multipoint v2 array
This commit is contained in:
@@ -98,6 +98,26 @@ bool jsonRead(const String& json, String key, int& value, bool e) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool jsonReadArray(const String& json, String key, JsonArray& jArray, bool e) {
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) {
|
||||
if (e) {
|
||||
SerialPrint("E", F("jsonReadArray"), error.f_str());
|
||||
jsonErrorDetected();
|
||||
}
|
||||
return false;
|
||||
} else if (!doc.containsKey(key)) {
|
||||
if (e) {
|
||||
SerialPrint("E", F("jsonReadArray"), key + " missing in " + json);
|
||||
jsonErrorDetected();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
jArray = doc[key];
|
||||
return true;
|
||||
}
|
||||
|
||||
// new==============================================================================
|
||||
bool jsonWriteStr_(String& json, const String& key, const String& value, bool e) {
|
||||
bool ret = true;
|
||||
|
||||
@@ -3,195 +3,208 @@
|
||||
#define TRIESONE 25 // количество попыток подключения к одной сети из несколких
|
||||
#define TRIES 40 // количество попыток подключения сети если она одна
|
||||
|
||||
void routerConnect() {
|
||||
WiFi.setAutoConnect(false);
|
||||
WiFi.persistent(false);
|
||||
void routerConnect()
|
||||
{
|
||||
WiFi.setAutoConnect(false);
|
||||
WiFi.persistent(false);
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
byte triesOne = TRIESONE;
|
||||
WiFi.mode(WIFI_STA);
|
||||
byte triesOne = TRIESONE;
|
||||
|
||||
String _ssid = jsonReadStr(settingsFlashJson, "routerssid");
|
||||
String _password = jsonReadStr(settingsFlashJson, "routerpass");
|
||||
std::vector<String> _ssidList;
|
||||
std::vector<String> _passwordList;
|
||||
for (size_t i = 0; i < 3; i++)
|
||||
{
|
||||
String _stmp = selectFromMarkerToMarker(_ssid, ",", i);
|
||||
String _ptmp = selectFromMarkerToMarker(_password, ",", i);
|
||||
if (_stmp == "not found" && _ssidList.size() == 0)
|
||||
{
|
||||
triesOne = TRIES;
|
||||
_ssidList.push_back(_ssid);
|
||||
_passwordList.push_back(_password);
|
||||
break;
|
||||
}
|
||||
if (_stmp != "not found")
|
||||
{
|
||||
_ssidList.push_back(_stmp);
|
||||
_passwordList.push_back(_ptmp);
|
||||
}
|
||||
}
|
||||
JsonArray _ssidList;
|
||||
JsonArray _passwordList;
|
||||
jsonReadArray(settingsFlashJson, "routerssid", _ssidList);
|
||||
jsonReadArray(settingsFlashJson, "routerpass", _passwordList);
|
||||
if (_ssidList.size() > 1)
|
||||
triesOne = TRIES;
|
||||
|
||||
if (_ssid == "" && _password == "") {
|
||||
WiFi.begin();
|
||||
} else {
|
||||
WiFi.begin(_ssidList[0].c_str(), _passwordList[0].c_str());
|
||||
if (_passwordList.size() == 0 && _ssidList[0] == "" && _passwordList[0] == "")
|
||||
{
|
||||
WiFi.begin();
|
||||
}
|
||||
else
|
||||
{
|
||||
WiFi.begin(_ssidList[0].as<const char*>(), _passwordList[0].as<const char*>());
|
||||
#ifdef ESP32
|
||||
WiFi.setTxPower(WIFI_POWER_19_5dBm);
|
||||
WiFi.setTxPower(WIFI_POWER_19_5dBm);
|
||||
#else
|
||||
WiFi.setOutputPower(20.5);
|
||||
WiFi.setOutputPower(20.5);
|
||||
#endif
|
||||
|
||||
if (_ssidList.size() > 1)
|
||||
{
|
||||
SerialPrint("i", "WIFI", "ssid list: " + _ssid);
|
||||
SerialPrint("i", "WIFI", "pass list: " + _password);
|
||||
}
|
||||
String _ssid;
|
||||
String _password;
|
||||
for (int8_t i = 0; i < _ssidList.size(); i++)
|
||||
{
|
||||
_ssid = _ssid + _ssidList[i].as<String>() + "; ";
|
||||
}
|
||||
for (size_t i = 0; i < _ssidList.size(); i++) {
|
||||
triesOne = TRIESONE;
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
break;
|
||||
WiFi.begin(_ssidList[i].c_str(), _passwordList[i].c_str());
|
||||
SerialPrint("i", "WIFI", "ssid connect: " + _ssidList[i]);
|
||||
SerialPrint("i", "WIFI", "pass connect: " + _passwordList[i]);
|
||||
while (--triesOne && WiFi.status() != WL_CONNECTED) {
|
||||
for (int8_t i = 0; i < _passwordList.size(); i++)
|
||||
{
|
||||
_password = _password + _passwordList[i].as<String>() + "; ";
|
||||
}
|
||||
SerialPrint("i", "WIFI", "ssid list: " + _ssid);
|
||||
SerialPrint("i", "WIFI", "pass list: " + _password);
|
||||
}
|
||||
for (size_t i = 0; i < _ssidList.size(); i++)
|
||||
{
|
||||
triesOne = TRIESONE;
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
break;
|
||||
WiFi.begin(_ssidList[i].as<const char*>(), _passwordList[i].as<const char*>());
|
||||
SerialPrint("i", "WIFI", "ssid connect: " + _ssidList[i].as<String>());
|
||||
SerialPrint("i", "WIFI", "pass connect: " + _passwordList[i].as<String>());
|
||||
while (--triesOne && WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
// SerialPrint("i", "WIFI", ": " + String((int)WiFi.status()));
|
||||
#ifdef ESP8266
|
||||
if (WiFi.status() == WL_CONNECT_FAILED || WiFi.status() == WL_WRONG_PASSWORD)
|
||||
if (WiFi.status() == WL_CONNECT_FAILED || WiFi.status() == WL_WRONG_PASSWORD)
|
||||
#else
|
||||
if (WiFi.status() == WL_CONNECT_FAILED)
|
||||
#endif
|
||||
{
|
||||
SerialPrint("E", "WIFI", "password is not correct");
|
||||
triesOne = 1;
|
||||
jsonWriteInt(errorsHeapJson, "passer", 1);
|
||||
break;
|
||||
}
|
||||
Serial.print(".");
|
||||
delay(1000);
|
||||
}
|
||||
Serial.println("");
|
||||
if (WiFi.status() == WL_CONNECT_FAILED)
|
||||
#endif
|
||||
{
|
||||
SerialPrint("E", "WIFI", "password is not correct");
|
||||
triesOne = 1;
|
||||
jsonWriteInt(errorsHeapJson, "passer", 1);
|
||||
break;
|
||||
}
|
||||
Serial.print(".");
|
||||
delay(1000);
|
||||
}
|
||||
Serial.println("");
|
||||
}
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println("");
|
||||
startAPMode();
|
||||
} else {
|
||||
Serial.println("");
|
||||
SerialPrint("i", "WIFI", "http://" + WiFi.localIP().toString());
|
||||
jsonWriteStr(settingsFlashJson, "ip", WiFi.localIP().toString());
|
||||
if (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
Serial.println("");
|
||||
startAPMode();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("");
|
||||
SerialPrint("i", "WIFI", "http://" + WiFi.localIP().toString());
|
||||
jsonWriteStr(settingsFlashJson, "ip", WiFi.localIP().toString());
|
||||
|
||||
mqttInit();
|
||||
}
|
||||
SerialPrint("i", F("WIFI"), F("Network Init"));
|
||||
mqttInit();
|
||||
}
|
||||
SerialPrint("i", F("WIFI"), F("Network Init"));
|
||||
}
|
||||
|
||||
bool startAPMode() {
|
||||
SerialPrint("i", "WIFI", "AP Mode");
|
||||
bool startAPMode()
|
||||
{
|
||||
SerialPrint("i", "WIFI", "AP Mode");
|
||||
|
||||
WiFi.disconnect();
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.disconnect();
|
||||
WiFi.mode(WIFI_AP);
|
||||
|
||||
String _ssidAP = jsonReadStr(settingsFlashJson, "apssid");
|
||||
String _passwordAP = jsonReadStr(settingsFlashJson, "appass");
|
||||
String _ssidAP = jsonReadStr(settingsFlashJson, "apssid");
|
||||
String _passwordAP = jsonReadStr(settingsFlashJson, "appass");
|
||||
|
||||
WiFi.softAP(_ssidAP.c_str(), _passwordAP.c_str());
|
||||
IPAddress myIP = WiFi.softAPIP();
|
||||
WiFi.softAP(_ssidAP.c_str(), _passwordAP.c_str());
|
||||
IPAddress myIP = WiFi.softAPIP();
|
||||
|
||||
SerialPrint("i", "WIFI", "AP IP: " + myIP.toString());
|
||||
jsonWriteStr(settingsFlashJson, "ip", myIP.toString());
|
||||
SerialPrint("i", "WIFI", "AP IP: " + myIP.toString());
|
||||
jsonWriteStr(settingsFlashJson, "ip", myIP.toString());
|
||||
|
||||
if (jsonReadInt(errorsHeapJson, "passer") != 1) {
|
||||
ts.add(
|
||||
WIFI_SCAN, 30 * 1000, [&](void*) {
|
||||
String sta_ssid = jsonReadStr(settingsFlashJson, "routerssid");
|
||||
|
||||
SerialPrint("i", "WIFI", "scanning for " + sta_ssid);
|
||||
|
||||
if (RouterFind(sta_ssid)) {
|
||||
ts.remove(WIFI_SCAN);
|
||||
WiFi.scanDelete();
|
||||
routerConnect();
|
||||
}
|
||||
},
|
||||
nullptr, true);
|
||||
}
|
||||
return true;
|
||||
if (jsonReadInt(errorsHeapJson, "passer") != 1)
|
||||
{
|
||||
ts.add(
|
||||
WIFI_SCAN, 30 * 1000,
|
||||
[&](void *)
|
||||
{
|
||||
JsonArray jArray;
|
||||
jsonReadArray(settingsFlashJson, "routerssid", jArray);
|
||||
for (int8_t i = 0; i < jArray.size(); i++)
|
||||
{
|
||||
SerialPrint("i", "WIFI", "scanning for " + jArray[i].as<String>());
|
||||
}
|
||||
if (RouterFind(jArray))
|
||||
{
|
||||
ts.remove(WIFI_SCAN);
|
||||
WiFi.scanDelete();
|
||||
routerConnect();
|
||||
}
|
||||
},
|
||||
nullptr, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean RouterFind(String ssid) {
|
||||
bool res = false;
|
||||
int n = WiFi.scanComplete();
|
||||
SerialPrint("i", "WIFI", "scan result: " + String(n, DEC));
|
||||
boolean RouterFind(JsonArray jArray)
|
||||
{
|
||||
bool res = false;
|
||||
int n = WiFi.scanComplete();
|
||||
SerialPrint("i", "WIFI", "scan result: " + String(n, DEC));
|
||||
|
||||
if (n == -2) { //Сканирование не было запущено, запускаем
|
||||
SerialPrint("i", "WIFI", "start scanning");
|
||||
WiFi.scanNetworks(true, false); // async, show_hidden
|
||||
}
|
||||
if (n == -2)
|
||||
{ // Сканирование не было запущено, запускаем
|
||||
SerialPrint("i", "WIFI", "start scanning");
|
||||
WiFi.scanNetworks(true, false); // async, show_hidden
|
||||
}
|
||||
|
||||
else if (n == -1) { //Сканирование все еще выполняется
|
||||
SerialPrint("i", "WIFI", "scanning in progress");
|
||||
}
|
||||
else if (n == -1)
|
||||
{ // Сканирование все еще выполняется
|
||||
SerialPrint("i", "WIFI", "scanning in progress");
|
||||
}
|
||||
|
||||
else if (n == 0) { //ни одна сеть не найдена
|
||||
SerialPrint("i", "WIFI", "no networks found");
|
||||
WiFi.scanNetworks(true, false);
|
||||
}
|
||||
else if (n > 0)
|
||||
else if (n == 0)
|
||||
{ // ни одна сеть не найдена
|
||||
SerialPrint("i", "WIFI", "no networks found");
|
||||
WiFi.scanNetworks(true, false);
|
||||
}
|
||||
else if (n > 0)
|
||||
{
|
||||
for (int8_t i = 0; i < n; i++)
|
||||
{
|
||||
std::vector<String> _ssidList;
|
||||
for (size_t i = 0; i < 3; i++)
|
||||
for (int8_t k = 0; k < jArray.size(); k++)
|
||||
{
|
||||
if (WiFi.SSID(i) == String(jArray[k]))
|
||||
{
|
||||
String _stmp = selectFromMarkerToMarker(ssid, ",", i);
|
||||
if (_stmp == "not found" && _ssidList.size() == 0)
|
||||
{
|
||||
_ssidList.push_back(ssid);
|
||||
break;
|
||||
}
|
||||
if (_stmp != "not found")
|
||||
_ssidList.push_back(_stmp);
|
||||
res = true;
|
||||
}
|
||||
}
|
||||
// SerialPrint("i", "WIFI", (res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
|
||||
jsonWriteStr_(ssidListHeapJson, String(i), WiFi.SSID(i));
|
||||
|
||||
for (int8_t i = 0; i < n; i++)
|
||||
{
|
||||
if (WiFi.SSID(i) == _ssidList[0] || WiFi.SSID(i) == _ssidList[1] ||
|
||||
WiFi.SSID(i) == _ssidList[2])
|
||||
{
|
||||
res = true;
|
||||
}
|
||||
// SerialPrint("i", "WIFI", (res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
|
||||
jsonWriteStr_(ssidListHeapJson, String(i), WiFi.SSID(i));
|
||||
|
||||
// String(WiFi.RSSI(i)
|
||||
}
|
||||
// String(WiFi.RSSI(i)
|
||||
}
|
||||
SerialPrint("i", "WIFI", ssidListHeapJson);
|
||||
WiFi.scanDelete();
|
||||
return res;
|
||||
}
|
||||
SerialPrint("i", "WIFI", ssidListHeapJson);
|
||||
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";
|
||||
}
|
||||
uint8_t RSSIquality()
|
||||
{
|
||||
uint8_t res = 0;
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
{
|
||||
int rssi = WiFi.RSSI();
|
||||
if (rssi >= -50)
|
||||
{
|
||||
res = 6; //"Excellent";
|
||||
}
|
||||
return res;
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user