переписан 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

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