diff options
author | dakkar <dakkar@thenautilus.net> | 2022-01-28 15:16:38 +0000 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2022-01-28 15:16:38 +0000 |
commit | cdd4f766759f8a8e1ea36c6524895714099d2f38 (patch) | |
tree | a6a2345093e2b7df7dc1b4f49cfb3357133e96c1 /main.ino | |
parent | start (diff) | |
download | env-sensor-cdd4f766759f8a8e1ea36c6524895714099d2f38.tar.gz env-sensor-cdd4f766759f8a8e1ea36c6524895714099d2f38.tar.bz2 env-sensor-cdd4f766759f8a8e1ea36c6524895714099d2f38.zip |
sensirion example, works
Diffstat (limited to 'main.ino')
-rw-r--r-- | main.ino | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/main.ino b/main.ino new file mode 100644 index 0000000..bc27deb --- /dev/null +++ b/main.ino @@ -0,0 +1,94 @@ +#include <Arduino.h> +#include <SensirionI2CScd4x.h> +#include <Wire.h> + +SensirionI2CScd4x scd4x; + +void printUint16Hex(uint16_t value) { + Serial.print(value < 4096 ? "0" : ""); + Serial.print(value < 256 ? "0" : ""); + Serial.print(value < 16 ? "0" : ""); + Serial.print(value, HEX); +} + +void printSerialNumber(uint16_t serial0, uint16_t serial1, uint16_t serial2) { + Serial.print("Serial: 0x"); + printUint16Hex(serial0); + printUint16Hex(serial1); + printUint16Hex(serial2); + Serial.println(); +} + +void setup() { + + Serial.begin(115200); + while (!Serial) { + delay(100); + } + + Wire.begin(21,22); + + uint16_t error; + char errorMessage[256]; + + scd4x.begin(Wire); + + // stop potentially previously started measurement + error = scd4x.stopPeriodicMeasurement(); + if (error) { + Serial.print("Error trying to execute stopPeriodicMeasurement(): "); + errorToString(error, errorMessage, 256); + Serial.println(errorMessage); + } + + uint16_t serial0; + uint16_t serial1; + uint16_t serial2; + error = scd4x.getSerialNumber(serial0, serial1, serial2); + if (error) { + Serial.print("Error trying to execute getSerialNumber(): "); + errorToString(error, errorMessage, 256); + Serial.println(errorMessage); + } else { + printSerialNumber(serial0, serial1, serial2); + } + + // Start Measurement + error = scd4x.startPeriodicMeasurement(); + if (error) { + Serial.print("Error trying to execute startPeriodicMeasurement(): "); + errorToString(error, errorMessage, 256); + Serial.println(errorMessage); + } + + Serial.println("Waiting for first measurement... (5 sec)"); +} + +void loop() { + uint16_t error; + char errorMessage[256]; + + delay(5000); + + // Read Measurement + uint16_t co2 = 0; + float temperature = 0.0f; + float humidity = 0.0f; + error = scd4x.readMeasurement(co2, temperature, humidity); + if (error) { + Serial.print("Error trying to execute readMeasurement(): "); + errorToString(error, errorMessage, 256); + Serial.println(errorMessage); + } else if (co2 == 0) { + Serial.println("Invalid sample detected, skipping."); + } else { + Serial.print("Co2:"); + Serial.print(co2); + Serial.print("\t"); + Serial.print("Temperature:"); + Serial.print(temperature); + Serial.print("\t"); + Serial.print("Humidity:"); + Serial.println(humidity); + } +} |