Merge pull request #97 from DmitryBorisenko33/ver3

Возможность добавлять любой сенсор
This commit is contained in:
Dmitry Borisenko
2021-12-15 00:24:59 +01:00
committed by GitHub
14 changed files with 172 additions and 18 deletions

5
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,5 @@
{
"files.associations": {
"array": "cpp"
}
}

View File

@@ -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"
}
}

View File

@@ -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]*
0;sht20;humid;anydataHum;Сенсоры;Влажность;2;c[1];int[50]*
0;sensor;anyid;anydata;Сенсоры;Параметр;1;c[1];int[10];type[type1]*

Binary file not shown.

View File

@@ -84,6 +84,7 @@
#define EnableSensorPzem
#define EnableSensorUltrasonic
#define EnableSensorUptime
#define EnableSensorAny
#define EnableTelegram
#define EnableUart
#endif

View File

@@ -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);

4
include/YourSensor.h Normal file
View File

@@ -0,0 +1,4 @@
#pragma once
#include <Arduino.h>
float yourSensorReading(String type);

View File

@@ -0,0 +1,34 @@
#ifdef EnableSensorAny
#pragma once
#include <Arduino.h>
#include "Global.h"
class SensorAny;
typedef std::vector<SensorAny> 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

View File

@@ -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]

View File

@@ -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

View File

@@ -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);

23
src/YourSensor.cpp Normal file
View File

@@ -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;
}

65
src/items/vSensorAny.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include "Consts.h"
#ifdef EnableSensorAny
#include <Arduino.h>
#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

View File

@@ -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++) {