mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 11:59:12 +03:00
добавил шаблон любого сенсора
This commit is contained in:
@@ -84,6 +84,7 @@
|
|||||||
#define EnableSensorPzem
|
#define EnableSensorPzem
|
||||||
#define EnableSensorUltrasonic
|
#define EnableSensorUltrasonic
|
||||||
#define EnableSensorUptime
|
#define EnableSensorUptime
|
||||||
|
#define EnableSensorAny
|
||||||
#define EnableTelegram
|
#define EnableTelegram
|
||||||
#define EnableUart
|
#define EnableUart
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ int jsonReadInt(String& json, String name);
|
|||||||
|
|
||||||
boolean jsonReadBool(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 jsonWriteStr(String& json, String name, String value);
|
||||||
|
|
||||||
String jsonWriteInt(String& json, String name, int value);
|
String jsonWriteInt(String& json, String name, int value);
|
||||||
|
|||||||
34
include/items/vSensorAny.h
Normal file
34
include/items/vSensorAny.h
Normal 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
|
||||||
4
include/sensors/YourSensor.h
Normal file
4
include/sensors/YourSensor.h
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
float yourSensorReading(String type);
|
||||||
@@ -11,14 +11,15 @@
|
|||||||
#include "items/vOutput.h"
|
#include "items/vOutput.h"
|
||||||
#include "items/vPwmOut.h"
|
#include "items/vPwmOut.h"
|
||||||
#include "items/vSensorAnalog.h"
|
#include "items/vSensorAnalog.h"
|
||||||
|
#include "items/vSensorAny.h"
|
||||||
#include "items/vSensorBme280.h"
|
#include "items/vSensorBme280.h"
|
||||||
#include "items/vSensorSht20.h"
|
|
||||||
#include "items/vSensorBmp280.h"
|
#include "items/vSensorBmp280.h"
|
||||||
#include "items/vSensorCcs811.h"
|
#include "items/vSensorCcs811.h"
|
||||||
#include "items/vSensorDallas.h"
|
#include "items/vSensorDallas.h"
|
||||||
#include "items/vSensorDht.h"
|
#include "items/vSensorDht.h"
|
||||||
#include "items/vSensorNode.h"
|
#include "items/vSensorNode.h"
|
||||||
#include "items/vSensorPzem.h"
|
#include "items/vSensorPzem.h"
|
||||||
|
#include "items/vSensorSht20.h"
|
||||||
#include "items/vSensorUltrasonic.h"
|
#include "items/vSensorUltrasonic.h"
|
||||||
#include "items/vSensorUptime.h"
|
#include "items/vSensorUptime.h"
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ void loopCmdExecute() {
|
|||||||
String tmp = selectToMarker(orderBuf, ","); //выделяем первую команду rel 5 1,
|
String tmp = selectToMarker(orderBuf, ","); //выделяем первую команду rel 5 1,
|
||||||
sCmd.readStr(tmp); //выполняем
|
sCmd.readStr(tmp); //выполняем
|
||||||
if (tmp != "") {
|
if (tmp != "") {
|
||||||
sCmd.readStr(tmp);
|
sCmd.readStr(tmp);
|
||||||
SerialPrint("I", F("Order done L"), tmp);
|
SerialPrint("I", F("Order done L"), tmp);
|
||||||
}
|
}
|
||||||
orderBuf = deleteBeforeDelimiter(orderBuf, ","); //осекаем
|
orderBuf = deleteBeforeDelimiter(orderBuf, ","); //осекаем
|
||||||
@@ -70,7 +71,7 @@ void csvCmdExecute(String& cmdStr) {
|
|||||||
count++;
|
count++;
|
||||||
|
|
||||||
if (count > 1) {
|
if (count > 1) {
|
||||||
//SerialPrint("I", "Items", buf);
|
// SerialPrint("I", "Items", buf);
|
||||||
String order = selectToMarker(buf, " "); //отсечка самой команды
|
String order = selectToMarker(buf, " "); //отсечка самой команды
|
||||||
if (order == F("button-out")) {
|
if (order == F("button-out")) {
|
||||||
#ifdef EnableButtonOut
|
#ifdef EnableButtonOut
|
||||||
@@ -115,6 +116,10 @@ void csvCmdExecute(String& cmdStr) {
|
|||||||
} else if (order == F("sht20")) {
|
} else if (order == F("sht20")) {
|
||||||
#ifdef EnableSensorSht20
|
#ifdef EnableSensorSht20
|
||||||
sCmd.addCommand(order.c_str(), sht20Sensor);
|
sCmd.addCommand(order.c_str(), sht20Sensor);
|
||||||
|
#endif
|
||||||
|
} else if (order == F("sensor")) {
|
||||||
|
#ifdef EnableSensorAny
|
||||||
|
sCmd.addCommand(order.c_str(), AnySensor);
|
||||||
#endif
|
#endif
|
||||||
} else if (order == F("bmp280")) {
|
} else if (order == F("bmp280")) {
|
||||||
#ifdef EnableSensorBmp280
|
#ifdef EnableSensorBmp280
|
||||||
@@ -146,7 +151,7 @@ void csvCmdExecute(String& cmdStr) {
|
|||||||
#endif
|
#endif
|
||||||
} else if (order == F("impuls-in")) {
|
} else if (order == F("impuls-in")) {
|
||||||
#ifdef EnableImpulsIn
|
#ifdef EnableImpulsIn
|
||||||
//sCmd.addCommand(order.c_str(), impulsInSensor);
|
// sCmd.addCommand(order.c_str(), impulsInSensor);
|
||||||
#endif
|
#endif
|
||||||
} else if (order == F("sensor-node")) {
|
} else if (order == F("sensor-node")) {
|
||||||
#ifdef EnableSensorNode
|
#ifdef EnableSensorNode
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "Utils/JsonUtils.h"
|
#include "Utils/JsonUtils.h"
|
||||||
#include "Utils/FileUtils.h"
|
|
||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
|
#include "Utils/FileUtils.h"
|
||||||
|
|
||||||
String jsonReadStr(String& json, String name) {
|
String jsonReadStr(String& json, String name) {
|
||||||
DynamicJsonBuffer jsonBuffer;
|
DynamicJsonBuffer jsonBuffer;
|
||||||
@@ -20,6 +21,12 @@ int jsonReadInt(String& json, String name) {
|
|||||||
return root[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) {
|
String jsonWriteStr(String& json, String name, String value) {
|
||||||
DynamicJsonBuffer jsonBuffer;
|
DynamicJsonBuffer jsonBuffer;
|
||||||
JsonObject& root = jsonBuffer.parseObject(json);
|
JsonObject& root = jsonBuffer.parseObject(json);
|
||||||
|
|||||||
64
src/items/vSensorAny.cpp
Normal file
64
src/items/vSensorAny.cpp
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
#include "Consts.h"
|
||||||
|
#ifdef EnableSensorAny
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#include "BufferExecute.h"
|
||||||
|
#include "Class/LineParsing.h"
|
||||||
|
#include "Global.h"
|
||||||
|
#include "items/vSensorAny.h"
|
||||||
|
#include "sensors/SensorYour.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) {
|
||||||
|
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());
|
||||||
|
myLineParsing.clear();
|
||||||
|
|
||||||
|
static bool firstTime = true;
|
||||||
|
if (firstTime) mySensorAny = new MySensorAnyVector();
|
||||||
|
firstTime = false;
|
||||||
|
mySensorAny->push_back(SensorAny(params));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
9
src/sensors/YourSensor.cpp
Normal file
9
src/sensors/YourSensor.cpp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#include "sensors/YourSensor.h"
|
||||||
|
|
||||||
|
float yourSensorReading(String type) {
|
||||||
|
float value;
|
||||||
|
if (type == "name1") {
|
||||||
|
value++;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user