From 875a7c010173dea13a9b19fcd5342d1055560b5a Mon Sep 17 00:00:00 2001 From: Fisch Date: Mon, 8 Nov 2021 20:21:50 +0100 Subject: [PATCH] fix mhz19 calibration handler --- include/sensor_mhz19b.cpp | 26 +++++--------------------- include/sensor_mhz19b.h | 7 +++---- src/main.cpp | 33 +++++++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/include/sensor_mhz19b.cpp b/include/sensor_mhz19b.cpp index ecc3812..a078b3c 100644 --- a/include/sensor_mhz19b.cpp +++ b/include/sensor_mhz19b.cpp @@ -66,25 +66,7 @@ void Sensor_MHZ19B::setSettings(float minchange, unsigned long senddelaymax, uns } -bool Sensor_MHZ19B::mhz19calibrationHandler(const HomieRange& range, const String& value) { - if (range.isRange) { - return false; //if range is given but index is not in allowed range - } - Homie.getLogger() << "mhz19 calibration " << ": " << value << endl; - - if (value=="zero") { - mhz19->calibrateZero(); - Homie.getLogger() << "mhz19 calibration " << ": " << value << endl; - #ifdef STATUSNODE - sensorNode->setProperty("status").send("MHZ19 Zero Calibration triggered"); - #endif - } else { - Homie.getLogger() << "Value outside range" << endl; - return false; - } - - return true; -} + //Called during setup void Sensor_MHZ19B::advertise(HomieNode& p_sensorNode) @@ -92,7 +74,7 @@ void Sensor_MHZ19B::advertise(HomieNode& p_sensorNode) sensorNode = &p_sensorNode; sensorNode->advertise("co2"); #ifdef MHZ19CALIBRATIONTOPIC - sensorNode->advertise("mhz19calibration").settable(&Sensor_MHZ19B::mhz19calibrationHandler)); //not working!!! TODO: Fix it + sensorNode->advertise("mhz19calibration").settable(mhz19calibrationHandler); #endif } @@ -169,4 +151,6 @@ int Sensor_MHZ19B::mhz19_readValue_reimplemented(Stream *_streamRef, MHZ19 *_mhz return co2; } - +void Sensor_MHZ19B::calibrateZero() { + mhz19->calibrateZero(); +} \ No newline at end of file diff --git a/include/sensor_mhz19b.h b/include/sensor_mhz19b.h index cd40fff..fa04d17 100644 --- a/include/sensor_mhz19b.h +++ b/include/sensor_mhz19b.h @@ -33,7 +33,7 @@ private: - //#define MHZ19CALIBRATIONTOPIC //TODO: fix it + #define MHZ19CALIBRATIONTOPIC public: Sensor_MHZ19B(int prx, int ptx); @@ -41,10 +41,9 @@ public: void init(); void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay); void advertise(HomieNode& p_sensorNode); - void sensorloop(); + void sensorloop(); - bool mhz19calibrationHandler(const HomieRange& range, const String& value); - + void calibrateZero(); }; #endif diff --git a/src/main.cpp b/src/main.cpp index 4927bb1..ce67ac0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,12 @@ #define STATUSNODE + +// data/homie/config.json hochladen mit platformio run --target uploadfs +// config contains homie device name, mqtt ip and wifi credentials + +HomieNode sensorNode("sensors", "Sensors","sensors"); //id, name, type + #include "sensordata.h" @@ -189,9 +195,30 @@ #endif #ifdef SENSOR_MHZ19B + bool mhz19calibrationHandler(const HomieRange& range, const String& value); #include "sensor_mhz19b.cpp" Sensor_MHZ19B sensor_mhz19b(SENSOR_MHZ19B_SERIAL_RX,SENSOR_MHZ19B_SERIAL_TX); + bool mhz19calibrationHandler(const HomieRange& range, const String& value) { + if (range.isRange) { + return false; //if range is given but index is not in allowed range + } + Homie.getLogger() << "mhz19 calibration " << ": " << value << endl; + + if (value=="zero") { + sensor_mhz19b.calibrateZero();//mhz19->calibrateZero(); + Homie.getLogger() << "mhz19 calibration " << ": " << value << endl; + #ifdef STATUSNODE + sensorNode.setProperty("status").send("MHZ19 Zero Calibration triggered"); + #endif + } else { + Homie.getLogger() << "Value outside range" << endl; + return false; + } + + return true; + } + #ifndef SENSOR_MHZ19B_minchange #define SENSOR_MHZ19B_minchange 10.0 #endif @@ -317,12 +344,6 @@ #endif -// data/homie/config.json hochladen mit platformio run --target uploadfs -// config contains homie device name, mqtt ip and wifi credentials - - - -HomieNode sensorNode("sensors", "Sensors","sensors"); //id, name, type char tempstring[16]; //for dtostrf