diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..889b02bd --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "array": "cpp" + } +} \ No newline at end of file diff --git a/data_esp/config.json b/data_esp/config.json index 6684c7f5..434fc906 100644 --- a/data_esp/config.json +++ b/data_esp/config.json @@ -3,8 +3,8 @@ "chipID": "", "apssid": "IoTmanager", "appass": "", - "routerssid": "Mikro", - "routerpass": "4455667788", + "routerssid": "rise", + "routerpass": "hostel3333", "timezone": 3, "ntp": "pool.ntp.org", "mqttServer": "live-control.ru", @@ -34,4 +34,4 @@ "uartTX": "12", "uartRX": "13", "grafmax": "0" -} +} \ No newline at end of file diff --git a/data_esp/items/items.txt b/data_esp/items/items.txt index bdacaa1a..3cf592d9 100644 --- a/data_esp/items/items.txt +++ b/data_esp/items/items.txt @@ -33,4 +33,5 @@ 0;logging;crtid;chart3;Графики;История;order;val[any];int[23:30];cnt[100];st[0]* 0;uptime;uptid;anydataTime;Системные;%name%#uptime;order;int[60]* 0;sht20;tmpid;anydataTemp;Сенсоры;Температура;1;c[1] -0;sht20;humid;anydataHum;Сенсоры;Влажность;2;c[1];int[50]* \ No newline at end of file +0;sht20;humid;anydataHum;Сенсоры;Влажность;2;c[1];int[50]* +0;sensor;anyid;anydata;Сенсоры;Параметр;1;c[1];int[10];type[type1]* \ No newline at end of file diff --git a/data_esp/set.device.json.gz b/data_esp/set.device.json.gz index a55eb128..28938bb6 100644 Binary files a/data_esp/set.device.json.gz and b/data_esp/set.device.json.gz differ diff --git a/include/Consts.h b/include/Consts.h index 1fde3815..e0d1740a 100644 --- a/include/Consts.h +++ b/include/Consts.h @@ -84,6 +84,7 @@ #define EnableSensorPzem #define EnableSensorUltrasonic #define EnableSensorUptime +#define EnableSensorAny #define EnableTelegram #define EnableUart #endif diff --git a/include/Utils/JsonUtils.h b/include/Utils/JsonUtils.h index 2b19dcc5..f8c60d3e 100644 --- a/include/Utils/JsonUtils.h +++ b/include/Utils/JsonUtils.h @@ -8,6 +8,8 @@ int jsonReadInt(String& json, String name); boolean jsonReadBool(String& json, String name); +float jsonReadFloat(String& json, String name); + String jsonWriteStr(String& json, String name, String value); String jsonWriteInt(String& json, String name, int value); diff --git a/include/YourSensor.h b/include/YourSensor.h new file mode 100644 index 00000000..8549f9fb --- /dev/null +++ b/include/YourSensor.h @@ -0,0 +1,4 @@ +#pragma once + +#include +float yourSensorReading(String type); diff --git a/include/items/vSensorAny.h b/include/items/vSensorAny.h new file mode 100644 index 00000000..65970c93 --- /dev/null +++ b/include/items/vSensorAny.h @@ -0,0 +1,34 @@ +#ifdef EnableSensorAny +#pragma once +#include + +#include "Global.h" + +class SensorAny; + +typedef std::vector MySensorAnyVector; + +class SensorAny { + public: + SensorAny(const String& paramsAny); + ~SensorAny(); + + void loop(); + void read(); + + private: + String _paramsAny; + int _interval; + float _c; + String _key; + String _addr; + String _type; + + unsigned long prevMillis; + unsigned long difference; +}; + +extern MySensorAnyVector* mySensorAny; + +extern void AnySensor(); +#endif \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 57768031..01c01df3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,7 @@ ; https://docs.platformio.org/page/projectconf.html [platformio] -default_envs = esp32_4mb +default_envs = esp8266_4mb data_dir = data_esp [common_env_data] diff --git a/src/BufferExecute.cpp b/src/BufferExecute.cpp index 111a1579..ed677d61 100644 --- a/src/BufferExecute.cpp +++ b/src/BufferExecute.cpp @@ -11,14 +11,15 @@ #include "items/vOutput.h" #include "items/vPwmOut.h" #include "items/vSensorAnalog.h" +#include "items/vSensorAny.h" #include "items/vSensorBme280.h" -#include "items/vSensorSht20.h" #include "items/vSensorBmp280.h" #include "items/vSensorCcs811.h" #include "items/vSensorDallas.h" #include "items/vSensorDht.h" #include "items/vSensorNode.h" #include "items/vSensorPzem.h" +#include "items/vSensorSht20.h" #include "items/vSensorUltrasonic.h" #include "items/vSensorUptime.h" @@ -44,7 +45,7 @@ void loopCmdExecute() { String tmp = selectToMarker(orderBuf, ","); //выделяем первую команду rel 5 1, sCmd.readStr(tmp); //выполняем if (tmp != "") { - sCmd.readStr(tmp); + sCmd.readStr(tmp); SerialPrint("I", F("Order done L"), tmp); } orderBuf = deleteBeforeDelimiter(orderBuf, ","); //осекаем @@ -70,7 +71,7 @@ void csvCmdExecute(String& cmdStr) { count++; if (count > 1) { - //SerialPrint("I", "Items", buf); + // SerialPrint("I", "Items", buf); String order = selectToMarker(buf, " "); //отсечка самой команды if (order == F("button-out")) { #ifdef EnableButtonOut @@ -116,6 +117,10 @@ void csvCmdExecute(String& cmdStr) { } else if (order == F("sht20")) { #ifdef EnableSensorSht20 sCmd.addCommand(order.c_str(), sht20Sensor); +#endif + } else if (order == F("sensor")) { +#ifdef EnableSensorAny + sCmd.addCommand(order.c_str(), AnySensor); #endif } else if (order == F("bmp280")) { #ifdef EnableSensorBmp280 @@ -147,7 +152,7 @@ void csvCmdExecute(String& cmdStr) { #endif } else if (order == F("impuls-in")) { #ifdef EnableImpulsIn - //sCmd.addCommand(order.c_str(), impulsInSensor); + // sCmd.addCommand(order.c_str(), impulsInSensor); #endif } else if (order == F("sensor-node")) { #ifdef EnableSensorNode diff --git a/src/Utils/JsonUtils.cpp b/src/Utils/JsonUtils.cpp index ca70c082..b471e2ac 100644 --- a/src/Utils/JsonUtils.cpp +++ b/src/Utils/JsonUtils.cpp @@ -1,6 +1,7 @@ #include "Utils/JsonUtils.h" -#include "Utils/FileUtils.h" + #include "Global.h" +#include "Utils/FileUtils.h" String jsonReadStr(String& json, String name) { DynamicJsonBuffer jsonBuffer; @@ -20,6 +21,12 @@ int jsonReadInt(String& json, String name) { return root[name]; } +float jsonReadFloat(String& json, String name) { + DynamicJsonBuffer jsonBuffer; + JsonObject& root = jsonBuffer.parseObject(json); + return root[name]; +} + String jsonWriteStr(String& json, String name, String value) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.parseObject(json); diff --git a/src/YourSensor.cpp b/src/YourSensor.cpp new file mode 100644 index 00000000..a3ba9895 --- /dev/null +++ b/src/YourSensor.cpp @@ -0,0 +1,23 @@ +#include "YourSensor.h" + +float yourSensorReading(String type) { + float value; + if (type == "type1") { // type1 - замените на название вашего датчика, потом это же название указывайте в type[], в вебе + //сюда вставляем процедуру чтения одного датчика + static int a; + a++; + value = a; + } else if (type == "type2") { + //сюда вставляем процедуру чтения другого датчика + static int b; + b--; + value = b; + } else if (type == "type3") { + //сюда третьего и так далее, создавайте сколько угодно else if.... + //если у одного датчика несколько параметров то под каждый из них делайте свой else if и свое имя type + static int c; + c++; + value = c * 10; + } + return value; +} diff --git a/src/items/vSensorAny.cpp b/src/items/vSensorAny.cpp new file mode 100644 index 00000000..12917194 --- /dev/null +++ b/src/items/vSensorAny.cpp @@ -0,0 +1,65 @@ +#include "Consts.h" +#ifdef EnableSensorAny +#include + +#include "BufferExecute.h" +#include "Class/LineParsing.h" +#include "Global.h" +#include "YourSensor.h" +#include "items/vSensorAny.h" + +SensorAny::SensorAny(const String& paramsAny) { + _paramsAny = paramsAny; + + _interval = jsonReadInt(_paramsAny, "int"); + _c = jsonReadFloat(_paramsAny, "c"); + _key = jsonReadStr(_paramsAny, "key"); + _addr = jsonReadStr(_paramsAny, "addr"); + _type = jsonReadStr(_paramsAny, "type"); +} + +SensorAny::~SensorAny() {} + +void SensorAny::loop() { + difference = millis() - prevMillis; + if (difference >= _interval * 1000) { + prevMillis = millis(); + read(); + } +} + +void SensorAny::read() { + float value = yourSensorReading(_type); + + value = value * _c; + + eventGen2(_key, String(value)); + jsonWriteStr(configLiveJson, _key, String(value)); + publishStatus(_key, String(value)); + String path = mqttRootDevice + "/" + _key + "/status"; + String json = "{}"; + jsonWriteStr(json, "status", String(value)); + String MyJson = json; + jsonWriteStr(MyJson, "topic", path); + ws.textAll(MyJson); + SerialPrint("I", "Sensor", "'" + _key + "' data: " + String(value)); +} + +MySensorAnyVector* mySensorAny = nullptr; + +void AnySensor() { + String params = "{}"; + myLineParsing.update(); + jsonWriteStr(params, "key", myLineParsing.gkey()); + jsonWriteStr(params, "addr", myLineParsing.gaddr()); + jsonWriteStr(params, "int", myLineParsing.gint()); + jsonWriteStr(params, "c", myLineParsing.gc()); + jsonWriteStr(params, "type", myLineParsing.gtype()); + myLineParsing.clear(); + + static bool firstTime = true; + if (firstTime) mySensorAny = new MySensorAnyVector(); + firstTime = false; + mySensorAny->push_back(SensorAny(params)); +} +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 27755a42..1396b7a3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,6 +24,7 @@ #include "items/vImpulsOut.h" #include "items/vLogging.h" #include "items/vSensorAnalog.h" +#include "items/vSensorAny.h" #include "items/vSensorBme280.h" #include "items/vSensorBmp280.h" #include "items/vSensorCcs811.h" @@ -34,13 +35,12 @@ #include "items/vSensorSht20.h" #include "items/vSensorUltrasonic.h" #include "items/vSensorUptime.h" - //#include "WebServer.h" void not_async_actions(); Timings metric; boolean initialized = false; -extern int flagq ; +extern int flagq; void setup() { Serial.begin(115200); Serial.flush(); @@ -97,12 +97,12 @@ void loop() { #endif #ifdef WEBSOCKET_ENABLED - ws.cleanupClients(); - if ( flagq == 1){ - SerialPrint("I", "WS ", "choose_log_date_and_send()"); - choose_log_date_and_sendWS(); - flagq = 0; -} + ws.cleanupClients(); + if (flagq == 1) { + SerialPrint("I", "WS ", "choose_log_date_and_send()"); + choose_log_date_and_sendWS(); + flagq = 0; + } #endif timeNow->loop(); mqttLoop(); @@ -189,6 +189,13 @@ void loop() { } } #endif +#ifdef EnableSensorAny + if (mySensorAny != nullptr) { + for (unsigned int i = 0; i < mySensorAny->size(); i++) { + mySensorAny->at(i).loop(); + } + } +#endif #ifdef EnableSensorBmp280 if (mySensorBmp280 != nullptr) { for (unsigned int i = 0; i < mySensorBmp280->size(); i++) {