add mhz19 zero calibration

This commit is contained in:
interfisch 2020-12-31 17:47:13 +01:00
parent f051321c81
commit dbaf926def
2 changed files with 56 additions and 3 deletions

View File

@ -16,8 +16,8 @@
#data_dir=data_sensoresp1 #data_dir=data_sensoresp1
#data_dir=data_sensoresp2 #data_dir=data_sensoresp2
#data_dir=data_sensoresp3 #data_dir=data_sensoresp3
#data_dir=data_sensoresp4 data_dir=data_sensoresp4
data_dir=data_sensoresp5 #data_dir=data_sensoresp5
#Outdoor #Outdoor

View File

@ -15,6 +15,7 @@
#define FW_NAME "sensoresp" //gets printed on topic/$fw/name #define FW_NAME "sensoresp" //gets printed on topic/$fw/name
#define FW_VERSION "1.0.0" //gets printed on topic/$fw/version #define FW_VERSION "1.0.0" //gets printed on topic/$fw/version
#define STATUSNODE
struct sensordata struct sensordata
{ {
@ -27,6 +28,7 @@ struct sensordata
}; };
#ifdef SENSOR_DHT22 #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 // Digital pin connected to the DHT sensor. // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
#include <Adafruit_Sensor.h> //required for dht library #include <Adafruit_Sensor.h> //required for dht library
@ -110,7 +112,7 @@ struct sensordata
/* /*
* MHZ19 Library: https://platformio.org/lib/show/1620/SevSegSPI * MHZ19 Library: https://platformio.org/lib/show/1620/SevSegSPI
* Software Serial Library: https://platformio.org/lib/show/168/EspSoftwareSerial * 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
//SW Serial RX: to mhz19 tx (green cable) //SW Serial RX: to mhz19 tx (green cable)
@ -142,12 +144,18 @@ struct sensordata
SoftwareSerial mhz19_swSerial; SoftwareSerial mhz19_swSerial;
#define BAUD_RATE_MHZ19 9600 #define BAUD_RATE_MHZ19 9600
#define MHZ19CALIBRATIONTOPIC
#include <MHZ19.h> #include <MHZ19.h>
MHZ19 mhz19; MHZ19 mhz19;
bool mhz19_ready=false; bool mhz19_ready=false;
int value_co2=-1; //[ppm] int value_co2=-1; //[ppm]
int mhz19_readValue_reimplemented(Stream *_streamRef, MHZ19 *_mhz19Ref); //declare function int mhz19_readValue_reimplemented(Stream *_streamRef, MHZ19 *_mhz19Ref); //declare function
#ifdef MHZ19CALIBRATIONTOPIC
bool mhz19calibrationHandler(const HomieRange& range, const String& value);
#endif
#endif #endif
@ -155,6 +163,8 @@ struct sensordata
struct sensordata dataSDS018_pm25; struct sensordata dataSDS018_pm25;
struct sensordata dataSDS018_pm10; 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 //SDS18 dust sensor for 2.5µm and 10µm
//Needs 5V //Needs 5V
@ -252,6 +262,7 @@ void setup() {
delay(1000); //wait for sensors to get powered delay(1000); //wait for sensors to get powered
#ifdef SENSOR_DHT22 #ifdef SENSOR_DHT22
Serial.println("initializing dht"); Serial.println("initializing dht");
dht.begin(); dht.begin();
@ -358,8 +369,11 @@ void setup() {
if (mhz19abctries>0) { if (mhz19abctries>0) {
Serial.println("mhz19 abc disabled successfully"); Serial.println("mhz19 abc disabled successfully");
} }
#endif #endif
#ifdef SENSOR_SDS018 #ifdef SENSOR_SDS018
Serial.println("initializing sds018"); Serial.println("initializing sds018");
sds018_swSerial.begin(BAUD_RATE_SDS018, SWSERIAL_8N1, SDS018_SERIAL_RX, SDS018_SERIAL_TX, false, 256); sds018_swSerial.begin(BAUD_RATE_SDS018, SWSERIAL_8N1, SDS018_SERIAL_RX, SDS018_SERIAL_TX, false, 256);
@ -414,12 +428,19 @@ void setup() {
#endif #endif
#endif #endif
// ##### Advertise topics below here #####
//Homie_setFirmware(FW_NAME, FW_VERSION); //Homie_setFirmware(FW_NAME, FW_VERSION);
//Homie_setBrand(FW_NAME); //Homie_setBrand(FW_NAME);
Homie_setFirmware(FW_NAME, FW_VERSION); Homie_setFirmware(FW_NAME, FW_VERSION);
Homie.setLoopFunction(loopHandler); 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 #ifdef SENSOR_DHT22
@ -466,6 +487,9 @@ void setup() {
#ifdef SENSOR_MHZ19 #ifdef SENSOR_MHZ19
sensorNode.advertise("co2"); sensorNode.advertise("co2");
#ifdef MHZ19CALIBRATIONTOPIC
sensorNode.advertise("mhz19calibration").settable(mhz19calibrationHandler);
#endif
#endif #endif
#ifdef SENSOR_SDS018 #ifdef SENSOR_SDS018
@ -893,6 +917,9 @@ void loop_TCS34725_lux()
lastread_tcs34725=millis(); lastread_tcs34725=millis();
if (tcs.isSaturated){ if (tcs.isSaturated){
Serial.println("Warning: tcs34725 is saturated"); 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); //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 #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 ######## * ######## HELPER FUNCTIONS ########