#include <DHT22.h> // Using DHT22-specific library
// Pin definitions
#define DHTPIN 15 // DHT22 data pin
#define GAS_PIN 34 // MQ2 analog pin (ADC1_CH6)
#define SOUND_PIN 35 // Sound sensor analog pin (ADC1_CH7)
// Initialize DHT22 sensor
DHT22 dht22(DHTPIN);
// Variables for sensor readings
float temperature = 0.0;
float humidity = 0.0;
int gasValue = 0;
int soundLevel = 0;
float gasVoltage = 0.0;
float gasPPM = 0.0;
// Sound detection variables
int soundThreshold = 2000; // Adjusted threshold for potentiometer
bool coughDetected = false;
void setup() {
Serial.begin(115200);
// Configure ADC pins
analogReadResolution(12); // 12-bit ADC resolution (0-4095)
Serial.println("=================================");
Serial.println(" BreathAI Sensor Monitor");
Serial.println("=================================");
Serial.println("Initializing sensors...");
delay(2000); // Give sensors time to stabilize
Serial.println("Sensors ready!");
Serial.println("Reading format: Temp(°C) | Humidity(%) | Gas(ppm) | Sound Level | Status");
Serial.println("----------------------------------------------------------------------");
}
void loop() {
readDHT22();
readGasSensor();
readSoundSensor();
displayReadings();
checkAlerts();
delay(2000);
}
void readDHT22() {
// Read temperature and humidity from DHT22
temperature = dht22.getTemperature();
humidity = dht22.getHumidity();
// Check if readings are valid using isnan() function
if (isnan(temperature) || isnan(humidity)) {
Serial.println("ERROR: Failed to read from DHT22 sensor!");
temperature = -999;
humidity = -999;
}
}
void readGasSensor() {
gasValue = analogRead(GAS_PIN);
gasVoltage = gasValue * (3.3 / 4095.0);
gasPPM = mapFloat(gasValue, 0, 4095, 0, 1000);
}
void readSoundSensor() {
soundLevel = analogRead(SOUND_PIN);
coughDetected = (soundLevel > soundThreshold);
}
void displayReadings() {
Serial.print("Temp: ");
if (temperature != -999) {
Serial.print(temperature, 1);
Serial.print("°C");
} else {
Serial.print("ERROR");
}
Serial.print(" | Humidity: ");
if (humidity != -999) {
Serial.print(humidity, 1);
Serial.print("%");
} else {
Serial.print("ERROR");
}
Serial.print(" | Gas: ");
Serial.print(gasPPM, 0);
Serial.print(" ppm");
Serial.print(" | Sound: ");
Serial.print(soundLevel);
Serial.print(" | Status: ");
if (coughDetected) {
Serial.print("COUGH DETECTED!");
} else {
Serial.print("Normal");
}
Serial.println();
}
void checkAlerts() {
bool alertTriggered = false;
String alertMessage = "ALERT: ";
if (temperature > 35.0 && temperature != -999) {
alertMessage += "High Temp! ";
alertTriggered = true;
}
if (humidity > 80.0 && humidity != -999) {
alertTriggered = true;
alertMessage += "High Humidity! ";
}
if (gasPPM > 300) {
alertMessage += "High Gas Levels! ";
alertTriggered = true;
}
if (coughDetected) {
alertMessage += "Cough Activity! ";
alertTriggered = true;
}
if (alertTriggered) {
Serial.println("🚨 " + alertMessage);
}
}
float mapFloat(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}