Merge branch 'ver4dev' into v4dev

This commit is contained in:
2023-10-03 22:04:54 +03:00
committed by GitHub
113 changed files with 5761 additions and 2275 deletions

View File

@@ -2,26 +2,34 @@
#include "utils/FileUtils.h"
// new================================================================================
String jsonReadStrDoc(DynamicJsonDocument& doc, String name) {
String jsonReadStrDoc(DynamicJsonDocument &doc, String name)
{
return doc[name].as<String>();
}
void jsonWriteStrDoc(DynamicJsonDocument& doc, String name, String value) {
void jsonWriteStrDoc(DynamicJsonDocument &doc, String name, String value)
{
doc[name] = value;
}
// new==============================================================================
bool jsonRead(const String& json, String key, long& value, bool e) {
bool jsonRead(const String &json, String key, long &value, bool e)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
return false;
} else if (!doc.containsKey(key)) {
if (e) {
}
else if (!doc.containsKey(key))
{
if (e)
{
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
jsonErrorDetected();
}
@@ -31,17 +39,23 @@ bool jsonRead(const String& json, String key, long& value, bool e) {
return true;
}
bool jsonRead(const String& json, String key, float& value, bool e) {
bool jsonRead(const String &json, String key, float &value, bool e)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
return false;
} else if (!doc.containsKey(key)) {
if (e) {
}
else if (!doc.containsKey(key))
{
if (e)
{
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
jsonErrorDetected();
}
@@ -51,17 +65,23 @@ bool jsonRead(const String& json, String key, float& value, bool e) {
return true;
}
bool jsonRead(const String& json, String key, String& value, bool e) {
bool jsonRead(const String &json, String key, String &value, bool e)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
return false;
} else if (!doc.containsKey(key)) {
if (e) {
}
else if (!doc.containsKey(key))
{
if (e)
{
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
jsonErrorDetected();
}
@@ -71,24 +91,31 @@ bool jsonRead(const String& json, String key, String& value, bool e) {
return true;
}
bool jsonRead(const String& json, String key, bool& value, bool e) {
bool jsonRead(const String &json, String key, bool &value, bool e)
{
int lvalue = value;
bool ret = jsonRead(json, key, lvalue, e);
value = lvalue;
return ret;
}
bool jsonRead(const String& json, String key, int& value, bool e) {
bool jsonRead(const String &json, String key, int &value, bool e)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
return false;
} else if (!doc.containsKey(key)) {
if (e) {
}
else if (!doc.containsKey(key))
{
if (e)
{
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
jsonErrorDetected();
}
@@ -98,13 +125,59 @@ bool jsonRead(const String& json, String key, int& value, bool e) {
return true;
}
bool jsonReadArray(const String &json, String key, std::vector<String> &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;
}
// SerialPrint("E", F("jsonReadArray"), key + " doc " + doc[key].as<String>());
if (doc[key].is<JsonArray>())
{
for (int8_t i = 0; i < doc[key].size(); i++)
jArray.push_back(doc[key][i].as<String>());
// SerialPrint("E", F("jsonReadArray"), "isArray"+key + " doc " + doc[key].as<String>());
}
else
{
jArray.push_back(doc[key].as<String>());
// DynamicJsonDocument docArr(JSON_BUFFER_SIZE/5);
// jArray = doc[key].as<JsonArray>();
// String tmp = doc[key].as<String>();
// jArray.add("dsdsd");
// SerialPrint("E", F("jsonReadArray"), "notArray"+key + " doc " + doc[key].as<String>());
// SerialPrint("E", F("jsonReadArray"), "count: " + String(jArray.size()) +" key: " + key + " arr " + jArray[0]);
}
// SerialPrint("E", F("jsonReadArray"), "count: " + String(jArray.size()) +" key: " + key + " doc " + jArray[0].as<String>());
return true;
}
// new==============================================================================
bool jsonWriteStr_(String& json, const String& key, const String& value, bool e) {
bool jsonWriteStr_(String &json, const String &key, const String &value, bool e)
{
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
@@ -116,12 +189,15 @@ bool jsonWriteStr_(String& json, const String& key, const String& value, bool e)
return ret;
}
bool jsonWriteBool_(String& json, const String& key, bool value, bool e) {
bool jsonWriteBool_(String &json, const String &key, bool value, bool e)
{
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
@@ -133,12 +209,15 @@ bool jsonWriteBool_(String& json, const String& key, bool value, bool e) {
return ret;
}
bool jsonWriteInt_(String& json, const String& key, int value, bool e) {
bool jsonWriteInt_(String &json, const String &key, int value, bool e)
{
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
@@ -150,12 +229,15 @@ bool jsonWriteInt_(String& json, const String& key, int value, bool e) {
return ret;
}
bool jsonWriteFloat_(String& json, const String &key, float value, bool e) {
bool jsonWriteFloat_(String &json, const String &key, float value, bool e)
{
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
@@ -167,24 +249,29 @@ bool jsonWriteFloat_(String& json, const String &key, float value, bool e) {
return ret;
}
void writeUint8tValueToJsonString(uint8_t* payload, size_t length, size_t headerLenth, String& json) {
void writeUint8tValueToJsonString(uint8_t *payload, size_t length, size_t headerLenth, String &json)
{
String payloadStr;
payloadStr.reserve(length + 1);
for (size_t i = headerLenth; i < length; i++) {
for (size_t i = headerLenth; i < length; i++)
{
payloadStr += (char)payload[i];
}
jsonMergeObjects(json, payloadStr);
}
bool jsonMergeObjects(String& json1, String& json2, bool e) {
bool jsonMergeObjects(String &json1, String &json2, bool e)
{
bool ret = true;
DynamicJsonDocument doc1(JSON_BUFFER_SIZE);
DeserializationError error1 = deserializeJson(doc1, json1);
DynamicJsonDocument doc2(JSON_BUFFER_SIZE);
DeserializationError error2 = deserializeJson(doc2, json2);
jsonMergeDocs(doc1.as<JsonObject>(), doc2.as<JsonObject>());
if (error1 || error2) {
if (e) {
if (error1 || error2)
{
if (e)
{
SerialPrint("E", F("json"), "jsonMergeObjects error");
jsonErrorDetected();
}
@@ -195,18 +282,23 @@ bool jsonMergeObjects(String& json1, String& json2, bool e) {
return ret;
}
void jsonMergeDocs(JsonObject dest, JsonObjectConst src) {
for (auto kvp : src) {
void jsonMergeDocs(JsonObject dest, JsonObjectConst src)
{
for (auto kvp : src)
{
dest[kvp.key()] = kvp.value();
}
}
// depricated======================================================================
String jsonReadStr(const String& json, String name, bool e) {
String jsonReadStr(const String &json, String name, bool e)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
@@ -214,11 +306,14 @@ String jsonReadStr(const String& json, String name, bool e) {
return doc[name].as<String>();
}
boolean jsonReadBool(const String& json, String name, bool e) {
boolean jsonReadBool(const String &json, String name, bool e)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
@@ -226,11 +321,14 @@ boolean jsonReadBool(const String& json, String name, bool e) {
return doc[name].as<bool>();
}
int jsonReadInt(const String& json, String name, bool e) {
int jsonReadInt(const String &json, String name, bool e)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
@@ -238,11 +336,14 @@ int jsonReadInt(const String& json, String name, bool e) {
return doc[name].as<int>();
}
long int jsonReadLInt(const String& json, String name, bool e) {
long int jsonReadLInt(const String &json, String name, bool e)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonRead"), error.f_str());
jsonErrorDetected();
}
@@ -251,11 +352,14 @@ long int jsonReadLInt(const String& json, String name, bool e) {
}
// depricated========================================================================
String jsonWriteStr(String& json, String name, String value, bool e) {
String jsonWriteStr(String &json, String name, String value, bool e)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
@@ -266,11 +370,14 @@ String jsonWriteStr(String& json, String name, String value, bool e) {
return json;
}
String jsonWriteBool(String& json, String name, boolean value, bool e) {
String jsonWriteBool(String &json, String name, boolean value, bool e)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
@@ -281,11 +388,14 @@ String jsonWriteBool(String& json, String name, boolean value, bool e) {
return json;
}
String jsonWriteInt(String& json, String name, int value, bool e) {
String jsonWriteInt(String &json, String name, int value, bool e)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
@@ -296,11 +406,14 @@ String jsonWriteInt(String& json, String name, int value, bool e) {
return json;
}
String jsonWriteFloat(String& json, String name, float value, bool e) {
String jsonWriteFloat(String &json, String name, float value, bool e)
{
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (e) {
if (error)
{
if (e)
{
SerialPrint("E", F("jsonWrite"), error.f_str());
jsonErrorDetected();
}
@@ -311,7 +424,8 @@ String jsonWriteFloat(String& json, String name, float value, bool e) {
return json;
}
void jsonErrorDetected() {
void jsonErrorDetected()
{
// пример как отправить ошибку с количеством
// jsonWriteInt(errorsHeapJson, F("jse2"), 1);
// int number = jsonReadInt(errorsHeapJson, F("jse2n"));

View File

@@ -34,7 +34,7 @@ void updateDeviceStatus() {
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
ret = httpResponseCode;
ret = http.errorToString(httpResponseCode).c_str();
if (httpResponseCode == HTTP_CODE_OK) {
String payload = http.getString();
ret += " " + payload;

View File

@@ -1,118 +1,174 @@
#include "utils/WiFiUtils.h"
#include <vector>
#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 tries = 40;
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;
jsonReadArray(settingsFlashJson, "routerssid", _ssidList);
jsonReadArray(settingsFlashJson, "routerpass", _passwordList);
if (_ssidList.size() > 1)
triesOne = TRIES;
if (_ssid == "" && _password == "") {
WiFi.begin();
} else {
WiFi.begin(_ssid.c_str(), _password.c_str());
if (_passwordList.size() == 0 && _ssidList[0] == "" && _passwordList[0] == "")
{
WiFi.begin();
}
else
{
WiFi.begin(_ssidList[0].c_str(), _passwordList[0].c_str());
#ifdef ESP32
WiFi.setTxPower(WIFI_POWER_19_5dBm);
WiFi.setTxPower(WIFI_POWER_19_5dBm);
#else
WiFi.setOutputPower(20.5);
WiFi.setOutputPower(20.5);
#endif
SerialPrint("i", "WIFI", "ssid: " + _ssid);
SerialPrint("i", "WIFI", "pass: " + _password);
String _ssid;
String _password;
for (int8_t i = 0; i < _ssidList.size(); i++)
{
_ssid = _ssid + _ssidList[i] + "; ";
}
while (--tries && WiFi.status() != WL_CONNECTED) {
if (WiFi.status() == WL_CONNECT_FAILED) {
SerialPrint("E", "WIFI", "password is not correct");
tries = 1;
jsonWriteInt(errorsHeapJson, "passer", 1);
break;
}
Serial.print(".");
delay(1000);
for (int8_t i = 0; i < _passwordList.size(); i++)
{
_password = _password + _passwordList[i] + "; ";
}
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", "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].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)
{
// SerialPrint("i", "WIFI", ": " + String((int)WiFi.status()));
#ifdef ESP8266
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);
}
SerialPrint("i", F("WIFI"), F("Network Init"));
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());
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 *)
{
std::vector<String> jArray;
jsonReadArray(settingsFlashJson, "routerssid", jArray);
for (int8_t i = 0; i < jArray.size(); i++)
{
SerialPrint("i", "WIFI", "scanning for " + jArray[i]);
}
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(std::vector<String> 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) {
for (int8_t i = 0; i < n; i++) {
if (WiFi.SSID(i) == ssid) {
res = true;
}
// SerialPrint("i", "WIFI", (res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
jsonWriteStr_(ssidListHeapJson, String(i), WiFi.SSID(i));
// String(WiFi.RSSI(i)
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++)
{
for (int8_t k = 0; k < jArray.size(); k++)
{
if (WiFi.SSID(i) == jArray[k])
{
res = true;
}
}
// SerialPrint("i", "WIFI", (res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
jsonWriteStr_(ssidListHeapJson, String(i), WiFi.SSID(i));
// String(WiFi.RSSI(i)
}
SerialPrint("i", "WIFI", ssidListHeapJson);
WiFi.scanDelete();
return res;
}
SerialPrint("i", "WIFI", ssidListHeapJson);
WiFi.scanDelete();
return res;
}
// boolean isNetworkActive() {