переписан dht

This commit is contained in:
Dmitry Borisenko
2020-12-21 01:46:11 +01:00
parent c5772f8451
commit d9672d17b0
14 changed files with 380 additions and 211 deletions

View File

@@ -9,10 +9,10 @@
9;0;analog-adc;id;fillgauge;Сенсоры;Аналоговый;order;pin[0];map[0,1024,0,100];c[1];int[10]
10;0;dallas-temp;id;anydataTemp;Сенсоры;Температура;order;pin[2];index[0];int[10]
11;0;ultrasonic-cm;id;anydata;Сенсоры;Расстояние;order;pin[12,13];map[0,500,0,100];c[1];int[10]
12;0;dht-hum;id;anydataHum;Сенсоры;Влажность;order;pin[2];type[dht11];c[1]
13;0;dht-temp;id;anydataTemp;Сенсоры;Температура;order;pin[2];type[dht11];c[1]
14;0;dht-hum;id;anydataHum;Сенсоры;Влажность;order;pin[2];type[dht22];c[1]
15;0;dht-temp;id;anydataTemp;Сенсоры;Температура;order;pin[2];type[dht22];c[1]
12;0;dht-temp;id;anydataTemp;Сенсоры;Температура;order;pin[2];type[dht11];c[1];int[10]
13;0;dht-hum;id;anydataHum;Сенсоры;Влажность;order;pin[2];type[dht11];c[1];int[10]
14;0;dht-temp;id;anydataTemp;Сенсоры;Температура;order;pin[2];type[dht22];c[1];int[10]
15;0;dht-hum;id;anydataHum;Сенсоры;Влажность;order;pin[2];type[dht22];c[1];int[10]
16;0;bme280-temp;id;anydataTemp;Сенсоры;Температура;order;addr[0x76];c[1]
17;0;bme280-hum;id;anydataHum;Сенсоры;Влажность;order;addr[0x76];c[1]
18;0;bme280-press;id;anydataPress;Сенсоры;Давление;order;addr[0x76];c[1]

View File

@@ -12,19 +12,7 @@ extern int getKeyNum(String& key, String& keyNumberTable);
extern void buttonIn();
extern void buttonInSet();
extern void analogAdc();
extern void analogReading();
//extern void ultrasonicCm();
//extern void ultrasonicReading();
extern void dallasTemp();
extern void dallasReading();
extern void dhtTemp();
extern void dhtReadingTemp();
extern void dhtHum();
extern void dhtReadingHum();
extern void bme280Temp();
extern void bme280ReadingTemp();

View File

@@ -8,7 +8,6 @@
#include <ArduinoJson.h>
#include <ArduinoOTA.h>
#include <Bounce2.h>
#include <DHTesp.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <PubSubClient.h>
@@ -89,6 +88,9 @@ extern int countDown_EnterCounter;
extern String logging_KeyList;
extern int logging_EnterCounter;
//=========================================
extern int dhtTmp_EnterCounter;
extern int dhtHum_EnterCounter;
//=========================================
// Sensors
extern String sensorReadingMap10sec;

View File

@@ -1,36 +0,0 @@
//#pragma once
//#include <Arduino.h>
//
//#include "Class/LineParsing.h"
//#include "Global.h"
//#include "items/SensorConvertingClass.h"
//
//class SensorAnalogClass : public SensorConvertingClass {
// public:
// SensorAnalogClass() : SensorConvertingClass(){};
//
// void SensorAnalogInit() {
// jsonWriteStr(configOptionJson, _key + "_pin", _pin);
// jsonWriteStr(configOptionJson, _key + "_map", _map);
// jsonWriteStr(configOptionJson, _key + "_с", _c);
// }
//
// int SensorAnalogRead(String key, String pin) {
// int value;
//#ifdef ESP32
// int pinInt = pin.toInt();
// value = analogRead(pinInt);
//#endif
//#ifdef ESP8266
// value = analogRead(A0);
//#endif
// value = this->mapping(key, value);
// float valueFl = this->correction(key, value);
// eventGen2(key, String(valueFl));
// jsonWriteStr(configLiveJson, key, String(valueFl));
// publishStatus(key, String(valueFl));
// SerialPrint("I", "Sensor", "'" + key + "' data: " + String(valueFl));
// return value;
// }
//};
//extern SensorAnalogClass mySensorAnalog;

View File

@@ -1,80 +1,80 @@
#pragma once
#include "Consts.h"
#ifdef SensorDhtEnabled
#include <Arduino.h>
#include "Class/LineParsing.h"
#include "Global.h"
#include "items/SensorConvertingClass.h"
DHTesp dht;
class SensorDhtClass : public SensorConvertingClass {
public:
SensorDhtClass() : SensorConvertingClass(){};
void SensorDhtInit() {
if (_type == "dht11") {
dht.setup(_pin.toInt(), DHTesp::DHT11);
}
if (_type == "dht22") {
dht.setup(_pin.toInt(), DHTesp::DHT22);
}
sensorReadingMap10sec += _key + ",";
//to do если надо будет читать несколько dht
//dhtEnterCounter++;
//jsonWriteInt(configOptionJson, _key + "_num", dhtEnterCounter);
jsonWriteStr(configOptionJson, _key + "_map", _map);
jsonWriteStr(configOptionJson, _key + "_с", _c);
}
void SensorDhtReadTemp(String key) {
//to do если надо будет читать несколько dht
//int cnt = jsonReadInt(configOptionJson, key + "_num");
float value;
static int counter;
if (dht.getStatus() != 0 && counter < 5) {
counter++;
//return;
} else {
counter = 0;
value = dht.getTemperature();
if (String(value) != "nan") {
//value = this->mapping(key, value);
float valueFl = this->correction(key, value);
eventGen2(key, String(valueFl));
jsonWriteStr(configLiveJson, key, String(valueFl));
publishStatus(key, String(valueFl));
SerialPrint("I", "Sensor", "'" + key + "' data: " + String(valueFl));
} else {
Serial.println("[E] sensor '" + key);
}
}
}
void SensorDhtReadHum(String key) {
//to do если надо будет читать несколько dht
//int cnt = jsonReadInt(configOptionJson, key + "_num");
float value;
static int counter;
if (dht.getStatus() != 0 && counter < 5) {
counter++;
//return;
} else {
counter = 0;
value = dht.getHumidity();
if (String(value) != "nan") {
//value = this->mapping(key, value);
float valueFl = this->correction(key, value);
eventGen2(key, String(valueFl));
jsonWriteStr(configLiveJson, key, String(valueFl));
publishStatus(key, String(valueFl));
SerialPrint("I", "Sensor", "'" + key + "' data: " + String(valueFl));
} else {
Serial.println("[E] sensor '" + key);
}
}
}
};
extern SensorDhtClass mySensorDht;
#endif
//#pragma once
//#include "Consts.h"
//#ifdef SensorDhtEnabled
//#include <Arduino.h>
//#include "Class/LineParsing.h"
//#include "Global.h"
//#include "items/SensorConvertingClass.h"
//
//DHTesp dht;
//class SensorDhtClass : public SensorConvertingClass {
// public:
// SensorDhtClass() : SensorConvertingClass(){};
//
// void SensorDhtInit() {
// if (_type == "dht11") {
// dht.setup(_pin.toInt(), DHTesp::DHT11);
// }
// if (_type == "dht22") {
// dht.setup(_pin.toInt(), DHTesp::DHT22);
// }
// sensorReadingMap10sec += _key + ",";
//
// //to do если надо будет читать несколько dht
// //dhtEnterCounter++;
// //jsonWriteInt(configOptionJson, _key + "_num", dhtEnterCounter);
//
// jsonWriteStr(configOptionJson, _key + "_map", _map);
// jsonWriteStr(configOptionJson, _key + "_с", _c);
// }
//
// void SensorDhtReadTemp(String key) {
// //to do если надо будет читать несколько dht
// //int cnt = jsonReadInt(configOptionJson, key + "_num");
// float value;
// static int counter;
// if (dht.getStatus() != 0 && counter < 5) {
// counter++;
// //return;
// } else {
// counter = 0;
// value = dht.getTemperature();
// if (String(value) != "nan") {
// //value = this->mapping(key, value);
// float valueFl = this->correction(key, value);
// eventGen2(key, String(valueFl));
// jsonWriteStr(configLiveJson, key, String(valueFl));
// publishStatus(key, String(valueFl));
// SerialPrint("I", "Sensor", "'" + key + "' data: " + String(valueFl));
// } else {
// Serial.println("[E] sensor '" + key);
// }
// }
// }
//
// void SensorDhtReadHum(String key) {
// //to do если надо будет читать несколько dht
// //int cnt = jsonReadInt(configOptionJson, key + "_num");
// float value;
// static int counter;
// if (dht.getStatus() != 0 && counter < 5) {
// counter++;
// //return;
// } else {
// counter = 0;
// value = dht.getHumidity();
// if (String(value) != "nan") {
// //value = this->mapping(key, value);
// float valueFl = this->correction(key, value);
// eventGen2(key, String(valueFl));
// jsonWriteStr(configLiveJson, key, String(valueFl));
// publishStatus(key, String(valueFl));
// SerialPrint("I", "Sensor", "'" + key + "' data: " + String(valueFl));
// } else {
// Serial.println("[E] sensor '" + key);
// }
// }
// }
//};
//extern SensorDhtClass mySensorDht;
//#endif

View File

@@ -0,0 +1,63 @@
#pragma once
#include <Arduino.h>
#include <DHTesp.h>
#include "Global.h"
#include "GyverFilters.h"
extern DHTesp* dht;
class SensorDht;
typedef std::vector<SensorDht> MySensorDhtVector;
struct tmpParams {
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long difference;
unsigned long interval;
String key;
unsigned int pin;
int map1;
int map2;
int map3;
int map4;
float c;
};
struct humParams {
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long difference;
unsigned long interval;
String key;
unsigned int pin;
int map1;
int map2;
int map3;
int map4;
float c;
};
class SensorDht {
public:
SensorDht();
~SensorDht();
void loopTmp();
void loopHum();
void readTmp();
void readHum();
void tmpInit(const tmpParams& tmpSet);
void humInit(const humParams& humSet);
private:
tmpParams _tmpSet;
humParams _humSet;
};
extern MySensorDhtVector* mySensorDht;
extern void dhtTmp();
extern void dhtHum();

View File

@@ -10,6 +10,8 @@
#include "items/vLogging.h"
#include "items/vImpulsOut.h"
#include "items/vCountDown.h"
#include "items/vSensorAnalog.h"
#include "items/vSensorDht.h"
void loopCmdAdd(const String& cmdStr) {
if (cmdStr.endsWith(",")) {
@@ -73,7 +75,7 @@ void csvCmdExecute(String& cmdStr) {
}
#ifdef SensorDhtEnabled
else if (order == F("dht-temp")) {
sCmd.addCommand(order.c_str(), dhtTemp);
sCmd.addCommand(order.c_str(), dhtTmp);
}
else if (order == F("dht-hum")) {
sCmd.addCommand(order.c_str(), dhtHum);
@@ -84,7 +86,7 @@ void csvCmdExecute(String& cmdStr) {
sCmd.addCommand(order.c_str(), bme280Temp);
}
else if (order == F("bme280-hum")) {
sCmd.addCommand(order.c_str(), bme280Hum);
//sCmd.addCommand(order.c_str(), bme280Hum);
}
else if (order == F("bme280-press")) {
sCmd.addCommand(order.c_str(), bme280Press);

View File

@@ -18,14 +18,14 @@ void getFSInfo() {
jsonWriteStr(configSetupJson, F("freeBytes"), String(freePer) + "% (" + prettyBytes(freeBytes) + ")");
SerialPrint("I", F("FS"), "totalBytes=" + String(totalBytes));
SerialPrint("I", F("FS"), "usedBytes=" + String(usedBytes));
SerialPrint("I", F("FS"), "maxOpenFiles=" + String(maxOpenFiles));
SerialPrint("I", F("FS"), "blockSize=" + String(blockSize));
SerialPrint("I", F("FS"), "pageSize=" + String(pageSize));
SerialPrint("I", F("FS"), "maxPathLength=" + String(maxPathLength));
SerialPrint("I", F("FS"), "freeBytes=" + String(freeBytes));
SerialPrint("I", F("FS"), "freePer=" + String(freePer));
//SerialPrint("I", F("FS"), "totalBytes=" + String(totalBytes));
//SerialPrint("I", F("FS"), "usedBytes=" + String(usedBytes));
//SerialPrint("I", F("FS"), "maxOpenFiles=" + String(maxOpenFiles));
//SerialPrint("I", F("FS"), "blockSize=" + String(blockSize));
//SerialPrint("I", F("FS"), "pageSize=" + String(pageSize));
//SerialPrint("I", F("FS"), "maxPathLength=" + String(maxPathLength));
//SerialPrint("I", F("FS"), "freeBytes=" + String(freeBytes));
//SerialPrint("I", F("FS"), "freePer=" + String(freePer));
}
else {
SerialPrint("E", F("FS"), F("FS info error"));

View File

@@ -59,6 +59,9 @@ int countDown_EnterCounter = -1;
String logging_KeyList = "";
int logging_EnterCounter = -1;
//=========================================
int dhtTmp_EnterCounter = -1;
int dhtHum_EnterCounter = -1;
//=========================================
// Sensors
String sensorReadingMap10sec;

View File

@@ -1,16 +1,17 @@
#include "Init.h"
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "Cmd.h"
#include "Global.h"
#include "Class/LineParsing.h"
#include "items/vLogging.h"
#include "items/vImpulsOut.h"
#include "items/vButtonOut.h"
#include "items/vCountDown.h"
#include "items/vImpulsOut.h"
#include "items/vInOutput.h"
#include "items/vLogging.h"
#include "items/vPwmOut.h"
#include "items/vSensorDallas.h"
#include "items/vSensorUltrasonic.h"
#include "items/vInOutput.h"
#include "items/vPwmOut.h"
#include "items/vCountDown.h"
void loadConfig() {
configSetupJson = readFile("config.json", 4096);
@@ -42,7 +43,6 @@ void espInit() {
}
void deviceInit() {
sensorReadingMap10sec = "";
//======clear dallas params======
@@ -92,14 +92,16 @@ void deviceInit() {
countDown_KeyList = "";
countDown_EnterCounter = -1;
//===================================
dhtTmp_EnterCounter = -1;
dhtHum_EnterCounter = -1;
//=========================================
#ifdef LAYOUT_IN_RAM
all_widgets = "";
#else
removeFile(String("layout.txt"));
#endif
myLineParsing.clearErrors();
fileCmdExecute(String(DEVICE_CONFIG_FILE));
@@ -108,8 +110,7 @@ void deviceInit() {
if (errors > 0) {
jsonWriteStr(configSetupJson, F("warning3"), F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #ffc7c7; padding: 10px;'>Обнаружен неверный номер пина</p></font></div>"));
}
else {
} else {
jsonWriteStr(configSetupJson, F("warning3"), "");
}
@@ -135,4 +136,3 @@ void uptime_init() {
void handle_uptime() {
jsonWriteStr(configSetupJson, "uptime", timeNow->getUptime());
}

View File

@@ -1,21 +0,0 @@
//#include "BufferExecute.h"
//#include "items/SensorAnalogClass.h"
//#ifdef ANALOG_ENABLED
////==============================================Модуль аналогового сенсора===========================================================================================
////===================================================================================================================================================================
//SensorAnalogClass mySensorAnalog;
//void analogAdc() {
// mySensorAnalog.update();
// String key = mySensorAnalog.gkey();
// sCmd.addCommand(key.c_str(), analogReading);
// sensorReadingMap10sec += key + ",";
// mySensorAnalog.SensorAnalogInit();
// mySensorAnalog.clear();
//}
//
//void analogReading() {
// String key = sCmd.order();
// String pin = jsonReadStr(configOptionJson, key + "_pin");
// mySensorAnalog.SensorAnalogRead(key, pin);
//}
//#endif

View File

@@ -1,35 +1,35 @@
#include "Consts.h"
#ifdef SensorDhtEnabled
#include "items/SensorDhtClass.h"
#include "BufferExecute.h"
//=========================================DHT Sensor==================================================================
//dht-temp;id;anydata;Сенсоры;Температура;order;pin;type[dht11];c[1]
//dht-hum;id;anydata;Сенсоры;Влажность;order;pin;type[dht11];c[1]
//=========================================================================================================================================
SensorDhtClass mySensorDht;
void dhtTemp() {
mySensorDht.update();
String key = mySensorDht.gkey();
sCmd.addCommand(key.c_str(), dhtReadingTemp);
mySensorDht.SensorDhtInit();
mySensorDht.clear();
}
void dhtReadingTemp() {
String key = sCmd.order();
mySensorDht.SensorDhtReadTemp(key);
}
void dhtHum() {
mySensorDht.update();
String key = mySensorDht.gkey();
sCmd.addCommand(key.c_str(), dhtReadingHum);
mySensorDht.SensorDhtInit();
mySensorDht.clear();
}
void dhtReadingHum() {
String key = sCmd.order();
mySensorDht.SensorDhtReadHum(key);
}
#endif
//#include "Consts.h"
//#ifdef SensorDhtEnabled
//#include "items/SensorDhtClass.h"
//#include "BufferExecute.h"
////=========================================DHT Sensor==================================================================
////dht-temp;id;anydata;Сенсоры;Температура;order;pin;type[dht11];c[1]
////dht-hum;id;anydata;Сенсоры;Влажность;order;pin;type[dht11];c[1]
////=========================================================================================================================================
//SensorDhtClass mySensorDht;
//void dhtTemp() {
// mySensorDht.update();
// String key = mySensorDht.gkey();
// sCmd.addCommand(key.c_str(), dhtReadingTemp);
// mySensorDht.SensorDhtInit();
// mySensorDht.clear();
//}
//void dhtReadingTemp() {
// String key = sCmd.order();
// mySensorDht.SensorDhtReadTemp(key);
//}
//
//
//
//void dhtHum() {
// mySensorDht.update();
// String key = mySensorDht.gkey();
// sCmd.addCommand(key.c_str(), dhtReadingHum);
// mySensorDht.SensorDhtInit();
// mySensorDht.clear();
//}
//void dhtReadingHum() {
// String key = sCmd.order();
// mySensorDht.SensorDhtReadHum(key);
//}
//#endif

161
src/items/vSensorDht.cpp Normal file
View File

@@ -0,0 +1,161 @@
#include "items/vSensorDht.h"
#include <Arduino.h>
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "Global.h"
DHTesp* dht = nullptr;
SensorDht::SensorDht() {}
SensorDht::~SensorDht() {}
void SensorDht::tmpInit(const tmpParams& tmpSet) {
_tmpSet = tmpParams(tmpSet);
if (!dht) {
dht = new DHTesp();
}
dht->setup(_tmpSet.pin, DHTesp::DHT11);
}
void SensorDht::humInit(const humParams& humSet) {
_humSet = humParams(humSet);
if (!dht) {
dht = new DHTesp();
}
dht->setup(_tmpSet.pin, DHTesp::DHT11);
}
void SensorDht::loopTmp() {
_tmpSet.currentMillis = millis();
_tmpSet.difference = _tmpSet.currentMillis - _tmpSet.prevMillis;
if (_tmpSet.difference >= _tmpSet.interval) {
_tmpSet.prevMillis = millis();
readTmp();
}
}
void SensorDht::loopHum() {
_humSet.currentMillis = millis();
_humSet.difference = _humSet.currentMillis - _humSet.prevMillis;
if (_humSet.difference >= _humSet.interval) {
_humSet.prevMillis = millis();
readHum();
}
}
void SensorDht::readTmp() {
float value;
static int counter;
if (dht->getStatus() != 0 && counter < 5) {
counter++;
SerialPrint("E", "Sensor", "Disconnected");
} else {
counter = 0;
value = dht->getTemperature();
if (String(value) != "nan") {
//value = map(value, _tmpSet.map1, _tmpSet.map2, _tmpSet.map3, _tmpSet.map4);
value = value * _tmpSet.c;
eventGen2(_tmpSet.key, String(value));
jsonWriteStr(configLiveJson, _tmpSet.key, String(value));
publishStatus(_tmpSet.key, String(value));
SerialPrint("I", "Sensor", "'" + _tmpSet.key + "' data: " + String(value));
} else {
SerialPrint("E", "Sensor", "'" + _tmpSet.key + "' data: " + String(value));
}
}
}
void SensorDht::readHum() {
float value;
static int counter;
if (dht->getStatus() != 0 && counter < 5) {
counter++;
SerialPrint("E", "Sensor", "Disconnected");
} else {
counter = 0;
value = dht->getHumidity();
if (String(value) != "nan") {
//value = map(value, _humSet.map1, _humSet.map2, _humSet.map3, _humSet.map4);
value = value * _humSet.c;
eventGen2(_humSet.key, String(value));
jsonWriteStr(configLiveJson, _humSet.key, String(value));
publishStatus(_humSet.key, String(value));
SerialPrint("I", "Sensor", "'" + _humSet.key + "' data: " + String(value));
} else {
SerialPrint("E", "Sensor", "'" + _humSet.key + "' data: " + String(value));
}
}
}
MySensorDhtVector* mySensorDht = nullptr;
void dhtTmp() {
myLineParsing.update();
String interval = myLineParsing.gint();
String pin = myLineParsing.gpin();
String key = myLineParsing.gkey();
String map = myLineParsing.gmap();
String c = myLineParsing.gc();
myLineParsing.clear();
int map1 = selectFromMarkerToMarker(map, ",", 0).toInt();
int map2 = selectFromMarkerToMarker(map, ",", 1).toInt();
int map3 = selectFromMarkerToMarker(map, ",", 2).toInt();
int map4 = selectFromMarkerToMarker(map, ",", 3).toInt();
tmpParams buf;
buf.interval = interval.toInt() * 1000;
buf.key = key;
buf.pin = pin.toInt();
buf.map1 = map1;
buf.map2 = map2;
buf.map3 = map3;
buf.map4 = map4;
buf.c = c.toFloat();
dhtTmp_EnterCounter++;
static bool firstTime = true;
if (firstTime) mySensorDht = new MySensorDhtVector();
firstTime = false;
mySensorDht->push_back(SensorDht());
mySensorDht->at(dhtTmp_EnterCounter).tmpInit(buf);
}
void dhtHum() {
myLineParsing.update();
String interval = myLineParsing.gint();
String pin = myLineParsing.gpin();
String key = myLineParsing.gkey();
String map = myLineParsing.gmap();
String c = myLineParsing.gc();
myLineParsing.clear();
int map1 = selectFromMarkerToMarker(map, ",", 0).toInt();
int map2 = selectFromMarkerToMarker(map, ",", 1).toInt();
int map3 = selectFromMarkerToMarker(map, ",", 2).toInt();
int map4 = selectFromMarkerToMarker(map, ",", 3).toInt();
humParams buf;
buf.interval = interval.toInt() * 1000;
buf.key = key;
buf.pin = pin.toInt();
buf.map1 = map1;
buf.map2 = map2;
buf.map3 = map3;
buf.map4 = map4;
buf.c = c.toFloat();
dhtHum_EnterCounter++;
static bool firstTime = true;
if (firstTime) mySensorDht = new MySensorDhtVector();
firstTime = false;
mySensorDht->push_back(SensorDht());
mySensorDht->at(dhtHum_EnterCounter).humInit(buf);
}

View File

@@ -25,6 +25,7 @@
#include "items/vSensorAnalog.h"
#include "items/vSensorDallas.h"
#include "items/vSensorUltrasonic.h"
#include "items/vSensorDht.h"
void not_async_actions();
@@ -134,4 +135,10 @@ void loop() {
mySensorAnalog->at(i).loop();
}
}
if (mySensorDht != nullptr) {
for (unsigned int i = 0; i < mySensorDht->size(); i++) {
mySensorDht->at(i).loopTmp();
mySensorDht->at(i).loopHum();
}
}
}