0% found this document useful (0 votes)
19 views2 pages

Codigo

The document describes an Arduino project that monitors air quality with an MQ-6 gas sensor and DHT11 temperature and humidity sensor. It connects to WiFi and sends data and alerts to Telegram via an ESP8266. When gas is detected, it lights an LED and sends a message to Telegram. Users can also request temperature and humidity readings via commands.

Uploaded by

dolaure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Codigo

The document describes an Arduino project that monitors air quality with an MQ-6 gas sensor and DHT11 temperature and humidity sensor. It connects to WiFi and sends data and alerts to Telegram via an ESP8266. When gas is detected, it lights an LED and sends a message to Telegram. Users can also request temperature and humidity readings via commands.

Uploaded by

dolaure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <DHT.

h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include<ArduinoJson.h>
#define CHAT_ID "5964421534"

#define mq6 5
#define led 23

#define ssid "Galaxy A043386"


#define password "niwi5470"

const String botToken = "6744064329:AAEwsYbjH2gyiubhoCCa10fpMrY1snVSPSM";


const unsigned long botMTBS = 1000;
unsigned long botLastScan;

const long interval = 10000;


unsigned long previousMillis = 0;
float Temp;
float Hum;

DHT dht(15, DHT11);


WiFiClientSecure secured_client;
UniversalTelegramBot bot(botToken, secured_client);

void setup() {
Serial.begin(115200);
dht.begin();
delay(3000);

pinMode(mq6, INPUT);
pinMode(led, OUTPUT);

Serial.print("Conectar a la red WiFi");


Serial.println(ssid);
WiFi.begin(ssid, password);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);

while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi conectado. Direccion IP: ");
Serial.println(WiFi.localIP());
}

void loop(){
if(millis() - botLastScan > botMTBS){
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while(numNewMessages){
Serial.println("Respuesta obtenida!");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
botLastScan = millis();
}
// Si se detecta gas, el valor será LOW (0); de lo contrario, será HIGH (1)
if (digitalRead(mq6) == HIGH) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial.println("¡Se ha detectado gas!");
bot.sendMessage(CHAT_ID, "Se ha detectado gas", "");
digitalWrite(led, HIGH);
}
}
else {
Serial.println("No se detecta gas");
digitalWrite(led, LOW);
}
}

void handleNewMessages(int numNewMessages){


Serial.print("handleNewMessages ");
Serial.println(numNewMessages);

for(int i = 0; i < numNewMessages; i++){


String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;

String from_name = bot.messages[i].from_name;


if(from_name == ""){
from_name = "Guest";
}
TempAndHum();
if (text == "/Temp"){
bot.sendMessage(chat_id, String(Temp), "");
}
if (text == "/Hum"){
bot.sendMessage(chat_id, String(Hum), "");
}
}
}

void TempAndHum(){
Temp = dht.readTemperature();
Hum = dht.readHumidity();
Serial.print("Temperatura: "); Serial.print(Temp); Serial.print(" °c");
Serial.print("Humedad: "); Serial.print(Hum); Serial.print(" %");
Serial.println("---");
delay(1000);
}

You might also like