Air Conditioning On/off Control Via Telegram and Esp32

by CarlosVoltT in Circuits > Remote Control

86 Views, 2 Favorites, 0 Comments

Air Conditioning On/off Control Via Telegram and Esp32

IMG_20220417_230920384-820x461 (1).jpg

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

In this tutorial we will see how to control an air conditioner, with an esp32 and the telegram application, from anywhere in the world, where we have access to the internet. The device can capture the commands of an air conditioner with an infrared receiver module, and in this project we will control the on and off with the telegram application, but we will also be able to monitor the change in temperature, to see if it has actually been turned on.


Female pins

ky-022 infrared receiver module

Size: 6.4*7.4*5.1MM, acceptance angle 90°, working voltage 2.7-5.5V.

Frequency 37.9KHZ, receiving distance 18m.

Daylight rejection up to 500LUX, electromagnetic interference capability, built-in infrared dedicated IC.

Widely used: stereo, TV, VCR, CD, set-top boxes, digital photo frame, car audio, remote control toys, satellite receivers, hard disk, air conditioning, heating, fans, lighting and other household appliances.

Pinout:

1 …. GND (-)

2 …. + 5V

3 …. Output (S)

Un transistor 2n3904

Description

SMALL SIGNAL BIPOLAR TRANSISTOR

Detailed description

Transistors – Bipolar (BJT) – Single NPN 40 V 200 mA 300 MHz 625 mW Through Hole TO-92-3

Data sheet

2n3904

Pinout

A 100 Ohm resistor

A 470 Ohm resistor

A DHT11 Temperature and Humidity Sensor

This module consists of a DHT11 digital humidity and temperature sensor and a 1 kΩ resistor. The DHT11 uses an internal thermistor and a capacitive humidity sensor to determine environmental conditions, an internal chip is responsible for converting the readings to a serial digital signal.

Cables Dupont

An infrared emitter

A PCB

Gerber file —> Gerber_Air conditioning control by telegram

Circuit



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();
}
}