180 lines
5.3 KiB
C++
180 lines
5.3 KiB
C++
//Connect SCL to D1, SDA to D2, GND and 3v3
|
|
#include "sensor_multichannelgasgmxxx.h"
|
|
|
|
|
|
|
|
Sensor_MULTICHANNELGASGMXXX::Sensor_MULTICHANNELGASGMXXX()
|
|
{
|
|
gas = new GAS_GMXXX<TwoWire>();
|
|
}
|
|
|
|
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");
|
|
*/ |