From dbaf926def83fc3b032d5942a84651e24167d268 Mon Sep 17 00:00:00 2001 From: Fisch Date: Thu, 31 Dec 2020 17:47:13 +0100 Subject: [PATCH] add mhz19 zero calibration --- platformio.ini | 4 ++-- src/main.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/platformio.ini b/platformio.ini index c87cdc1..5442b89 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,8 +16,8 @@ #data_dir=data_sensoresp1 #data_dir=data_sensoresp2 #data_dir=data_sensoresp3 -#data_dir=data_sensoresp4 -data_dir=data_sensoresp5 +data_dir=data_sensoresp4 +#data_dir=data_sensoresp5 #Outdoor diff --git a/src/main.cpp b/src/main.cpp index 21ce707..0f542c1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,7 @@ #define FW_NAME "sensoresp" //gets printed on topic/$fw/name #define FW_VERSION "1.0.0" //gets printed on topic/$fw/version +#define STATUSNODE struct sensordata { @@ -27,6 +28,7 @@ struct sensordata }; + #ifdef SENSOR_DHT22 // Digital pin connected to the DHT sensor. // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed #include //required for dht library @@ -110,7 +112,7 @@ struct sensordata /* * MHZ19 Library: https://platformio.org/lib/show/1620/SevSegSPI * Software Serial Library: https://platformio.org/lib/show/168/EspSoftwareSerial - * SDS018 example: https://github.com/crystaldust/sds018/blob/master/sds018.ino + */ // SW Serial //SW Serial RX: to mhz19 tx (green cable) @@ -142,12 +144,18 @@ struct sensordata SoftwareSerial mhz19_swSerial; #define BAUD_RATE_MHZ19 9600 + #define MHZ19CALIBRATIONTOPIC + #include MHZ19 mhz19; bool mhz19_ready=false; int value_co2=-1; //[ppm] int mhz19_readValue_reimplemented(Stream *_streamRef, MHZ19 *_mhz19Ref); //declare function + + #ifdef MHZ19CALIBRATIONTOPIC + bool mhz19calibrationHandler(const HomieRange& range, const String& value); + #endif #endif @@ -155,6 +163,8 @@ struct sensordata struct sensordata dataSDS018_pm25; struct sensordata dataSDS018_pm10; + // SDS018 example: https://github.com/crystaldust/sds018/blob/master/sds018.ino + //SDS18 dust sensor for 2.5µm and 10µm //Needs 5V @@ -252,6 +262,7 @@ void setup() { delay(1000); //wait for sensors to get powered + #ifdef SENSOR_DHT22 Serial.println("initializing dht"); dht.begin(); @@ -358,8 +369,11 @@ void setup() { if (mhz19abctries>0) { Serial.println("mhz19 abc disabled successfully"); } + #endif + + #ifdef SENSOR_SDS018 Serial.println("initializing sds018"); sds018_swSerial.begin(BAUD_RATE_SDS018, SWSERIAL_8N1, SDS018_SERIAL_RX, SDS018_SERIAL_TX, false, 256); @@ -414,12 +428,19 @@ void setup() { #endif #endif + + // ##### Advertise topics below here ##### //Homie_setFirmware(FW_NAME, FW_VERSION); //Homie_setBrand(FW_NAME); Homie_setFirmware(FW_NAME, FW_VERSION); Homie.setLoopFunction(loopHandler); + #ifdef STATUSNODE + //to return some stuff about status, errors etc. + Serial.println("Using status node"); + sensorNode.advertise("status"); + #endif #ifdef SENSOR_DHT22 @@ -466,6 +487,9 @@ void setup() { #ifdef SENSOR_MHZ19 sensorNode.advertise("co2"); + #ifdef MHZ19CALIBRATIONTOPIC + sensorNode.advertise("mhz19calibration").settable(mhz19calibrationHandler); + #endif #endif #ifdef SENSOR_SDS018 @@ -893,6 +917,9 @@ void loop_TCS34725_lux() lastread_tcs34725=millis(); if (tcs.isSaturated){ Serial.println("Warning: tcs34725 is saturated"); + #ifdef STATUSNODE + sensorNode.setProperty("status").send("TCS34725 is saturated"); + #endif } } //value_tcs_lux = tcs.calculateLux(value_tcs_r, value_tcs_g, value_tcs_b); @@ -1308,6 +1335,32 @@ void ICACHE_RAM_ATTR interrupt_raingauge() #endif +/* ################################# +* ########### topic handler ####### +*/ + +#ifdef MHZ19CALIBRATIONTOPIC + 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") { + 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; +} +#endif + /*################################## * ######## HELPER FUNCTIONS ########