6#include <Adafruit_BME280.h>
7#include <Adafruit_BME680.h>
8#include <Adafruit_BMP280.h>
9#include <Adafruit_SCD30.h>
10#include <Adafruit_SHT31.h>
11#include <Adafruit_Sensor.h>
13#include <DFRobot_MultiGasSensor.h>
15#include <NOxGasIndexAlgorithm.h>
16#include <SensirionI2CScd4x.h>
17#include <SensirionI2CSen5x.h>
18#include <SensirionI2CSgp41.h>
19#include <SparkFun_Particle_Sensor_SN-GCJA5_Arduino_Library.h>
20#include <VOCGasIndexAlgorithm.h>
22#include <cm1106_uart.h>
23#include <drivers/NoiseSlave.h>
24#include <drivers/PMS5003T.h>
25#include <drivers/geiger.h>
26#include <drivers/pm1006.h>
32#include <dht_nonblocking.h>
35#define CSL_VERSION "0.7.7"
36#define CSL_REVISION 386
51#ifndef DFROBOT_CO_I2C_ADDR
52#define DFROBOT_CO_I2C_ADDR 0x78
54#ifndef DFROBOT_O3_I2C_ADDR
55#define DFROBOT_O3_I2C_ADDR 0x79
57#ifndef DFROBOT_NH3_I2C_ADDR
58#define DFROBOT_NH3_I2C_ADDR 0x7A
60#ifndef DFROBOT_NO2_I2C_ADDR
61#define DFROBOT_NO2_I2C_ADDR 0x7B
120#define I2C1_SDA_PIN 11
121#define I2C1_SCL_PIN 12
125#define SENSOR_RETRY 1000
128#if defined(ARDUINO_ARCH_ESP32)
129#define SENSOR_COMMS SERIALPORT2
131#define SENSOR_COMMS 3
135#define SENSOR_UNITS \
136 X(NUNIT, "NUNIT", "NUNIT") \
137 X(PM1, "ug/m3", "PM1") \
138 X(PM25, "ug/m3", "PM2.5") \
139 X(PM4, "ug/m3", "PM4") \
140 X(PM10, "ug/m3", "PM10") \
145 X(CO2, "ppm", "CO2") \
146 X(CO2TEMP, "C", "CO2T") \
147 X(CO2TEMPK, "K", "CO2TK") \
148 X(CO2TEMPF, "F", "CO2TF") \
149 X(CO2HUM, "%", "CO2H") \
150 X(PRESS, "hPa", "P") \
152 X(GAS, "Ohm", "Gas") \
153 X(CPM, "CPM", "RAD") \
154 X(RAD, "uSv/h", "RAD") \
155 X(NH3, "ppm", "NH3") \
157 X(NO2, "ppm", "NO2") \
159 X(NOXI, "noxi", "NOXI") \
160 X(VOCI, "voci", "VOCI") \
161 X(NOX, "nox", "NOX") \
162 X(VOC, "voc", "VOC") \
163 X(NOISE, "dB", "Noise") \
164 X(NOISEAVG, "dB", "NoiseAvg") \
165 X(NOISEPEAK, "dB", "NoisePeak") \
166 X(NOISEMIN, "dB", "NoiseMin") \
167 X(NOISEAVGLEGAL, "dB", "NoiseAvgLegal") \
168 X(NOISEAVGLEGALMAX, "dB", "NoiseAvgLegalMax") \
169 X(NOISEL90, "dB", "NoiseL90") \
170 X(NOISELD, "dB", "Ld") \
171 X(NOISELE, "dB", "Le") \
172 X(NOISELN, "dB", "Ln") \
173 X(NOISELDEN, "dB", "Lden") \
174 X(UCOUNT, "COUNT", "UCOUNT")
176#define X(unit, symbol, name) unit,
177typedef enum UNIT :
size_t { SENSOR_UNITS } UNIT;
181#define SENSORS_TYPES \
182 X(Auto, "GENERIC", 1) \
183 X(SGCJA5, "GCJA5", 1) \
184 X(SSPS30, "SPS30", 1) \
185 X(SDS011, "SDS011", 1) \
186 X(SMHZ19, "MHZ19", 2) \
187 X(SCM1106, "CM1106", 2) \
188 X(SAIRS8, "SAIRS8", 2) \
189 X(IKEAVK, "IKEAVK", 1) \
190 X(P5003T, "PM5003T", 1) \
191 X(SSCD30, "SCD30", 2) \
192 X(SSCD4X, "SCD4X", 2) \
193 X(SSEN5X, "SEN5X", 1) \
194 X(SSHT31, "SHT31", 3) \
195 X(SBME280, "BME280", 3) \
196 X(SBMP280, "BMP280", 3) \
197 X(SBME680, "BME680", 3) \
198 X(SAHTXX, "AHTXX", 3) \
199 X(SAM232X, "AM232X", 3) \
200 X(SDHTX, "DHTX", 3) \
201 X(SDFRCO, "DFRCO", 3) \
202 X(SDFRNH3, "DFRNH3", 3) \
203 X(SDFRNO2, "DFRNO2", 3) \
204 X(SDFRO3, "DFRO3", 3) \
205 X(SCAJOE, "CAJOE", 3) \
206 X(SSGP41, "SGP41", 3) \
207 X(SNOISE, "NOISE", 3) \
208 X(SCOUNT, "SCOUNT", 3)
210#define X(utype, uname, umaintype) utype,
211typedef enum SENSORS :
size_t { SENSORS_TYPES } SENSORS;
215enum class SensorGroup {
223enum class TEMPUNIT { CELSIUS, FAHRENHEIT, KELVIN };
225typedef void (*errorCbFn)(
const char *msg);
226typedef void (*voidCbFn)();
260 uint8_t conditioning_s = 10;
263 NOxGasIndexAlgorithm noxAlgorithm;
287 float dhthumi, dhttemp;
308 SensirionI2CSen5x sen5x;
327 void init(u_int pms_type = 0,
int pms_rx = PMS_RX,
int pms_tx = PMS_TX);
381 float getVOC()
const;
383 float getNOX()
const;
385 float getVOCI()
const;
387 float getNOXI()
const;
391 float getNoise()
const;
393 float getNoiseAverage()
const;
395 float getNoisePeak()
const;
397 float getNoiseMin()
const;
399 float getNoiseLegalAverage()
const;
401 float getNoiseLegalMaximum()
const;
402 float getNoiseL90()
const;
403 float getNoiseLd()
const;
404 float getNoiseLe()
const;
405 float getNoiseLn()
const;
406 float getNoiseLden()
const;
407 bool sendNoiseSensorTime(uint32_t unixTime);
408 bool syncNoiseSensorTime();
409 void setNoiseSensorTimeSyncInterval(uint32_t intervalMs);
481 errorCbFn _onErrorCb =
nullptr;
483 voidCbFn _onDataCb =
nullptr;
485 int dev_uart_type = -1;
487 bool s8_uart2 =
false;
491 bool readAllComplete =
false;
493 uint8_t sensors_registered_count;
495 uint8_t units_registered_count;
497 uint8_t current_unit = 0;
515 TEMPUNIT temp_unit = TEMPUNIT::CELSIUS;
526 TwoWire *noiseWire =
nullptr;
527 SensorData noiseSensorData{};
528 bool noiseWireReady =
false;
529 uint8_t noiseSensorAddress = 0;
531 bool noiseSensorEnabled =
false;
532 float noiseInstant = 0.0;
533 float noiseAvgValue = 0.0;
534 float noisePeakValue = 0.0;
535 float noiseMinValue = 0.0;
536 float noiseAvgLegalValue = 0.0;
537 float noiseAvgLegalMaxValue = 0.0;
538 float noiseL90Value = 0.0;
539 float noiseLdValue = 0.0;
540 float noiseLeValue = 0.0;
541 float noiseLnValue = 0.0;
542 float noiseLdenValue = 0.0;
543 bool noiseScanDone =
false;
544 uint32_t noiseLastTimeSyncMs = 0;
545 uint32_t noiseLastSyncAttemptMs = 0;
546 uint32_t noiseTimeSyncIntervalMs = 86400000;
547 static constexpr uint32_t NOISE_SYNC_RETRY_MS = 30000;
548 bool noiseTimeSyncEnabled =
true;
570 void setSCD30TempOffset(
float offset);
571 float getSCD30TempOffset()
const;
572 void setSCD30AltitudeOffset(
float offset);
573 void CO2correctionAlt();
574 float hpaCalculation(
float altitude);
578 void setSCD4xTempOffset(
float offset);
579 float getSCD4xTempOffset()
const;
580 void setSCD4xAltitudeOffset(
float offset);
584 void setsen5xTempOffset(
float offset);
595 bool dhtIsReady(
float *temperature,
float *humidity);
598 void DFRobotNH3Init();
599 void DFRobotNH3Read();
600 void DFRobotCOInit();
601 void DFRobotCORead();
602 void DFRobotNO2Init();
603 void DFRobotNO2Read();
604 void DFRobotO3Init();
605 void DFRobotO3Read();
607 float dfrGasTempCompensation(
float rawPpm,
float temperature, uint8_t gasType);
608 float dfrGasPressCompensation(
float ppm,
float pressure);
609 float dfrGasHumiCompensation(
float ppm,
float humidity, uint8_t gasType);
610 bool dfrHasExternalTempSensor()
const;
614 bool sensorSerialInit(u_int pms_type,
int rx,
int tx);
615 bool pmSensorAutoDetect(u_int pms_type);
617 bool pmGenericRead();
623 bool CO2CM1106Read();
625 bool CO2CM1106Init();
626 bool senseAirS8Init();
627 bool senseAirS8Read();
628 bool sensorSerial2Init(
int rx,
int tx);
633 bool sps30UARTInit();
636 void sps30ErrToMess(
char *mess, uint8_t r);
637 void sps30Errorloop(
char *mess, uint8_t r);
638 void sps30DeviceInfo();
642 void onSensorError(
const char *msg);
648 bool serialInit(u_int pms_type,
unsigned long speed_baud,
int pms_rx,
int pms_tx);
650 String hwSerialRead(
unsigned int length_buffer);
654 void DEBUG(
const char *text,
const char *textb =
"")
const;
660 void printUART(
const char *name,
unsigned long speed,
int pin_rx,
int pin_tx);
662 void printI2C(
const char *name,
const char *i2cname,
int pin_sda,
int pin_scl);
664 void tempRegister(
bool isCO2temp);
666 void sensorRegister(SENSORS sensor);
668 void sensorAnnounce(SENSORS sensor);
670 void unitRegister(UNIT unit);
672 uint8_t *getUnitsRegistered();
674 bool noiseSensorAutoDetect();
675 void noiseSensorService();
676 void noiseSensorCollect();
677 bool noiseSensorReadIdentity(TwoWire &wire, uint8_t address, SensorIdentity &identity);
678 bool noiseSensorReadData(TwoWire &wire, uint8_t address, SensorData &out);
679 bool noiseSensorDevicePresent(TwoWire &wire, uint8_t address);
680 void noiseSensorInitWire();
684 Stream &_debugPort = WM_DEBUG_PORT;
686 Stream &_debugPort = Serial;
690#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SENSORSHANDLER)
CanAirIO Sensors Manager main class.
Definition: Sensors.hpp:232
float getNH3() const
get NH3 value in ppm
Definition: Sensors.cpp:494
float getTempOffset() const
Get temperature offset for Sensirion sensors (from internal sensor in SCD4x and SCD30)
Definition: Sensors.cpp:473
void printUnitsRegistered(bool debug=false)
print the sensor units names available
Definition: Sensors.cpp:835
void setTempOffset(float offset)
Set temperature offset for all temperature sensors.
Definition: Sensors.cpp:461
float getAltitude() const
get Altitude value in meters
Definition: Sensors.cpp:488
SensirionI2CScd4x scd4x
SCD4x object sensor.
Definition: Sensors.hpp:306
bool i2conly
only detect i2c sensors flag
Definition: Sensors.hpp:266
String getUnitSymbol(UNIT unit) const
get the sensor unit symbol
Definition: Sensors.cpp:691
int getUARTDeviceTypeSelected() const
UART only: get the UART sensor type. See SENSORS enum. Also getDeviceName()
Definition: Sensors.cpp:591
DFRobot_GAS_I2C dfrNH3
DFRobot gravity NH3 object sensor addr 0x7A.
Definition: Sensors.hpp:314
uint16_t getPM10() const
get PM10 ug/m3 value
Definition: Sensors.cpp:352
void loop()
Main sensors loop. All sensors are read here, please call it on main loop.
Definition: Sensors.cpp:72
void setCO2AltitudeOffset(float altitude)
set CO2 altitude offset (m)
Definition: Sensors.cpp:290
void resetAllVariables()
reset all library variables (generic sensors units)
Definition: Sensors.cpp:2604
String getSensorName(SENSORS sensor) const
get the sensor name
Definition: Sensors.cpp:625
float getTOffset() const
Get sensorlib actual internal temperature offset.
Definition: Sensors.cpp:454
uint16_t getPM1() const
get PM1.0 ug/m3 value
Definition: Sensors.cpp:343
bool isUnitRegistered(UNIT unit) const
get the sensor unit status on the registry
Definition: Sensors.cpp:656
float getNO2() const
get NO2 value in ppm
Definition: Sensors.cpp:500
float getCO() const
get CO value in ppm
Definition: Sensors.cpp:497
int16_t getLibraryRevision() const
return the current revision code number
Definition: Sensors.cpp:603
CM1106_UART * cm1106
CM1106 UART main object sensor.
Definition: Sensors.hpp:294
String getUnitName(UNIT unit) const
get the sensor unit name
Definition: Sensors.cpp:681
bool devmode
Debug mode for increase verbose.
Definition: Sensors.hpp:238
void resetUnitsRegister()
reset the sensor units registry.
Definition: Sensors.cpp:714
void setCO2RecalibrationFactor(int ppmValue)
set CO2 recalibration PPM value (400 to 2000)
Definition: Sensors.cpp:253
String getLibraryVersion() const
returns the CanAirIO Sensorslib version
Definition: Sensors.cpp:600
AHTxx aht10
AHTXX sensors object.
Definition: Sensors.hpp:281
float altoffset
Altitude compensation variable.
Definition: Sensors.hpp:247
bool readAllSensors()
Read all sensors but use only one time or use loop() instead. All sensors are read here....
Definition: Sensors.cpp:101
uint8_t getUnitsRegisteredCount() const
get device sensors units detected count
Definition: Sensors.cpp:674
void initTOffset(float offset)
Initialize internal temperature offset to be used on startup.
Definition: Sensors.cpp:447
Adafruit_BMP280 bmp280
BMP280 object (Humidity, Pressure, Altitude and Temperature)
Definition: Sensors.hpp:277
struct sps_values val
SPS30 values. Only for Sensirion SPS30 sensor.
Definition: Sensors.hpp:235
bool isDataReady()
get the sensor status
Definition: Sensors.cpp:340
Adafruit_BME280 bme280
BME280 object (Humidity, Pressure, Altitude and Temperature)
Definition: Sensors.hpp:275
DFRobot_GAS_I2C dfrO3
DFRobot gravity O3 object sensor add 0x79.
Definition: Sensors.hpp:318
VOCGasIndexAlgorithm vocAlgorithm
Sensirion gas index algorithms for SGP41 raw signal processing.
Definition: Sensors.hpp:262
void enableGeigerSensor(int gpio)
Enable Geiger sensor on specific pin.
Definition: Sensors.cpp:2649
void setOnDataCallBack(voidCbFn cb)
Get sensor data.
Definition: Sensors.cpp:325
float getHumidity() const
get humidity % value of environment sensor
Definition: Sensors.cpp:361
AM232X am2320
AM2320 object (Humidity and temperature)
Definition: Sensors.hpp:273
Adafruit_SCD30 scd30
SCD30 object sensor.
Definition: Sensors.hpp:292
uint16_t getPM25() const
get PM2.5 ug/m3 value
Definition: Sensors.cpp:346
float getUnitValue(UNIT unit)
get the sensor unit value (float)
Definition: Sensors.cpp:750
void resetSensorsRegister()
reset the sensor registry.
Definition: Sensors.cpp:727
float getGas() const
get Gas resistance value of BMP680 sensor
Definition: Sensors.cpp:485
CM1106_sensor cm1106sensor
CM1106 UART main variable.
Definition: Sensors.hpp:296
Adafruit_SHT31 sht31
SHT31 object (Humidity and temperature)
Definition: Sensors.hpp:283
void setSeaLevelPressure(float hpa)
set the sea level pressure (hPa)
Definition: Sensors.cpp:312
bool isUARTSensorConfigured() const
UART only: check if the UART sensor is registered.
Definition: Sensors.cpp:585
bool isSensorRegistered(SENSORS sensor) const
Read and check the sensors status on initialization.
Definition: Sensors.cpp:613
float getGeigerMicroSievertHour(void) const
get Geiger count in uSv/h units
Definition: Sensors.cpp:2674
uint16_t getCO2() const
get CO2 ppm value
Definition: Sensors.cpp:355
SFE_PARTICLE_SENSOR pmGCJA5
Panasonic SN-GCJA5 object sensor.
Definition: Sensors.hpp:300
Adafruit_BME680 bme680
BME680 object (Humidity, Gas, IAQ, Pressure, Altitude and Temperature)
Definition: Sensors.hpp:279
DFRobot_GAS_I2C dfrNO2
DFRobot gravity NO2 object sensor add 0x7B.
Definition: Sensors.hpp:316
SensorGroup getSensorGroup(SENSORS sensor) const
get the sensor group type
Definition: Sensors.cpp:638
PMS5003T * pm5003t
PMS5003T Plantower with T&H of Airgradient.
Definition: Sensors.hpp:322
PM1006 * pm1006
IKEA Vindriktn object sensor.
Definition: Sensors.hpp:310
float sealevel
Sea level pressure (hPa)
Definition: Sensors.hpp:250
float getO3() const
get O3 value in ppm
Definition: Sensors.cpp:503
void init(u_int pms_type=0, int pms_rx=PMS_RX, int pms_tx=PMS_TX)
All sensors init.
Definition: Sensors.cpp:155
float getTemperature() const
get temperature value from environment sensor
Definition: Sensors.cpp:400
void setOnErrorCallBack(errorCbFn cb)
Optional callback for get the sensors errors.
Definition: Sensors.cpp:331
uint32_t getGeigerCPM(void) const
get Geiger count. Tics in the last 60secs
Definition: Sensors.cpp:2663
float getCO2humi() const
get humidity % value of CO2 sensor device
Definition: Sensors.cpp:358
float hpa
Altitude hpa calculation.
Definition: Sensors.hpp:253
void setSampleTime(int seconds)
set loop time interval for each sensor sample
Definition: Sensors.cpp:237
void setDebugMode(bool enable)
Optional for increase the debug level.
Definition: Sensors.cpp:337
SensirionI2CSgp41 sgp41
Sensirion sgp41 library (Rh, T, Voc, Nox)
Definition: Sensors.hpp:259
uint16_t getPM4() const
get PM4 ug/m3 value
Definition: Sensors.cpp:349
float toffset
Temperature offset (for final temp output)
Definition: Sensors.hpp:244
CM1106_ABC abc
CM1106 UART calibration object.
Definition: Sensors.hpp:298
UNIT getNextUnit()
get the next sensor unit available
Definition: Sensors.cpp:697
S8_UART * s8
SenseAir S8 CO2 object sensor.
Definition: Sensors.hpp:302
DFRobot_GAS_I2C dfrCO
DFRobot gravity CO object sensor addr 0x78.
Definition: Sensors.hpp:312
MHZ19 mhz19
Mhz19 object sensor.
Definition: Sensors.hpp:290
int sample_time
Initial sample time for all sensors.
Definition: Sensors.hpp:241
float getCO2temp() const
get temperature value from the CO2 sensor device
Definition: Sensors.cpp:387
S8_sensor s8sensor
SenseAir S8 CO2 object sensor.
Definition: Sensors.hpp:304
void detectI2COnly(bool enable)
Forced to enable I2C sensors only. Recommended to use only if you are using a I2C sensor and improve ...
Definition: Sensors.cpp:597
void resetNextUnit()
reset the next sensor unit counter.
Definition: Sensors.cpp:740
void setTemperatureUnit(TEMPUNIT tunit)
set the temperature type unit
Definition: Sensors.cpp:367
void printSensorsRegistered(bool debug=false)
print the sensor names detected
Definition: Sensors.cpp:850
uint8_t getSensorsRegisteredCount() const
get device sensors detected count
Definition: Sensors.cpp:606
float getPressure() const
get Pressure value in hPa
Definition: Sensors.cpp:491
GEIGER * rad
Geiger CAJOE object sensor.
Definition: Sensors.hpp:320
uint8_t * getSensorsRegistered()
get the sensor registry for retrieve the sensor names
Definition: Sensors.cpp:648
SPS30 sps30
Sensirion dust SPS30 library.
Definition: Sensors.hpp:256