websocket & function

This commit is contained in:
avaksru
2021-12-13 16:26:43 +03:00
parent e46a6247f9
commit d083fbf9cf
28 changed files with 602 additions and 103 deletions

View File

@@ -7,6 +7,7 @@
#include "Global.h"
#include "SoftUART.h"
#include "items/vButtonOut.h"
//#include "WebServer.h"
//this class save data to flash
ButtonOut::ButtonOut(String pin, boolean inv, String key, String type) {
_pin = pin;
@@ -21,6 +22,12 @@ ButtonOut::ButtonOut(String pin, boolean inv, String key, String type) {
this->execute(String(state)); //установили это состояние
#endif
#ifdef GATE_MODE
if (_pin != "") {
pinMode(_pin.toInt(), OUTPUT);
}
int state = jsonReadInt(configStoreJson, key); //прочитали из памяти
this->execute(String(state)); //установили это состояние
//TO DO запросили ноду о состоянии реле
//установили в это состояние кнопку в приложении
//если нода не ответила - кнопку сделать красным цветом
@@ -44,6 +51,20 @@ void ButtonOut::execute(String state) {
}
#endif
#ifdef GATE_MODE
// включаем кнопки на ESP гейта
if (state != "" && _pin != "") {
if (state == "change") {
state = String(!digitalRead(_pin.toInt()));
digitalWrite(_pin.toInt(), state.toInt());
} else {
if (_inv) {
digitalWrite(_pin.toInt(), !state.toInt());
} else {
digitalWrite(_pin.toInt(), state.toInt());
}
}
}
//отправили ноде команду на вкл выкл
//получили обратную связь - переставили кнопку в приложении
//не получили обратную связь - сделали кнопку красной
@@ -52,6 +73,12 @@ void ButtonOut::execute(String state) {
jsonWriteInt(configStoreJson, _key, state.toInt());
saveStore();
publishStatus(_key, state);
String path = mqttRootDevice + "/" + _key + "/status";
String json = "{}";
jsonWriteStr(json, "status", state);
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
}
MyButtonOutVector* myButtonOut = nullptr;

View File

@@ -32,6 +32,12 @@ void CountDownClass::loop() {
String time = String(prettyMillis(sec * 1000));
jsonWriteStr(configLiveJson, _key, time);
publishStatus(_key, time);
String path = mqttRootDevice + "/" + _key + "/status";
String json = "{}";
jsonWriteStr(json, "status", time);
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
sec--;
if (sec < 0) {
_countDownPeriod = 0;
@@ -48,10 +54,8 @@ void countDown() {
countDown_EnterCounter++;
addKey(key, countDown_KeyList, countDown_EnterCounter);
//Serial.println(countDown_EnterCounter);
//Serial.println(countDown_KeyList);
static bool firstTime = true;
if (firstTime) myCountDown = new MyCountDownVector();
firstTime = false;

View File

@@ -31,6 +31,12 @@ void Input::execute(String value) {
jsonWriteStr(configStoreJson, _key, value);
saveStore();
publishStatus(_key, value);
String path = mqttRootDevice + "/" + _key + "/status";
String json = "{}";
jsonWriteStr(json, "status", value);
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
}
MyInputVector* myInput = nullptr;

View File

@@ -166,7 +166,78 @@ void loggingExecute() {
}
}
void choose_log_date_and_sendWS() {
String all_line = logging_KeyList;
while (all_line.length() != 0) {
String tmp = selectToMarker(all_line, ",");
sendLogDataWS("/logs/" + tmp + ".txt", tmp);
all_line = deleteBeforeDelimiter(all_line, ",");
}
}
void sendLogDataWS(String file, String topic) {
File configFile = FileFS.open(file, "r");
if (!configFile) {
return;
}
configFile.seek(0, SeekSet);
int i = 0;
String buf = "{}";
String json_array;
String unix_time;
String value;
unsigned int psn;
unsigned int sz = configFile.size();
do {
i++;
psn = configFile.position();
String line = configFile.readStringUntil('\n');
unix_time = selectToMarker(line, " ");
jsonWriteInt(buf, "x", unix_time.toInt());
value = deleteBeforeDelimiter(line, " ");
jsonWriteFloat(buf, "y1", value.toFloat());
if (unix_time != "" || value != "") {
json_array += buf + ",";
}
int grafmax = jsonReadInt(configSetupJson, "grafmax");
if (grafmax != 0) {
if (i >= grafmax) {
json_array = "{\"status\":[" + json_array + "]}";
json_array.replace("},]}", "}]}");
// publishChart(topic, json_array);
// добавляем топик, выводим в ws
String path = mqttRootDevice + "/" + topic;
String json = "{}";
json=json_array;
jsonWriteStr(json, "topic", path);
ws.textAll(json);
json_array = "";
i = 0;
}
}
} while (psn < sz);
configFile.close();
json_array = "{\"status\":[" + json_array + "]}";
json_array.replace("},]}", "}]}");
//publishChart(topic, json_array);
// добавляем топик, выводим в ws
String path = mqttRootDevice + "/" + topic;
String json = "{}";
json=json_array;
jsonWriteStr(json, "topic", path);
ws.textAll(json);
}
void choose_log_date_and_send() {
String all_line = logging_KeyList;
while (all_line.length() != 0) {
String tmp = selectToMarker(all_line, ",");
@@ -205,7 +276,8 @@ void sendLogData(String file, String topic) {
json_array = "{\"status\":[" + json_array + "]}";
json_array.replace("},]}", "}]}");
publishChart(topic, json_array);
json_array = "";
json_array = "";
i = 0;
}
}
@@ -216,6 +288,7 @@ void sendLogData(String file, String topic) {
json_array = "{\"status\":[" + json_array + "]}";
json_array.replace("},]}", "}]}");
publishChart(topic, json_array);
}
void cleanLogAndData() {

View File

@@ -19,6 +19,12 @@ void Output::execute(String value) {
eventGen2(_key, value);
jsonWriteStr(configLiveJson, _key, value);
publishStatus(_key, value);
String path = mqttRootDevice + "/" + _key + "/status";
String json = "{}";
jsonWriteStr(json, "status", value);
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
//publishLastUpdateTime(_key, timeNow->getTime());
}

View File

@@ -25,6 +25,12 @@ void PwmOut::execute(String state) {
jsonWriteInt(configStoreJson, _key, state.toInt());
saveStore();
publishStatus(_key, state);
String path = mqttRootDevice + "/" + _key + "/status";
String json = "{}";
jsonWriteStr(json, "status", state);
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
}
MyPwmOutVector* myPwmOut = nullptr;

View File

@@ -45,6 +45,12 @@ void SensorAnalog::readAnalog() {
eventGen2(_key, String(valueFloat));
jsonWriteStr(configLiveJson, _key, String(valueFloat));
publishStatus(_key, String(valueFloat));
String path = mqttRootDevice + "/" + _key + "/status";
String json = "{}";
jsonWriteStr(json, "status", String(valueFloat));
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _key + "' data: " + String(valueFloat));
}

View File

@@ -48,16 +48,34 @@ void SensorBme280::read() {
eventGen2(_paramsTmp.key, String(tmp));
jsonWriteStr(configLiveJson, _paramsTmp.key, String(tmp));
publishStatus(_paramsTmp.key, String(tmp));
String path = mqttRootDevice + "/" +_paramsTmp.key + "/status";
String json = "{}";
jsonWriteStr(json, "status", String(tmp));
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsTmp.key + "' data: " + String(tmp));
eventGen2(_paramsHum.key, String(hum));
jsonWriteStr(configLiveJson, _paramsHum.key, String(hum));
publishStatus(_paramsHum.key, String(hum));
path = mqttRootDevice + "/" +_paramsHum.key + "/status";
json = "{}";
jsonWriteStr(json, "status", String(hum));
MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsHum.key + "' data: " + String(hum));
eventGen2(_paramsPrs.key, String(prs));
jsonWriteStr(configLiveJson, _paramsPrs.key, String(prs));
publishStatus(_paramsPrs.key, String(prs));
path = mqttRootDevice + "/" +_paramsPrs.key + "/status";
json = "{}";
jsonWriteStr(json, "status", String(prs));
MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsPrs.key + "' data: " + String(prs));
}

View File

@@ -44,11 +44,23 @@ void SensorBmp280::read() {
eventGen2(_paramsTmp.key, String(tmp));
jsonWriteStr(configLiveJson, _paramsTmp.key, String(tmp));
publishStatus(_paramsTmp.key, String(tmp));
String path = mqttRootDevice + "/" +_paramsTmp.key + "/status";
String json = "{}";
jsonWriteStr(json, "status", String(tmp));
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsTmp.key + "' data: " + String(tmp));
eventGen2(_paramsPrs.key, String(prs));
jsonWriteStr(configLiveJson, _paramsPrs.key, String(prs));
publishStatus(_paramsPrs.key, String(prs));
path = mqttRootDevice + "/" +_paramsPrs.key + "/status";
json = "{}";
jsonWriteStr(json, "status", String(prs));
MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsPrs.key + "' data: " + String(prs));
}

View File

@@ -41,11 +41,23 @@ void SensorCcs811::read() {
eventGen2(_paramsPpm.key, String(co2));
jsonWriteStr(configLiveJson, _paramsPpm.key, String(co2));
publishStatus(_paramsPpm.key, String(co2));
String path = mqttRootDevice + "/" +_paramsPpm.key + "/status";
String json = "{}";
jsonWriteStr(json, "status", String(co2));
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsPpm.key + "' data: " + String(co2));
eventGen2(_paramsPpb.key, String(ppm));
jsonWriteStr(configLiveJson, _paramsPpb.key, String(ppm));
publishStatus(_paramsPpb.key, String(ppm));
path = mqttRootDevice + "/" +_paramsPpb.key + "/status";
json = "{}";
jsonWriteStr(json, "status", String(ppm));
MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsPpb.key + "' data: " + String(ppm));
} else {
SerialPrint("E", "Sensor CCS", "Error");

View File

@@ -39,6 +39,12 @@ void SensorDallas::readDallas() {
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));
}

View File

@@ -44,11 +44,23 @@ void SensorDht::readTmpHum() {
eventGen2(_paramsTmp.key, String(tmp));
jsonWriteStr(configLiveJson, _paramsTmp.key, String(tmp));
publishStatus(_paramsTmp.key, String(tmp));
String path = mqttRootDevice + "/" +_paramsTmp.key + "/status";
String json = "{}";
jsonWriteStr(json, "status", String(tmp));
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsTmp.key + "' data: " + String(tmp));
eventGen2(_paramsHum.key, String(hum));
jsonWriteStr(configLiveJson, _paramsHum.key, String(hum));
publishStatus(_paramsHum.key, String(hum));
path = mqttRootDevice + "/" +_paramsHum.key + "/status";
json = "{}";
jsonWriteStr(json, "status", String(hum));
MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsHum.key + "' data: " + String(hum));
} else {

View File

@@ -1,51 +0,0 @@
//#include "items/vSensorImpulsIn.h"
//
//#include "BufferExecute.h"
//#include "Class/LineParsing.h"
//#include "Global.h"
//#include "SoftUART.h"
//
//SensorImpulsIn::SensorImpulsIn(const paramsImpulsIn& paramsImpuls) {
// _paramsImpuls = paramsImpulsIn(paramsImpuls);
// pinMode(14, INPUT);
// //pinMode(_paramsImpuls.pin, INPUT_PULLUP);
// //attachInterrupt(digitalPinToInterrupt(14), MYinterrupt, CHANGE);
//}
//
//SensorImpulsIn::~SensorImpulsIn() {}
//
////void SensorImpulsIn::read() {
// float voltage; //= (impulsIn->values()->voltage * _paramsV.c) + _paramsV.k;
//
// eventGen2(_paramsImpuls.key, String(voltage));
// jsonWriteStr(configLiveJson, _paramsImpuls.key, String(voltage));
// publishStatus(_paramsImpuls.key, String(voltage));
// SerialPrint("I", "Sensor", "'" + _paramsImpuls.key + "' data: " + String(voltage));
//}
//
//MySensorImpulsInVector* mySensorImpulsIn = nullptr;
//
//void impulsInSensor() {
// myLineParsing.update();
// String key = myLineParsing.gkey();
// String pin = myLineParsing.gpin();
// String c = myLineParsing.gc();
// String k = myLineParsing.gk();
// myLineParsing.clear();
//
// static paramsImpulsIn paramsImpuls;
//
// paramsImpuls.key = key;
// paramsImpuls.pin = pin.toInt();
// paramsImpuls.c = c.toFloat();
// paramsImpuls.k = k.toFloat();
//
// static bool firstTime = true;
// if (firstTime) mySensorImpulsIn = new MySensorImpulsInVector();
// firstTime = false;
// mySensorImpulsIn->push_back(SensorImpulsIn(paramsImpuls));
//}
//
//void MYinterrupt() {
// Serial.println("interrupt!");
//}

View File

@@ -37,6 +37,12 @@ void SensorNode::onChange(String newValue, String incommingKey) {
eventGen2(_params.key, newValue);
jsonWriteStr(configLiveJson, _params.key, newValue);
publishStatus(_params.key, newValue);
String path = mqttRootDevice + "/" +_params.key + "/status";
String json = "{}";
jsonWriteStr(json, "status", newValue);
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
_updateTime = timeNow->getDateTimeDotFormated();
@@ -48,17 +54,72 @@ void SensorNode::onChange(String newValue, String incommingKey) {
void SensorNode::publish() {
if (_minutesPassed < _params.tm1.toInt()) {
publishAnyJsonKey(_params.key, "info", String(_minutesPassed) + " min");
String path = mqttRootDevice + "/" + _params.key + "/status";
String json = "{}";
jsonWriteStr(json, "info", String(_minutesPassed) + " min");
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
publishAnyJsonKey(_params.key, "color", "");
path = mqttRootDevice + "/" + _params.key + "/status";
json = "{}";
jsonWriteStr(json, "color", "");
MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
} else if (_minutesPassed >= _params.tm1.toInt() && _minutesPassed < _params.tm2.toInt()) {
publishAnyJsonKey(_params.key, "info", String(_minutesPassed) + " min");
String path = mqttRootDevice + "/" + _params.key + "/status";
String json = "{}";
jsonWriteStr(json, "info", String(_minutesPassed) + " min");
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
publishAnyJsonKey(_params.key, "color", "orange");
path = mqttRootDevice + "/" + _params.key + "/status";
json = "{}";
jsonWriteStr(json, "color", "orange");
MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
} else if (_minutesPassed >= _params.tm2.toInt()) {
if (_updateTime == "") {
publishAnyJsonKey(_params.key, "info", "offline");
String path = mqttRootDevice + "/" + _params.key + "/status";
String json = "{}";
jsonWriteStr(json, "info", "offline");
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
} else {
publishAnyJsonKey(_params.key, "info", _updateTime);
String path = mqttRootDevice + "/" + _params.key + "/status";
String json = "{}";
jsonWriteStr(json, "info", _updateTime);
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
}
publishAnyJsonKey(_params.key, "color", "red");
String path = mqttRootDevice + "/" + _params.key + "/status";
String json = "{}";
jsonWriteStr(json, "color", "red");
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
}
}

View File

@@ -38,26 +38,56 @@ void SensorPzem::read() {
eventGen2(_paramsV.key, String(voltage));
jsonWriteStr(configLiveJson, _paramsV.key, String(voltage));
publishStatus(_paramsV.key, String(voltage));
String path = mqttRootDevice + "/" +_paramsV.key + "/status";
String json = "{}";
jsonWriteStr(json, "status", String(voltage));
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsV.key + "' data: " + String(voltage));
eventGen2(_paramsA.key, String(current));
jsonWriteStr(configLiveJson, _paramsA.key, String(current));
publishStatus(_paramsA.key, String(current));
path = mqttRootDevice + "/" +_paramsA.key + "/status";
json = "{}";
jsonWriteStr(json, "status", String(current));
MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsA.key + "' data: " + String(current));
eventGen2(_paramsWatt.key, String(power));
jsonWriteStr(configLiveJson, _paramsWatt.key, String(power));
publishStatus(_paramsWatt.key, String(power));
path = mqttRootDevice + "/" +_paramsWatt.key + "/status";
json = "{}";
jsonWriteStr(json, "status", String(power));
MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsWatt.key + "' data: " + String(power));
eventGen2(_paramsWattHrs.key, String(energy));
jsonWriteStr(configLiveJson, _paramsWattHrs.key, String(energy));
publishStatus(_paramsWattHrs.key, String(energy));
path = mqttRootDevice + "/" +_paramsWattHrs.key + "/status";
json = "{}";
jsonWriteStr(json, "status", String(energy));
MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsWattHrs.key + "' data: " + String(energy));
eventGen2(_paramsHz.key, String(freq));
jsonWriteStr(configLiveJson, _paramsHz.key, String(freq));
publishStatus(_paramsHz.key, String(freq));
path = mqttRootDevice + "/" +_paramsHz.key + "/status";
json = "{}";
jsonWriteStr(json, "status", String(freq));
MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsHz.key + "' data: " + String(freq));
} else {
SerialPrint("E", "Sensor PZEM", "Error, UART switched off");

View File

@@ -49,11 +49,24 @@ void SensorSht20::read() {
eventGen2(_paramsTmp.key, String(tmp));
jsonWriteStr(configLiveJson, _paramsTmp.key, String(tmp));
publishStatus(_paramsTmp.key, String(tmp));
String path = mqttRootDevice + "/" +_paramsTmp.key + "/status";
String json = "{}";
jsonWriteStr(json, "status", String(tmp));
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsTmp.key + "' data: " + String(tmp));
eventGen2(_paramsHum.key, String(hum));
jsonWriteStr(configLiveJson, _paramsHum.key, String(hum));
publishStatus(_paramsHum.key, String(hum));
path = mqttRootDevice + "/" +_paramsHum.key + "/status";
json = "{}";
jsonWriteStr(json, "status", String(hum));
MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsHum.key + "' data: " + String(hum));
}

View File

@@ -53,20 +53,22 @@ void SensorUltrasonic::readUltrasonic() {
value = duration_ / 29 / 2;
value = testFilter.filtered(value);
value = map(value, _map1, _map2, _map3, _map4);
float valueFloat = value * _c;
if (counter > 10) {
eventGen2(_key, String(valueFloat));
jsonWriteStr(configLiveJson, _key, String(valueFloat));
publishStatus(_key, String(valueFloat));
String path = mqttRootDevice + "/" +_key + "/status";
String json = "{}";
jsonWriteStr(json, "status", String(valueFloat));
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _key + "' data: " + String(valueFloat));
}
}
MySensorUltrasonicVector* mySensorUltrasonic = nullptr;
void ultrasonic() {
myLineParsing.update();
String interval = myLineParsing.gint();
@@ -75,15 +77,12 @@ void ultrasonic() {
String map = myLineParsing.gmap();
String c = myLineParsing.gc();
myLineParsing.clear();
unsigned int trig = selectFromMarkerToMarker(pin, ",", 0).toInt();
unsigned int echo = selectFromMarkerToMarker(pin, ",", 1).toInt();
int map1 = selectFromMarkerToMarker(map, ",", 0).toInt();
int map2 = selectFromMarkerToMarker(map, ",", 1).toInt();
int map3 = selectFromMarkerToMarker(map, ",", 2).toInt();
int map4 = selectFromMarkerToMarker(map, ",", 3).toInt();
static bool firstTime = true;
if (firstTime) mySensorUltrasonic = new MySensorUltrasonicVector();
firstTime = false;

View File

@@ -28,6 +28,12 @@ void SensorUptime::read() {
eventGen2(_paramsUpt.key, upt);
jsonWriteStr(configLiveJson, _paramsUpt.key, upt);
publishStatus(_paramsUpt.key, upt);
String path = mqttRootDevice + "/" +_paramsUpt.key + "/status";
String json = "{}";
jsonWriteStr(json, "status", upt);
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
SerialPrint("I", "Sensor", "'" + _paramsUpt.key + "' data: " + upt);
}
@@ -38,12 +44,9 @@ void uptimeSensor() {
String key = myLineParsing.gkey();
String interval = myLineParsing.gint();
myLineParsing.clear();
static paramsUptime paramsUpt;
paramsUpt.key = key;
paramsUpt.interval = interval.toInt() * 1000;
static bool firstTime = true;
if (firstTime) mySensorUptime = new MySensorUptimeVector();
firstTime = false;