From 5f9c1f787c5827949b82005e13ff09813c1c3326 Mon Sep 17 00:00:00 2001 From: Fisch Date: Sun, 21 Nov 2021 17:51:04 +0100 Subject: [PATCH] add grove gas sensor v2 multichannel gm-xxx --- include/sensor_multichannelgasgmxxx.cpp | 180 ++++++++++++++++++++++++ include/sensor_multichannelgasgmxxx.h | 43 ++++++ platformio.ini | 19 ++- src/main.cpp | 68 +++++++++ 4 files changed, 309 insertions(+), 1 deletion(-) create mode 100644 include/sensor_multichannelgasgmxxx.cpp create mode 100644 include/sensor_multichannelgasgmxxx.h diff --git a/include/sensor_multichannelgasgmxxx.cpp b/include/sensor_multichannelgasgmxxx.cpp new file mode 100644 index 0000000..c38f2c6 --- /dev/null +++ b/include/sensor_multichannelgasgmxxx.cpp @@ -0,0 +1,180 @@ +//Connect SCL to D1, SDA to D2, GND and 3v3 +#include "sensor_multichannelgasgmxxx.h" + + + +Sensor_MULTICHANNELGASGMXXX::Sensor_MULTICHANNELGASGMXXX() +{ + gas = new GAS_GMXXX(); +} + +void Sensor_MULTICHANNELGASGMXXX::init() //Things to be done during setup() +{ + Serial.println("initializing MULTICHANNELGASGMXXX"); + //The default addrss is 0x08; + //gas.begin(MyWire, 0x08); // use the software I2C + //gas.setAddress(0x64); change thee I2C address + gas->begin(Wire, 0x08); + + init_ok=true; +} + + + +//Also called during setup() +void Sensor_MULTICHANNELGASGMXXX::setSettings_no2(float minchange, unsigned long senddelaymax, unsigned long readdelay) +{ + data_no2.minchange=minchange; + data_no2.senddelaymax=senddelaymax; + data_no2.readdelay=readdelay; +} +void Sensor_MULTICHANNELGASGMXXX::setSettings_c2h5oh(float minchange, unsigned long senddelaymax, unsigned long readdelay) +{ + data_c2h5oh.minchange=minchange; + data_c2h5oh.senddelaymax=senddelaymax; + data_c2h5oh.readdelay=readdelay; +} +void Sensor_MULTICHANNELGASGMXXX::setSettings_voc(float minchange, unsigned long senddelaymax, unsigned long readdelay) +{ + data_voc.minchange=minchange; + data_voc.senddelaymax=senddelaymax; + data_voc.readdelay=readdelay; +} +void Sensor_MULTICHANNELGASGMXXX::setSettings_co(float minchange, unsigned long senddelaymax, unsigned long readdelay) +{ + data_co.minchange=minchange; + data_co.senddelaymax=senddelaymax; + data_co.readdelay=readdelay; +} + + +//Called during setup +void Sensor_MULTICHANNELGASGMXXX::advertise(HomieNode& p_sensorNode) +{ + sensorNode = &p_sensorNode; + sensorNode->advertise("no2"); + sensorNode->advertise("c2h5oh"); + sensorNode->advertise("voc"); + sensorNode->advertise("co"); +} + + +void Sensor_MULTICHANNELGASGMXXX::sensorloop_no2() +{ + if (init_ok) { + sensordata &d=data_no2; + + bool _changed=false; + if (millis() >= (d.lastreadtime+d.readdelay)) { + d.value = gas->calcVol(gas->measure_NO2()); // Volt + if (fabs(d.lastsentvalue-d.value)>=d.minchange){ + _changed=true; + } + d.lastreadtime=millis(); + } + + if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { + Serial.print("Sending MULTICHANNELGASGMXXX no2. reason="); + if (_changed) Serial.println("change"); else Serial.println("time"); + + Homie.getLogger() << "no2 " << ": " << d.value << endl; + sensorNode->setProperty("no2").send(String(d.value)); + d.lastsentvalue=d.value; + + d.lastsent=millis(); + } + } +} + +void Sensor_MULTICHANNELGASGMXXX::sensorloop_c2h5oh() +{ + if (init_ok) { + sensordata &d=data_c2h5oh; + + bool _changed=false; + if (millis() >= (d.lastreadtime+d.readdelay)) { + d.value = gas->calcVol(gas->measure_C2H5OH()); // Volt + if (fabs(d.lastsentvalue-d.value)>=d.minchange){ + _changed=true; + } + d.lastreadtime=millis(); + } + + if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { + Serial.print("Sending MULTICHANNELGASGMXXX c2h5oh. reason="); + if (_changed) Serial.println("change"); else Serial.println("time"); + + Homie.getLogger() << "c2h5oh " << ": " << d.value << endl; + sensorNode->setProperty("c2h5oh").send(String(d.value)); + d.lastsentvalue=d.value; + + d.lastsent=millis(); + } + } +} + +void Sensor_MULTICHANNELGASGMXXX::sensorloop_voc() +{ + if (init_ok) { + sensordata &d=data_voc; + + bool _changed=false; + if (millis() >= (d.lastreadtime+d.readdelay)) { + d.value = gas->calcVol(gas->measure_VOC()); // Volt + if (fabs(d.lastsentvalue-d.value)>=d.minchange){ + _changed=true; + } + d.lastreadtime=millis(); + } + + if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { + Serial.print("Sending MULTICHANNELGASGMXXX voc. reason="); + if (_changed) Serial.println("change"); else Serial.println("time"); + + Homie.getLogger() << "voc " << ": " << d.value << endl; + sensorNode->setProperty("voc").send(String(d.value)); + d.lastsentvalue=d.value; + + d.lastsent=millis(); + } + } +} + +void Sensor_MULTICHANNELGASGMXXX::sensorloop_co() +{ + if (init_ok) { + sensordata &d=data_co; + + bool _changed=false; + if (millis() >= (d.lastreadtime+d.readdelay)) { + d.value = gas->calcVol(gas->measure_CO()); // Volt + if (fabs(d.lastsentvalue-d.value)>=d.minchange){ + _changed=true; + } + d.lastreadtime=millis(); + } + + if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { + Serial.print("Sending MULTICHANNELGASGMXXX co. reason="); + if (_changed) Serial.println("change"); else Serial.println("time"); + + Homie.getLogger() << "co " << ": " << d.value << endl; + sensorNode->setProperty("co").send(String(d.value)); + d.lastsentvalue=d.value; + + d.lastsent=millis(); + } + } +} + + +/* +val = gas.measure_NO2(); Serial.print("NO2: "); Serial.print(val); Serial.print(" eq "); +Serial.print(gas.calcVol(val)); Serial.println("V"); +val = gas.measure_C2H5OH(); Serial.print("C2H5OH: "); Serial.print(val); Serial.print(" eq "); +Serial.print(gas.calcVol(val)); Serial.println("V"); +val = gas.measure_VOC(); Serial.print("VOC: "); Serial.print(val); Serial.print(" eq "); +Serial.print(gas.calcVol(val)); Serial.println("V"); +val = gas.measure_CO(); Serial.print("CO: "); Serial.print(val); Serial.print(" eq "); +Serial.print(gas.calcVol(val)); Serial.println("V"); +*/ \ No newline at end of file diff --git a/include/sensor_multichannelgasgmxxx.h b/include/sensor_multichannelgasgmxxx.h new file mode 100644 index 0000000..c00bb48 --- /dev/null +++ b/include/sensor_multichannelgasgmxxx.h @@ -0,0 +1,43 @@ +#ifndef SENSOR_MULTICHANNELGASGMXXX_H +#define SENSOR_MULTICHANNELGASGMXXX_H + +#include "sensordata.h" +#include + +#include +#include + + +class Sensor_MULTICHANNELGASGMXXX +{ + +private: + GAS_GMXXX *gas; + HomieNode *sensorNode; //reference to HomieNode + + struct sensordata data_no2; //struct values are changed in setup() + struct sensordata data_c2h5oh; + struct sensordata data_voc; + struct sensordata data_co; + + + bool init_ok; + +public: + Sensor_MULTICHANNELGASGMXXX(); + + void init(); + void setSettings_no2(float minchange, unsigned long senddelaymax, unsigned long readdelay); + void setSettings_c2h5oh(float minchange, unsigned long senddelaymax, unsigned long readdelay); + void setSettings_voc(float minchange, unsigned long senddelaymax, unsigned long readdelay); + void setSettings_co(float minchange, unsigned long senddelaymax, unsigned long readdelay); + void advertise(HomieNode& p_sensorNode); + void sensorloop_no2(); + void sensorloop_c2h5oh(); + void sensorloop_voc(); + void sensorloop_co(); + +}; + +#endif + diff --git a/platformio.ini b/platformio.ini index f8d785f..70007b8 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,8 +18,9 @@ #data_dir=data_sensoresp3 #data_dir=data_sensoresp4 #data_dir=data_sensoresp5 +data_dir=data_sensoresp6 #data_dir=data_sensoresp3dprinter -data_dir=data_sensoresptest +#data_dir=data_sensoresptest #Outdoor @@ -273,6 +274,22 @@ lib_deps = pololu/VL53L1X @ ^1.3.0 +#Flur Gas, motion +[env:sensoresp6] +platform = espressif8266 @ 2.5.0 +board = d1_mini +framework = arduino + +monitor_port = /dev/ttyUSB0 +monitor_speed = 115200 + +build_flags = + -D SENSOR_MULTICHANNELGASGMXXX + +lib_deps = + https://github.com/Seeed-Studio/Seeed_Arduino_MultiGas + ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error + Homie@3.0.0 #3D Printer [env:sensoresp3dprinter] diff --git a/src/main.cpp b/src/main.cpp index ce67ac0..018db66 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -345,6 +345,55 @@ HomieNode sensorNode("sensors", "Sensors","sensors"); //id, name, type #endif + +#ifdef SENSOR_MULTICHANNELGASGMXXX + #include "sensor_multichannelgasgmxxx.cpp" + Sensor_MULTICHANNELGASGMXXX sensor_multichannelgasgmxxx; + + #ifndef SENSOR_MULTICHANNELGASGMXXX_no2_minchange + #define SENSOR_MULTICHANNELGASGMXXX_no2_minchange 0.1 + #endif + #ifndef SENSOR_MULTICHANNELGASGMXXX_no2_senddelaymax + #define SENSOR_MULTICHANNELGASGMXXX_no2_senddelaymax 600000 + #endif + #ifndef SENSOR_MULTICHANNELGASGMXXX_no2_readdelay + #define SENSOR_MULTICHANNELGASGMXXX_no2_readdelay 10000 + #endif + + #ifndef SENSOR_MULTICHANNELGASGMXXX_c2h5oh_minchange + #define SENSOR_MULTICHANNELGASGMXXX_c2h5oh_minchange 0.1 + #endif + #ifndef SENSOR_MULTICHANNELGASGMXXX_c2h5oh_senddelaymax + #define SENSOR_MULTICHANNELGASGMXXX_c2h5oh_senddelaymax 600000 + #endif + #ifndef SENSOR_MULTICHANNELGASGMXXX_c2h5oh_readdelay + #define SENSOR_MULTICHANNELGASGMXXX_c2h5oh_readdelay 10000 + #endif + + #ifndef SENSOR_MULTICHANNELGASGMXXX_voc_minchange + #define SENSOR_MULTICHANNELGASGMXXX_voc_minchange 0.1 + #endif + #ifndef SENSOR_MULTICHANNELGASGMXXX_voc_senddelaymax + #define SENSOR_MULTICHANNELGASGMXXX_voc_senddelaymax 600000 + #endif + #ifndef SENSOR_MULTICHANNELGASGMXXX_voc_readdelay + #define SENSOR_MULTICHANNELGASGMXXX_voc_readdelay 10000 + #endif + + #ifndef SENSOR_MULTICHANNELGASGMXXX_co_minchange + #define SENSOR_MULTICHANNELGASGMXXX_co_minchange 0.1 + #endif + #ifndef SENSOR_MULTICHANNELGASGMXXX_co_senddelaymax + #define SENSOR_MULTICHANNELGASGMXXX_co_senddelaymax 600000 + #endif + #ifndef SENSOR_MULTICHANNELGASGMXXX_co_readdelay + #define SENSOR_MULTICHANNELGASGMXXX_co_readdelay 10000 + #endif + + +#endif + + char tempstring[16]; //for dtostrf @@ -445,6 +494,14 @@ void setup() { sensor_raingauge.setSettings(SENSOR_Raingauge_minchange,SENSOR_Raingauge_senddelaymax,SENSOR_Raingauge_readdelay); #endif + #ifdef SENSOR_MULTICHANNELGASGMXXX + sensor_multichannelgasgmxxx.init(); + sensor_multichannelgasgmxxx.setSettings_no2(SENSOR_MULTICHANNELGASGMXXX_no2_minchange,SENSOR_MULTICHANNELGASGMXXX_no2_senddelaymax,SENSOR_MULTICHANNELGASGMXXX_no2_readdelay); + sensor_multichannelgasgmxxx.setSettings_c2h5oh(SENSOR_MULTICHANNELGASGMXXX_c2h5oh_minchange,SENSOR_MULTICHANNELGASGMXXX_c2h5oh_senddelaymax,SENSOR_MULTICHANNELGASGMXXX_c2h5oh_readdelay); + sensor_multichannelgasgmxxx.setSettings_voc(SENSOR_MULTICHANNELGASGMXXX_voc_minchange,SENSOR_MULTICHANNELGASGMXXX_voc_senddelaymax,SENSOR_MULTICHANNELGASGMXXX_voc_readdelay); + sensor_multichannelgasgmxxx.setSettings_co(SENSOR_MULTICHANNELGASGMXXX_co_minchange,SENSOR_MULTICHANNELGASGMXXX_co_senddelaymax,SENSOR_MULTICHANNELGASGMXXX_co_readdelay); + #endif + // ##### Advertise topics below here ##### @@ -519,6 +576,10 @@ void setup() { #ifdef SENSOR_RAINGAUGE sensor_raingauge.advertise(sensorNode); #endif + + #ifdef SENSOR_MULTICHANNELGASGMXXX + sensor_multichannelgasgmxxx.advertise(sensorNode); + #endif Serial.println("connecting.."); @@ -596,6 +657,13 @@ void loopHandler() { sensor_raingauge.sensorloop(); #endif + #ifdef SENSOR_MULTICHANNELGASGMXXX + sensor_multichannelgasgmxxx.sensorloop_no2(); + sensor_multichannelgasgmxxx.sensorloop_c2h5oh(); + sensor_multichannelgasgmxxx.sensorloop_voc(); + sensor_multichannelgasgmxxx.sensorloop_co(); + #endif + }