#include <MQUnifiedsensor.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
// Configuración de pines
const int gasSensorPin = 34;
const int resetButtonPin = 32;
const int buzzerPin = 26;
const int greenLedPin = 27;
const int yellowLedPin = 14;
const int redLedPin = 12;
const int tempSensorPin = 33;
const int smokeSensorPin = 35;
const float gasThreshold = 200.0;
const float tempThreshold = 40.0;
const float smokeThreshold = 300.0;
// Configuración de la pantalla LCD 20x4
LiquidCrystal_I2C lcd(0x27, 22, 21); // Dirección I2C 0x27
// Configuración del sensor de temperatura DHT
#define DHTTYPE DHT22
DHT dht(tempSensorPin, DHTTYPE);
// Configuración de WiFi
const char* ssid = "Tenda_5181E0_5G";
const char* password = "zxcv1234";
const char* iftttServer = "maker.ifttt.com";
String eventName = "gas_alert";
String iftttKey = "TU_IFTTT_KEY";
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("WiFi connected");
// Inicializar la pantalla LCD
lcd.begin(20, 4);
lcd.backlight();
pinMode(gasSensorPin, INPUT);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(smokeSensorPin, INPUT);
dht.begin();
attachInterrupt(digitalPinToInterrupt(resetButtonPin), resetSystem, FALLING);
}
void loop() {
int gasValue = analogRead(gasSensorPin);
float tempValue = dht.readTemperature();
int smokeValue = analogRead(smokeSensorPin);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gas: ");
lcd.print(gasValue);
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(tempValue);
lcd.setCursor(0, 2);
lcd.print("Smoke: ");
lcd.print(smokeValue);
if (gasValue > gasThreshold || tempValue > tempThreshold || smokeValue > smokeThreshold) {
if (gasValue > gasThreshold) {
Serial.println("Gas detected!");
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
}
if (tempValue > tempThreshold) {
Serial.println("High temperature detected!");
digitalWrite(yellowLedPin, HIGH);
digitalWrite(redLedPin, LOW);
digitalWrite(greenLedPin, LOW);
}
if (smokeValue > smokeThreshold) {
Serial.println("Smoke detected!");
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
}
tone(buzzerPin, 1000); // tono de 1kHz
sendNotification(gasValue, tempValue, smokeValue);
} else {
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
noTone(buzzerPin);
Serial.println("Safe levels.");
}
delay(1000); // Leer sensores cada segundo
}
void resetSystem() {
noTone(buzzerPin);
digitalWrite(greenLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(redLedPin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System Reset");
delay(1000);
}
void sendNotification(int gasValue, float tempValue, int smokeValue) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "http://" + String(iftttServer) + "/trigger/" + eventName + "/with/key/" + iftttKey + "?value1=" + String(gasValue) + "&value2=" + String(tempValue) + "&value3=" + String(smokeValue);
http.begin(url);
int httpCode = http.GET();
http.end();
}
}