Devices & Components
ESP32
Software & Tools
rogerbit.
Project description
Code
Source code
c
Source code
1#include <WiFi.h> 2#include <WiFiClientSecure.h> 3#include <UniversalTelegramBot.h> 4#include "PinDefinitionsAndMore.h"//Configuración y definición de pines 5#include <IRremote.hpp> 6#include "DHT.h" 7// Reemplazar con los datos de tu red wifi 8#define WIFI_SSID "Tu_red_wifi" 9#define WIFI_PASSWORD "tu_clave_wifi" 10//Token de Telegram BOT se obtenienen desde Botfather en telegram 11#define BOT_TOKEN "tu_token_de_telegram" 12const unsigned long tiempo = 1000; //tiempo medio entre mensajes de escaneo 13WiFiClientSecure secured_client; 14UniversalTelegramBot bot(BOT_TOKEN, secured_client); 15unsigned long tiempoAnterior; //última vez que se realizó el análisis de mensajes 16int inicio = 1; 17String datos; 18String stringT; 19String stringH; 20int aireEstado = 0; 21// Descomenta la linea dependiendo del sensor que vas a usar 22#define DHTTYPE DHT11 // DHT 11 23//#define DHTTYPE DHT21 // DHT 21 24//#define DHTTYPE DHT22 // DHT 22 25#define DHTPin 23 //Pin del sensor DHT; 26DHT dht(DHTPin, DHTTYPE); 27float t; 28float h; 29String chat_id; 30#define ID_Chat "id_chat_telegram"//ID_Chat se obtiene de telegram 31void mensajesNuevos(int numerosMensajes) 32{ 33 for (int i = 0; i < numerosMensajes; i++) 34 { 35 String chat_id = bot.messages[i].chat_id; 36 String text = bot.messages[i].text; 37 if (text == "/TyH") 38 { 39h = dht.readHumidity();//Lectura de la humedad 40t = dht.readTemperature();//Lectura de la temperatura 41stringT = String(t); 42stringH = String(h); 43Serial.print("T:" ); 44Serial.println(t); 45Serial.print("H:" ); 46Serial.println(h); 47datos = "Temperatura: "+ stringT + " Humedad: " +stringH; 48bot.sendMessage(chat_id, datos, "");//Enviamos la temperatura y humedad 49 } 50//////////Comando para encender el aire acondicionado////// 51 if (text == "/Aireon") 52 { 53const uint16_t irSignal[227] = { 54//Código para encendido de aire acondicionado marca Electra 55 3100, 1600 , 500, 1100 , 450, 1100 , 450, 300 56 , 500, 350 , 450, 350 , 450, 1100 , 450, 350 57 , 450, 350 , 450, 1150 , 400, 1150 , 400, 400 58 , 400, 1150 , 450, 350 , 450, 350 , 450, 1100 59 , 450, 1150 , 400, 400 , 400, 1150 , 400, 1150 60 , 400, 400 , 400, 400 , 400, 1150 , 450, 350 61 , 450, 350 , 450, 1100 , 450, 350 , 450, 350 62 , 450, 350 , 450, 350 , 450, 350 , 450, 350 63 , 450, 350 , 400, 400 , 400, 400 , 400, 400 64 , 400, 400 , 400, 400 , 400, 400 , 400, 400 65 , 400, 400 , 400, 400 , 400, 400 , 400, 1150 66 , 400, 400 , 400, 400 , 400, 1150 , 450, 350 67 , 450, 350 , 400, 400 , 400, 1150 , 450, 350 68 , 450, 350 , 450, 350 , 450, 350 , 450, 350 69 , 450, 350 , 450, 350 , 400, 400 , 400, 400 70 , 400, 1150 , 450, 350 , 450, 350 , 450, 350 71 , 450, 350 , 450, 350 , 450, 350 , 450, 350 72 , 400, 400 , 400, 400 , 400, 400 , 400, 1150 73 , 400, 400 , 450, 350 , 450, 350 , 450, 350 74 , 400, 400 , 400, 400 , 400, 400 , 400, 400 75 , 400, 400 , 400, 400 , 400, 400 , 400, 400 76 , 400, 400 , 400, 400 , 400, 400 , 400, 400 77 , 400, 400 , 400, 400 , 400, 400 , 400, 400 78 , 400, 400 , 400, 400 , 350, 450 , 400, 400 79 , 350, 450 , 400, 400 , 400, 400 , 400, 400 80 , 350, 450 , 350, 450 , 350, 450 , 350, 450 81 , 350, 1200 , 400, 1150 , 400, 1150 , 400, 400 82 , 400, 400 , 400, 400 , 400, 400 , 400, 400 83 , 400, 400 , 400 84 85}; 86IrSender.sendRaw(irSignal, 227, 38); 87bot.sendMessage(chat_id, "Comando encendido ejecutado", ""); 88aireEstado = 1; 89 } 90//////////Comando para apagar el aire acondicionado//////// 91 if (text == "/Aireoff") 92 { 93const uint16_t irSignal2[227] = { 94//Código para apagado de aire acondicionado marca Electra 953100, 1600 , 450, 1100 , 500, 1100 , 450, 350 96 , 450, 350 , 450, 350 , 450, 1100 , 450, 400 97 , 400, 350 , 450, 1150 , 400, 1150 , 400, 400 98 , 400, 1150 , 400, 400 , 400, 400 , 400, 1150 99 , 400, 1150 , 400, 400 , 450, 1150 , 400, 1150 100 , 400, 400 , 400, 400 , 400, 1150 , 400, 400 101 , 400, 400 , 400, 1150 , 400, 400 , 400, 400 102 , 400, 400 , 400, 400 , 400, 400 , 400, 400 103 , 400, 400 , 400, 400 , 400, 400 , 400, 400 104 , 400, 400 , 400, 400 , 400, 400 , 400, 400 105 , 400, 400 , 400, 400 , 400, 400 , 400, 400 106 , 400, 400 , 400, 350 , 400, 1200 , 400, 400 107 , 400, 400 , 400, 1150 , 400, 1150 , 400, 400 108 , 400, 400 , 400, 400 , 400, 400 , 400, 400 109 , 400, 400 , 400, 400 , 400, 400 , 400, 400 110 , 400, 1150 , 400, 450 , 350, 400 , 400, 400 111 , 400, 400 , 400, 400 , 400, 400 , 400, 400 112 , 400, 400 , 400, 400 , 400, 400 , 400, 1150 113 , 400, 400 , 400, 400 , 400, 400 , 400, 400 114 , 400, 400 , 400, 400 , 400, 400 , 400, 400 115 , 400, 400 , 350, 450 , 350, 450 , 350, 450 116 , 350, 450 , 350, 450 , 350, 450 , 350, 450 117 , 350, 450 , 350, 450 , 350, 450 , 350, 450 118 , 350, 450 , 350, 450 , 350, 450 , 350, 400 119 , 400, 400 , 400, 400 , 400, 400 , 400, 400 120 , 400, 400 , 400, 400 , 400, 400 , 400, 400 121 , 400, 1150 , 400, 400 , 400, 400 , 400, 400 122 , 400, 400 , 400, 400 , 400, 400 , 350, 500 123 , 300, 450 , 400 124 125}; 126IrSender.sendRaw(irSignal2, 227, 38); 127bot.sendMessage(chat_id, "Comando apagado ejecutado", ""); 128aireEstado = 2; 129 } 130////////Último comando enviado /////// 131 132 if (text == "/Estado") 133 { 134 ////Ultimo comando enviado//// 135 if (aireEstado == 0) 136 { 137 bot.sendMessage(chat_id, "No definido", ""); 138 } 139 if (aireEstado == 1) 140 { 141 bot.sendMessage(chat_id, "Último comando: Encendido", ""); 142 } 143 if (aireEstado == 2) 144 { 145 bot.sendMessage(chat_id, "Último comando: Apagado", ""); 146 } 147 } 148 149 if (text == "/Ayuda") 150 { 151 String ayuda = "Bienvenido al sistema de control luces con Esp32, " ".\n"; 152 ayuda += "Estas son tus opciones.\n\n"; 153 ayuda += "/Aireon: para encender el aire \n"; 154 ayuda += "/Aireoff: para apagar el aire \n"; 155 ayuda += "/TyH : Muestra la Temperatura y Humedad \n"; 156 ayuda += "/Ayuda: Imprime este menú \n"; 157 ayuda += "Recuerda el sistema distingue entre mayuculas y minusculas \n"; 158 bot.sendMessage(chat_id, ayuda, ""); 159 } 160 161 } 162} 163 164void setup() 165{ 166 Serial.begin(115200); 167 dht.begin();//Inicializar el sensor DHT 168 // Intenta conectarse a la red wifi 169 Serial.print("Conectando a la red "); 170 Serial.print(WIFI_SSID); 171 WiFi.begin(WIFI_SSID, WIFI_PASSWORD); 172 secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); //Agregar certificado raíz para api.telegram.org 173 while (WiFi.status() != WL_CONNECTED) 174 { 175 Serial.print("."); 176 delay(500); 177 } 178 Serial.print("\nConectado a la red wifi. Dirección IP: "); 179 Serial.println(WiFi.localIP()); 180 if(inicio == 1){ 181 Serial.println("Sistema preparado"); 182 bot.sendMessage(ID_Chat, "Sistema preparado!!!, escribe /Ayuda para ver las opciones", "");//Enviamos un mensaje a telegram para informar que el sistema está listo 183 inicio = 0; 184 } 185} 186 187void loop() 188{ 189 //Verifica si hay datos nuevos en telegram cada 1 segundo 190 if (millis() - tiempoAnterior > tiempo) 191 { 192 int numerosMensajes = bot.getUpdates(bot.last_message_received + 1); 193 194 while (numerosMensajes) 195 { 196 Serial.println("Comando recibido"); 197 mensajesNuevos(numerosMensajes); 198 numerosMensajes = bot.getUpdates(bot.last_message_received + 1); 199 } 200 201 tiempoAnterior = millis(); 202 } 203 204}
Comments
Only logged in users can leave comments