Arduino_Sensor_Nodes/sketch_nov30a/sketch_nov30a.ino

106 lines
3.0 KiB
C++

#include <Wire.h>
#include <SensirionI2cScd4x.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
// --- Configuration ---
#define SDA_PIN 6
#define SCL_PIN 20
// 1. Define the TwoWire instance we want to use
// On ESP32, 'Wire' is the default TwoWire instance (I2C bus 0).
// We use a reference here to show how you would treat it as a generic TwoWire object.
TwoWire myI2C = TwoWire(0);
// Create sensor instances
// SensirionI2cScd4x scd4x;
Adafruit_BME680 bme;
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("Initializing Custom TwoWire I2C...");
// 2. Initialize the TwoWire instance with specific pins
// calling myI2C->begin() is the same as calling Wire.begin()
// myI2C->begin(SDA_PIN, SCL_PIN);
myI2C.begin(SDA_PIN, SCL_PIN, 100000);
// // --- Initialize SCD41 ---
// // Sensirion library accepts the TwoWire object by REFERENCE (no & symbol)
// scd4x.begin(*myI2C, 0x62);
// // Check SCD41
// uint16_t error;
// char errorMessage[256];
// scd4x.stopPeriodicMeasurement();
// error = scd4x.startPeriodicMeasurement();
// if (error) {
// Serial.print("SCD41 Error: ");
// errorToString(error, errorMessage, 256);
// Serial.println(errorMessage);
// } else {
// Serial.println("SCD41 initialized via TwoWire.");
// }
// --- Initialize BME680 ---
// Adafruit library accepts the TwoWire object by POINTER (needs & symbol if not already a pointer)
// We try address 0x77 first (common for generic modules), then 0x76 (Adafruit default)
Serial.println("Connecting to BME680...");
// Note the syntax: .begin(Address, TwoWirePointer)
if (!bme.begin(0x77, &myI2C)) {
Serial.println("BME680 not found at 0x77, trying 0x76...");
if (!bme.begin(0x76, &myI2C)) {
Serial.println("BME680 Init Failed! Check wiring.");
while (1);
}
}
Serial.println("BME680 initialized via TwoWire.");
// BME680 Settings
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150);
}
void loop() {
// Read SCD41
uint16_t co2 = 0;
float temp_scd = 0, hum_scd = 0;
bool scdReady = false;
// if (scd4x.getDataReadyStatus()) {
// uint16_t error = scd4x.readMeasurement(co2, temp_scd, hum_scd);
// if (!error && co2 != 0) scdReady = true;
// }
// Read BME680
bool bmeReady = bme.performReading();
// Output
Serial.println("\n--- Readings ---");
// if (scdReady) {
// Serial.print("SCD41 -> CO2: "); Serial.print(co2);
// Serial.print(" ppm | Temp: "); Serial.print(temp_scd); Serial.println(" C");
// } else {
// Serial.println("SCD41 -> Waiting...");
// }
if (bmeReady) {
Serial.print("BME680 -> Gas: "); Serial.print(bme.gas_resistance / 1000.0);
Serial.print(" KOhms | Temp: "); Serial.print(bme.temperature); Serial.println(" C");
} else {
Serial.println("BME680 -> Waiting...");
}
Serial.println("Loop");
delay(1000);
}