#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
// Configurações do LCD
#define COLUNAS 16
#define LINHAS 2
#define ENDERECO_LCD 0x27
LiquidCrystal_I2C lcd(ENDERECO_LCD, COLUNAS, LINHAS);
// Pinos dos sensores
#define PINO_MQ2 36 // Sensor de gás (pino analógico) pino VP
#define PINO_TMP36 39 //pino VN
#define PINO_BUZZER 25 // Pino do buzzer // Sensor de temperatura TMP36 (pino analógico)
// Constantes de conversão
#define VREF 3.9
#define ADC_RESOLUCAO 4095.0
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* serverName = "http://api.thingspeak.com/update";
String apiKey = "SX6A329RAW2PEB3L";
void setup() {
// Inicialização Serial
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Iniciando sensores...");
if (WiFi.status() == WL_CONNECTED){
delay(500);
Serial.print("Conectado!");
}
Serial.println("");
Serial.print("Conectado a rede: ");
Serial.println(WiFi.localIP());
// Inicialização do LCD
Wire.begin(21, 22); // SDA = 21, SCL = 22
lcd.init();
lcd.clear();
lcd.backlight();
pinMode(PINO_BUZZER, OUTPUT);
digitalWrite(PINO_BUZZER, LOW);
// Ajuste da atenuação para ler até 3.3V
analogSetAttenuation(ADC_11db);
// Aguarda o sensor MQ-2 esquentar
lcd.setCursor(0, 0);
lcd.print("Aquecendo sensor");
delay(20000);
lcd.clear();
}
void loop() {
// Leitura do sensor de gás (valor analógico)
int valorGas = analogRead(PINO_MQ2);
// Aciona buzzer se valor do gás for alto
if (valorGas > 1200) {
// Bips rápidos enquanto valorGas > 1200
for (int i = 0; i < 5; i++) {
digitalWrite(PINO_BUZZER, HIGH);
delay(50);
digitalWrite(PINO_BUZZER, LOW);
delay(50);
}
}
else if (valorGas > 800) {
// Bip único
digitalWrite(PINO_BUZZER, HIGH);
delay(100);
digitalWrite(PINO_BUZZER, LOW);
}
// Leitura do TMP36
int adcTemp = analogRead(PINO_TMP36);
float tensaoTemp = adcTemp * (VREF / ADC_RESOLUCAO);
float tempC = (tensaoTemp - 0.5) * 100.0;
// Monitor Serial
Serial.print("Valor do gás: ");
Serial.print(valorGas);
Serial.print(" | Temperatura: ");
Serial.print(tempC, 2);
Serial.println(" °C");
// LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Gas: ");
lcd.print(valorGas);
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(tempC, 1);
lcd.print((char)223); // Símbolo de grau
lcd.print("C");
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client;
HTTPClient http;
delay(5000); // wait for 10 seconds
// Read temperature as Celsius (the default)
//float t = dht.readTemperature();
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Data to send with HTTP POST
String dados_temp = "api_key=" + apiKey + "&field1=" + String(tempC);
String dados_gas = "api_key=" + apiKey + "&field2=" + String(valorGas);
// Send HTTP POST request
int resposta_temp = http.POST(dados_temp);
int resposta_gas = http.POST(dados_gas);
Serial.print("Resposta servidor: ");
Serial.println("Temperatura:");
Serial.print(resposta_temp);
Serial.print(" | ");
Serial.print("Gás:");
Serial.print(resposta_gas);
http.end();
} else {
Serial.println("WiFi Disconnected");
}
delay(1500);
}