#include <WiFi.h>
#include <HTTPClient.h>
// ==================== WiFi ====================
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ==================== PINOS ====================
#define PIN_GAS 34 // AOUT do sensor MQ
#define LED_VERDE 0
#define LED_VERM 2
#define BUZZER 15
// ==================== LIMITES ====================
#define GAS_ON 750 // entra em alarme
#define GAS_OFF 650 // sai do alarme
// ==================== VARIÁVEIS ====================
bool gasAlarm = false;
bool messageSent = false;
// ==================== PROTÓTIPOS ====================
void sendWhatsAppAlert();
int readGasFiltered();
// ==================== SETUP ====================
void setup() {
Serial.begin(115200);
// WiFi
WiFi.begin(ssid, password);
Serial.print("Conectando ao WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi conectado!");
// ADC ESP32
analogSetAttenuation(ADC_11db);
// GPIOs
pinMode(LED_VERDE, OUTPUT);
pinMode(LED_VERM, OUTPUT);
pinMode(BUZZER, OUTPUT);
// Estado inicial
digitalWrite(LED_VERDE, HIGH);
digitalWrite(LED_VERM, LOW);
digitalWrite(BUZZER, LOW);
}
// ==================== LOOP ====================
void loop() {
int gasValue = readGasFiltered();
Serial.print("Gás (PPM): ");
Serial.println(gasValue);
// ===== ENTRA EM ALARME =====
if (!gasAlarm && gasValue >= GAS_ON) {
gasAlarm = true;
Serial.println("⚠️ ALERTA: Vazamento de Gás");
digitalWrite(LED_VERDE, LOW);
digitalWrite(LED_VERM, HIGH);
digitalWrite(BUZZER, HIGH);
if (!messageSent) {
sendWhatsAppAlert();
messageSent = true;
}
}
// ===== SAI DO ALARME =====
else if (gasAlarm && gasValue <= GAS_OFF) {
gasAlarm = false;
messageSent = false;
Serial.println("✅ Ambiente Seguro");
digitalWrite(LED_VERDE, HIGH);
digitalWrite(LED_VERM, LOW);
digitalWrite(BUZZER, LOW);
}
delay(2000);
}
// ==================== FILTRO ADC ====================
int readGasFiltered() {
const int samples = 20;
long sum = 0;
for (int i = 0; i < samples; i++) {
sum += analogRead(PIN_GAS);
delay(5);
}
return sum / samples;
}
// ==================== WHATSAPP ====================
void sendWhatsAppAlert() {
if (WiFi.status() != WL_CONNECTED) return;
HTTPClient http;
String url = "https://api.callmebot.com/whatsapp.php?";
url += "phone=554896956965";
url += "&text=ALERTA:+Vazamento+de+Gás!";
url += "&apikey=6349684";
http.begin(url);
int httpCode = http.GET();
if (httpCode == 200) {
Serial.println("📨 Mensagem enviada com sucesso!");
} else {
Serial.print("❌ Erro HTTP: ");
Serial.println(httpCode);
}
http.end();
}