Last active
June 30, 2025 16:18
-
-
Save hackolite/1a0acf02340d91e6b71b9c0d1679879c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Adafruit_BME680.h> | |
#include <Adafruit_SGP30.h> | |
#include <SPI.h> | |
#include <SD.h> | |
const int chipSelect = 10; | |
const int uvPin = A2; | |
const float coeffUV = 5.0 / 1023.0; | |
const unsigned long interval = 15000; | |
unsigned long previousMillis = 0; | |
Adafruit_BME680 bme; | |
Adafruit_SGP30 sgp; | |
void setup() { | |
Serial.begin(9600); | |
delay(2000); | |
if (!bme.begin(0x76)) { | |
Serial.println(F("Erreur : BME680 non détecté !")); | |
while (1); | |
} | |
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); | |
delay(200); | |
if (!sgp.begin()) { | |
Serial.println(F("Erreur : SGP30 non détecté !")); | |
while (1); | |
} | |
if (!SD.begin(chipSelect)) { | |
Serial.println(F("Erreur : carte SD non détectée !")); | |
while (1); | |
} | |
File dataFile = SD.open("donnees.txt", FILE_WRITE); | |
if (dataFile && dataFile.size() == 0) { | |
dataFile.println(F("temps,temperature,pression,humidite,UV,eCO2")); | |
dataFile.close(); | |
} | |
Serial.println(F("Initialisation OK.")); | |
} | |
void loop() { | |
if (millis() - previousMillis < interval) return; | |
previousMillis = millis(); | |
int uv = analogRead(uvPin); | |
float uvVolt = uv * coeffUV; | |
if (!bme.performReading()) { | |
Serial.println(F("Erreur lecture BME680")); | |
return; | |
} | |
if (!sgp.IAQmeasure()) { | |
Serial.println(F("Erreur lecture SGP30")); | |
return; | |
} | |
// Impression directe sans String ni buffer char[] | |
File dataFile = SD.open("donnees.txt", FILE_WRITE); | |
if (dataFile) { | |
unsigned long now = millis(); | |
Serial.print(now); Serial.print(','); | |
Serial.print(bme.temperature, 1); Serial.print(','); | |
Serial.print(bme.pressure / 100.0, 1); Serial.print(','); | |
Serial.print(bme.humidity, 1); Serial.print(','); | |
// Serial.print(bme.gas_resistance / 1000.0, 1); Serial.print(','); | |
Serial.print(uv); Serial.print(','); | |
// Serial.print(uvVolt, 2); Serial.print(','); | |
Serial.println(sgp.eCO2); | |
// Serial.println(sgp.TVOC); | |
dataFile.print(now); dataFile.print(','); | |
dataFile.print(bme.temperature, 1); dataFile.print(','); | |
dataFile.print(bme.pressure / 100.0, 1); dataFile.print(','); | |
dataFile.print(bme.humidity, 1); dataFile.print(','); | |
// dataFile.print(bme.gas_resistance / 1000.0, 1); dataFile.print(','); | |
dataFile.print(uv); dataFile.print(','); | |
// dataFile.print(uvVolt, 2); dataFile.print(','); | |
dataFile.print(sgp.eCO2); dataFile.print(','); | |
// dataFile.println(sgp.TVOC); | |
dataFile.close(); | |
} else { | |
Serial.println(F("Erreur écriture fichier SD !")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment