96 lines
2.7 KiB
C++
96 lines
2.7 KiB
C++
#include <Wire.h>
|
|
|
|
// Default I2C Address for T6713 is 0x15 (decimal 21)
|
|
#define T6713_ADDR 0x15
|
|
#define I2C_SCL_PIN 20 // Change to your SCL pin
|
|
#define I2C_SDA_PIN 6 // Change to your SDA pin
|
|
|
|
#include <bme68xLibrary.h>
|
|
#ifndef BME68X_I2C_ADDR
|
|
#define BME68X_I2C_ADDR 0x76 // Change to 0x77 if sensor not found
|
|
#endif
|
|
Bme68x bme;
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN); // Join I2C bus
|
|
|
|
Serial.println("Initializing Sensors...");
|
|
|
|
// 1. Initialize BME680
|
|
bme.begin(BME68X_I2C_ADDR, Wire);
|
|
if(bme.checkStatus()) {
|
|
Serial.println("BME680 Error: " + bme.statusString());
|
|
if (bme.checkStatus() == BME68X_ERROR) {
|
|
Serial.println("Check wiring or I2C address (0x76 vs 0x77)");
|
|
while(1); // Halt if BME fails
|
|
}
|
|
}
|
|
Serial.println("BME680 Found!");
|
|
|
|
// Configure BME680 for "Forced Mode" (On-Demand)
|
|
bme.setTPH(); // Set default oversampling for Temp, Pres, Hum
|
|
|
|
// Configure Heater for Gas Measurement (300°C for 100ms)
|
|
bme.setHeaterProf(300, 100);
|
|
|
|
Serial.println("Sensors Ready. Starting Loop...");
|
|
delay(2000); // Let sensor warm up
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop() {
|
|
Serial.println("-----------------------------");
|
|
|
|
// --- Read T6713 (CO2) ---
|
|
int co2 = readT6713();
|
|
if (co2 >= 0) {
|
|
Serial.print("T6713 | CO2: "); Serial.print(co2); Serial.println(" ppm");
|
|
} else {
|
|
Serial.println("T6713 | Read Error");
|
|
}
|
|
|
|
// --- Read BME680 (Temp, Hum, Pres, Gas) ---
|
|
bme68xData data;
|
|
bme.setOpMode(BME68X_FORCED_MODE); // Trigger measurement
|
|
|
|
// Wait for measurement to complete (depends on heater duration)
|
|
delay(bme.getMeasDur(BME68X_FORCED_MODE) / 1000 + 50);
|
|
|
|
if (bme.fetchData()) {
|
|
bme.getData(data);
|
|
Serial.print("BME680 | Temp: "); Serial.print(data.temperature); Serial.println(" C");
|
|
Serial.print("BME680 | Hum: "); Serial.print(data.humidity); Serial.println(" %");
|
|
Serial.print("BME680 | Pres: "); Serial.print(data.pressure / 100.0); Serial.println(" hPa");
|
|
Serial.print("BME680 | Gas: "); Serial.print(data.gas_resistance / 1000.0); Serial.println(" kOhm");
|
|
} else {
|
|
Serial.println("BME680 | Failed to fetch data");
|
|
}
|
|
|
|
// Wait 5 seconds before next cycle
|
|
delay(5000);
|
|
}
|
|
|
|
// Helper function for T6713
|
|
int readT6713() {
|
|
Wire.beginTransmission(T6713_ADDR);
|
|
Wire.write(0x04); // Function: Read Input Registers
|
|
Wire.write(0x13); // Address High
|
|
Wire.write(0x8B); // Address Low
|
|
Wire.write(0x00);
|
|
Wire.write(0x01); // Length (1 register)
|
|
if (Wire.endTransmission() != 0) return -1;
|
|
|
|
delay(10); // Processing delay
|
|
|
|
Wire.requestFrom(T6713_ADDR, 4);
|
|
if (Wire.available() == 4) {
|
|
Wire.read(); Wire.read(); // Skip Func & Len
|
|
byte high = Wire.read();
|
|
byte low = Wire.read();
|
|
return (high << 8) | low;
|
|
}
|
|
return -1;
|
|
} |