Hackster will be offline on Monday, September 30 from 8pm to 10pm PDT to perform some scheduled maintenance.
carlosvolt
Published

Air conditioning on/off control via telegram and esp32

In this tutorial we will see how to control an air conditioner, with an esp32 and the telegram application, from anywhere in the world,

IntermediateProtip116
Air conditioning on/off control via telegram and esp32

Things used in this project

Hardware components

ESP32
Espressif ESP32
×1

Story

Read more

Code

Source code

C/C++
Source code
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include "PinDefinitionsAndMore.h"//Configuración y definición de pines
#include <IRremote.hpp>
#include "DHT.h"
// Reemplazar con los datos de tu red wifi
#define WIFI_SSID "Tu_red_wifi"
#define WIFI_PASSWORD "tu_clave_wifi"
//Token de Telegram BOT se obtenienen desde Botfather en telegram
#define BOT_TOKEN "tu_token_de_telegram"
const unsigned long tiempo = 1000; //tiempo medio entre mensajes de escaneo
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long tiempoAnterior; //última vez que se realizó el análisis de mensajes
int inicio = 1;
String datos;
String stringT;
String stringH;
int aireEstado = 0;
// Descomenta la linea dependiendo del sensor que vas a usar
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 
//#define DHTTYPE DHT22 // DHT 22
#define DHTPin 23 //Pin del sensor DHT;
DHT dht(DHTPin, DHTTYPE); 
float t;
float h;
String chat_id;
#define ID_Chat "id_chat_telegram"//ID_Chat se obtiene de telegram
void mensajesNuevos(int numerosMensajes)
{
  for (int i = 0; i < numerosMensajes; i++)
  {
    String chat_id = bot.messages[i].chat_id;
    String text = bot.messages[i].text;
    if (text == "/TyH")
    {
h = dht.readHumidity();//Lectura de la humedad
t = dht.readTemperature();//Lectura de la temperatura
stringT = String(t);
stringH = String(h);
Serial.print("T:" );
Serial.println(t);
Serial.print("H:" );
Serial.println(h);
datos = "Temperatura: "+ stringT + " Humedad: " +stringH;
bot.sendMessage(chat_id, datos, "");//Enviamos la temperatura y humedad
    }
//////////Comando para encender el aire acondicionado//////
    if (text == "/Aireon")
    {
const uint16_t irSignal[227] = {
//Código para encendido de aire acondicionado marca Electra
       3100, 1600     , 500, 1100     , 450, 1100     , 450,  300
     , 500,  350     , 450,  350     , 450, 1100     , 450,  350
     , 450,  350     , 450, 1150     , 400, 1150     , 400,  400
     , 400, 1150     , 450,  350     , 450,  350     , 450, 1100
     , 450, 1150     , 400,  400     , 400, 1150     , 400, 1150
     , 400,  400     , 400,  400     , 400, 1150     , 450,  350
     , 450,  350     , 450, 1100     , 450,  350     , 450,  350
     , 450,  350     , 450,  350     , 450,  350     , 450,  350
     , 450,  350     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400, 1150
     , 400,  400     , 400,  400     , 400, 1150     , 450,  350
     , 450,  350     , 400,  400     , 400, 1150     , 450,  350
     , 450,  350     , 450,  350     , 450,  350     , 450,  350
     , 450,  350     , 450,  350     , 400,  400     , 400,  400
     , 400, 1150     , 450,  350     , 450,  350     , 450,  350
     , 450,  350     , 450,  350     , 450,  350     , 450,  350
     , 400,  400     , 400,  400     , 400,  400     , 400, 1150
     , 400,  400     , 450,  350     , 450,  350     , 450,  350
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 350,  450     , 400,  400
     , 350,  450     , 400,  400     , 400,  400     , 400,  400
     , 350,  450     , 350,  450     , 350,  450     , 350,  450
     , 350, 1200     , 400, 1150     , 400, 1150     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400

};
IrSender.sendRaw(irSignal, 227, 38);
bot.sendMessage(chat_id, "Comando encendido ejecutado", "");
aireEstado = 1;
    }
//////////Comando para apagar el aire acondicionado////////
    if (text == "/Aireoff")
    {
const uint16_t irSignal2[227] = {
//Código para apagado de aire acondicionado marca Electra
3100, 1600     , 450, 1100     , 500, 1100     , 450,  350
     , 450,  350     , 450,  350     , 450, 1100     , 450,  400
     , 400,  350     , 450, 1150     , 400, 1150     , 400,  400
     , 400, 1150     , 400,  400     , 400,  400     , 400, 1150
     , 400, 1150     , 400,  400     , 450, 1150     , 400, 1150
     , 400,  400     , 400,  400     , 400, 1150     , 400,  400
     , 400,  400     , 400, 1150     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  350     , 400, 1200     , 400,  400
     , 400,  400     , 400, 1150     , 400, 1150     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400, 1150     , 400,  450     , 350,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400, 1150
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 350,  450     , 350,  450     , 350,  450
     , 350,  450     , 350,  450     , 350,  450     , 350,  450
     , 350,  450     , 350,  450     , 350,  450     , 350,  450
     , 350,  450     , 350,  450     , 350,  450     , 350,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 400,  400
     , 400, 1150     , 400,  400     , 400,  400     , 400,  400
     , 400,  400     , 400,  400     , 400,  400     , 350,  500
     , 300,  450     , 400

};
IrSender.sendRaw(irSignal2, 227, 38);
bot.sendMessage(chat_id, "Comando apagado ejecutado", "");
aireEstado = 2;
    }
////////Último comando enviado ///////     

    if (text == "/Estado")
    {
      ////Ultimo comando enviado////
      if (aireEstado == 0)
      {
        bot.sendMessage(chat_id, "No definido", "");
      }
      if (aireEstado == 1)
      {
        bot.sendMessage(chat_id, "Último comando: Encendido", "");
      }
      if (aireEstado == 2)
      {
        bot.sendMessage(chat_id, "Último comando: Apagado", "");
      }
    }

    if (text == "/Ayuda")
    {
      String ayuda = "Bienvenido al sistema de control luces con Esp32, " ".\n";
      ayuda += "Estas son tus opciones.\n\n";
      ayuda += "/Aireon: para encender el aire \n";
      ayuda += "/Aireoff: para apagar el aire \n";
      ayuda += "/TyH : Muestra la Temperatura y Humedad \n";
      ayuda += "/Ayuda: Imprime este menú \n";
      ayuda += "Recuerda el sistema distingue entre mayuculas y minusculas \n";
      bot.sendMessage(chat_id, ayuda, "");
    }
    
  }
}

void setup()
{
  Serial.begin(115200);
  dht.begin();//Inicializar el sensor DHT
  // Intenta conectarse a la red wifi
  Serial.print("Conectando a la red ");
  Serial.print(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); //Agregar certificado raíz para api.telegram.org
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.print("\nConectado a la red wifi. Dirección IP: ");
  Serial.println(WiFi.localIP());
   if(inicio == 1){
    Serial.println("Sistema preparado");
    bot.sendMessage(ID_Chat, "Sistema preparado!!!, escribe /Ayuda para ver las opciones", "");//Enviamos un mensaje a telegram para informar que el sistema está listo
    inicio = 0;
    }
}

void loop()
{
  //Verifica si hay datos nuevos en telegram cada 1 segundo
  if (millis() - tiempoAnterior > tiempo)
  {
    int numerosMensajes = bot.getUpdates(bot.last_message_received + 1);

    while (numerosMensajes)
    {
      Serial.println("Comando recibido");
      mensajesNuevos(numerosMensajes);
      numerosMensajes = bot.getUpdates(bot.last_message_received + 1);
    }

    tiempoAnterior = millis();
  }
  
}

Credits

carlosvolt

carlosvolt

32 projects • 3 followers

Comments