diff --git a/WifiControlSensor/SCD4X.h b/WifiControlSensor/SCD4X.h new file mode 100644 index 0000000..b084c9a --- /dev/null +++ b/WifiControlSensor/SCD4X.h @@ -0,0 +1,23 @@ +#pragma once +#ifdef CONFIG_ENABLE_SCD4X +#include "debug_sketch.h" + +int SCD4XGetMeasure(float &t, float &h, uint16_t &co2); +int SCD4XSetup(); +bool SCD4XIsConnected(); + +#else +int SCD4XGetMeasure(float &, float &, uint16_t &) +{ + return -1; +}; +int SCD4XSetup() +{ + SKETCH_DEBUG_PRINTLN("SCD4X is disabled at build time"); + return -1; +}; +bool SCD4XIsConnected() +{ + return false; +}; +#endif diff --git a/WifiControlSensor/SCD4X.ino b/WifiControlSensor/SCD4X.ino new file mode 100644 index 0000000..faa4bc4 --- /dev/null +++ b/WifiControlSensor/SCD4X.ino @@ -0,0 +1,61 @@ +#ifdef CONFIG_ENABLE_SCD4X +#include "SCD4X.h" +#include +#include +#include + +SensirionI2CScd4x scd4x; + +int SCD4XConnected = 0; + +int SCD4XSetup() +{ + Wire.begin(); + scd4x.begin(Wire); + + // Stop previous measurement + uint16_t error = scd4x.stopPeriodicMeasurement(); + if (error) { + SKETCH_DEBUG_PRINTLN("Cannot connect to SCD4X"); + return -1; + } + + // Start new measurement + error = scd4x.startPeriodicMeasurement(); + if (error) { + SKETCH_DEBUG_PRINTLN("Cannot start measurement for SCD4X"); + return -1; + } + SCD4XConnected = 1; + + return 0; +} +int SCD4XGetMeasure(float &temperature, float &humidity, uint16_t &co2) +{ + // Read Measurement + bool isDataReady = false; + + uint16_t error = scd4x.getDataReadyFlag(isDataReady); + if (error) { + SKETCH_DEBUG_PRINTLN("Error trying to execute getDataReadyFlag() for SCD4X "); + return -1; + } + if (!isDataReady) { + return -1; + } + error = scd4x.readMeasurement(co2, temperature, humidity); + if (error || co2 == 0) { + char errorMsg[256]; + SKETCH_DEBUG_PRINT("Error with reading measurement. Error : "); + errorToString(error, errorMsg, sizeof(errorMsg)); + SKETCH_DEBUG_PRINTLN(" Co2: %d", co2); + + return -1; + } + return 0; +} +bool SCD4XIsConnected() +{ + return SCD4XConnected != 0; +} +#endif