240 lines
7.5 KiB
C++
240 lines
7.5 KiB
C++
/*
|
|
* THIS FILE IS AUTOMATICALLY GENERATED
|
|
*
|
|
* Generator: sensirion-driver-generator 1.1.2
|
|
* Product: scd4x
|
|
* Model-Version: 2.0
|
|
*/
|
|
/*
|
|
* Copyright (c) 2025, Sensirion AG
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
* list of conditions and the following disclaimer.
|
|
*
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
* and/or other materials provided with the distribution.
|
|
*
|
|
* * Neither the name of Sensirion AG nor the names of its
|
|
* contributors may be used to endorse or promote products derived from
|
|
* this software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
#include <Arduino.h>
|
|
#include <SensirionI2cScd4x.h>
|
|
#include <Wire.h>
|
|
|
|
// macro definitions
|
|
// make sure that we use the proper definition of NO_ERROR
|
|
#ifdef NO_ERROR
|
|
#undef NO_ERROR
|
|
#endif
|
|
#define NO_ERROR 0
|
|
|
|
#define SDA_PIN 6
|
|
#define SCL_PIN 20
|
|
|
|
SensirionI2cScd4x sensor;
|
|
|
|
static char errorMessage[64];
|
|
static int16_t error;
|
|
|
|
|
|
|
|
#include <bme68xLibrary.h>
|
|
Bme68x bme;
|
|
|
|
|
|
void PrintUint64(uint64_t& value) {
|
|
Serial.print("0x");
|
|
Serial.print((uint32_t)(value >> 32), HEX);
|
|
Serial.print((uint32_t)(value & 0xFFFFFFFF), HEX);
|
|
}
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(115200);
|
|
while (!Serial) {
|
|
delay(100);
|
|
}
|
|
Wire.begin(SDA_PIN, SCL_PIN);
|
|
sensor.begin(Wire, SCD41_I2C_ADDR_62);
|
|
|
|
uint64_t serialNumber = 0;
|
|
delay(30);
|
|
// Ensure sensor is in clean state
|
|
error = sensor.wakeUp();
|
|
if (error != NO_ERROR) {
|
|
Serial.print("Error trying to execute wakeUp(): ");
|
|
errorToString(error, errorMessage, sizeof errorMessage);
|
|
Serial.println(errorMessage);
|
|
}
|
|
error = sensor.stopPeriodicMeasurement();
|
|
if (error != NO_ERROR) {
|
|
Serial.print("Error trying to execute stopPeriodicMeasurement(): ");
|
|
errorToString(error, errorMessage, sizeof errorMessage);
|
|
Serial.println(errorMessage);
|
|
}
|
|
error = sensor.reinit();
|
|
if (error != NO_ERROR) {
|
|
Serial.print("Error trying to execute reinit(): ");
|
|
errorToString(error, errorMessage, sizeof errorMessage);
|
|
Serial.println(errorMessage);
|
|
}
|
|
// Read out information about the sensor
|
|
error = sensor.getSerialNumber(serialNumber);
|
|
if (error != NO_ERROR) {
|
|
Serial.print("Error trying to execute getSerialNumber(): ");
|
|
errorToString(error, errorMessage, sizeof errorMessage);
|
|
Serial.println(errorMessage);
|
|
return;
|
|
}
|
|
Serial.print("serial number: ");
|
|
PrintUint64(serialNumber);
|
|
Serial.println();
|
|
//
|
|
// If temperature offset and/or sensor altitude compensation
|
|
// is required, you should call the respective functions here.
|
|
// Check out the header file for the function definitions.
|
|
// Start periodic measurements (5sec interval)
|
|
error = sensor.startPeriodicMeasurement();
|
|
if (error != NO_ERROR) {
|
|
Serial.print("Error trying to execute startPeriodicMeasurement(): ");
|
|
errorToString(error, errorMessage, sizeof errorMessage);
|
|
Serial.println(errorMessage);
|
|
return;
|
|
}
|
|
//
|
|
// If low-power mode is required, switch to the low power
|
|
// measurement function instead of the standard measurement
|
|
// function above. Check out the header file for the definition.
|
|
// For SCD41, you can also check out the single shot measurement example.
|
|
//
|
|
|
|
|
|
|
|
|
|
bme.begin(BME68X_I2C_ADDR_HIGH, Wire); // BME68X_I2C_ADDR_HIGH=0x76, BME68X_I2C_ADDR_LOW=0x77
|
|
|
|
if(bme.checkStatus())
|
|
{
|
|
if (bme.checkStatus() == BME68X_ERROR)
|
|
{
|
|
Serial.println("Sensor error:" + bme.statusString());
|
|
return;
|
|
}
|
|
else if (bme.checkStatus() == BME68X_WARNING)
|
|
{
|
|
Serial.println("Sensor Warning:" + bme.statusString());
|
|
}
|
|
}
|
|
/* Set the default configuration for temperature, pressure and humidity */
|
|
bme.setTPH();
|
|
/* Set the heater configuration to 300 deg C for 100ms for Forced mode */
|
|
bme.setHeaterProf(300, 100);
|
|
|
|
bme68xData data;
|
|
bme.setOpMode(BME68X_FORCED_MODE);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
void loop() {
|
|
|
|
bool dataReady = false;
|
|
uint16_t co2Concentration = 0;
|
|
float temperature = 0.0;
|
|
float relativeHumidity = 0.0;
|
|
//
|
|
// Slow down the sampling to 0.2Hz.
|
|
//
|
|
delay(5000);
|
|
error = sensor.getDataReadyStatus(dataReady);
|
|
if (error != NO_ERROR) {
|
|
Serial.print("Error trying to execute getDataReadyStatus(): ");
|
|
errorToString(error, errorMessage, sizeof errorMessage);
|
|
Serial.println(errorMessage);
|
|
return;
|
|
}
|
|
while (!dataReady) {
|
|
delay(100);
|
|
error = sensor.getDataReadyStatus(dataReady);
|
|
if (error != NO_ERROR) {
|
|
Serial.print("Error trying to execute getDataReadyStatus(): ");
|
|
errorToString(error, errorMessage, sizeof errorMessage);
|
|
Serial.println(errorMessage);
|
|
return;
|
|
}
|
|
}
|
|
//
|
|
// If ambient pressure compenstation during measurement
|
|
// is required, you should call the respective functions here.
|
|
// Check out the header file for the function definition.
|
|
error =
|
|
sensor.readMeasurement(co2Concentration, temperature, relativeHumidity);
|
|
if (error != NO_ERROR) {
|
|
Serial.print("Error trying to execute readMeasurement(): ");
|
|
errorToString(error, errorMessage, sizeof errorMessage);
|
|
Serial.println(errorMessage);
|
|
return;
|
|
}
|
|
//
|
|
// Print results in physical units.
|
|
Serial.print("CO2 concentration [ppm]: ");
|
|
Serial.print(co2Concentration);
|
|
Serial.println();
|
|
Serial.print("Temperature [°C]: ");
|
|
Serial.print(temperature);
|
|
Serial.println();
|
|
Serial.print("Relative Humidity [RH]: ");
|
|
Serial.print(relativeHumidity);
|
|
Serial.println();
|
|
|
|
Serial.println("---------------------------");
|
|
|
|
float bme_temperature(NAN), bme_humidity(NAN), bme_pressure(NAN), bme_gasResistance(NAN);
|
|
bme68xData data;
|
|
bme.setOpMode(BME68X_FORCED_MODE);
|
|
delayMicroseconds(bme.getMeasDur());
|
|
if (bme.fetchData()) {
|
|
bme.getData(data);
|
|
bme_temperature = data.temperature; // Temperature in °C
|
|
bme_humidity = data.humidity; // Humidity in %
|
|
bme_pressure = data.pressure; // Pressure in hPa
|
|
bme_gasResistance = data.gas_resistance; // Gas resistance in ohms
|
|
// errorCount = 0; // Reset error count on successful read
|
|
Serial.printf("Reported temperature: %.2f°C, Humidity: %.2f%%, Gas Resistance: %.2f ohms\r\n", bme_temperature, bme_humidity, bme_gasResistance);
|
|
|
|
} else {
|
|
Serial.println("BME680 -> Waiting...");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |