He gives a lot of information about how to use an ESP8266 or ESP32 in conjunction with an IR transmitter to remotely control an airco.
The brands that can be switched can be found on this page.
Needed:
- ESP8266 or ESP32
- IR transmitter
- Arduino installed
Here is an example Arduino file I have found on Randomnerdtutorials.
Code: Select all
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
* https://randomnerdtutorials.com/esp32-mqtt-publish-subscribe-arduino-ide/
*********/
// Load Wi-Fi library
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <PubSubClient.h>
//#include <Wire.h>
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <ir_MitsubishiHeavy.h>
// Replace the next variables with your SSID/Password combination
const char* ssid = "your-ssid";
const char* password = "your-password";
// Add your MQTT Broker IP address, example:
const char* mqtt_server = "192.168.1.144";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
// LED Pin for testing
const int ledPin = 2; // D4
// voor Mitsubishi
const uint16_t kIrLed = 4; // ESP8266 GPIO pin to use. Recommended: 4 (D2).
IRMitsubishiHeavy152Ac ac(kIrLed); // Set the GPIO used for sending messages.
void setup() {
Serial.begin(115200);
// default settings
// Set up what we want to send.
// See ir_MitsubishiHeavy.(cpp|h) for all the options.
Serial.println("Default state of the remote.");
Serial.println("Setting desired state for A/C.");
ac.setPower(true); // Turn it on.
ac.setFan(kMitsubishiHeavy152FanAuto); // Medium Fan
ac.setMode(kMitsubishiHeavyCool); // Cool mode
ac.setTemp(23); // Celsius
ac.setSwingVertical(kMitsubishiHeavy152SwingVHighest); // Swing vertically
ac.setSwingHorizontal(kMitsubishiHeavy152SwingHMiddle); // Swing Horizontally
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(ledPin, OUTPUT);
}
/*
Set up what we want to send. See ir_MitsubishiHeavy.(cpp|h) for all the options.
kMitsubishiHeavyAuto = 0; // 0b000
kMitsubishiHeavyCool = 1; // 0b001
kMitsubishiHeavyDry = 2; // 0b010
kMitsubishiHeavyFan = 3; // 0b011
kMitsubishiHeavyHeat = 4; // 0b100
kMitsubishiHeavyMinTemp = 17; // 17C
kMitsubishiHeavyMaxTemp = 31; // 31C
kMitsubishiHeavy152FanAuto = 0x0; // 0b0000
kMitsubishiHeavy152FanLow = 0x1; // 0b0001
kMitsubishiHeavy152FanMed = 0x2; // 0b0010
kMitsubishiHeavy152FanHigh = 0x3; // 0b0011
kMitsubishiHeavy152FanMax = 0x4; // 0b0100
kMitsubishiHeavy152FanEcono = 0x6; // 0b0110
kMitsubishiHeavy152FanTurbo = 0x8; // 0b1000
kMitsubishiHeavy152SwingVAuto = 0; // 0b000
kMitsubishiHeavy152SwingVHighest = 1; // 0b001
kMitsubishiHeavy152SwingVHigh = 2; // 0b010
kMitsubishiHeavy152SwingVMiddle = 3; // 0b011
kMitsubishiHeavy152SwingVLow = 4; // 0b100
kMitsubishiHeavy152SwingVLowest = 5; // 0b101
kMitsubishiHeavy152SwingVOff = 6; // 0b110
kMitsubishiHeavy152SwingHAuto = 0; // 0b0000
kMitsubishiHeavy152SwingHLeftMax = 1; // 0b0001
kMitsubishiHeavy152SwingHLeft = 2; // 0b0010
kMitsubishiHeavy152SwingHMiddle = 3; // 0b0011
kMitsubishiHeavy152SwingHRight = 4; // 0b0100
kMitsubishiHeavy152SwingHRightMax = 5; // 0b0101
kMitsubishiHeavy152SwingHRightLeft = 6; // 0b0110
kMitsubishiHeavy152SwingHLeftRight = 7; // 0b0111
kMitsubishiHeavy152SwingHOff = 8; // 0b1000
*/
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic airco/output , you check if the message is either "on" or "off".
// Changes the output state according to the message
if (String(topic) == "airco/output") {
Serial.print("1 Output receveid output : ");
if(messageTemp == "on"){
Serial.println("2 LedPin on");
digitalWrite(ledPin, HIGH);
Serial.println("2 Cooling is switched ON");
ac.begin();
delay(200);
ac.setPower(true); // Turn it on.
delay(200);
ac.send();
delay(1200);
}
else if(messageTemp == "off"){
Serial.println("3 LedPin off");
digitalWrite(ledPin, LOW);
Serial.println("3 Cooling is switched OFF");
ac.begin();
delay(200);
ac.setPower(false); // Turn it off.
delay(200);
ac.send();
delay(1200);
}
else if(messageTemp == "Vswing/auto"){
Serial.println("Set Vswing/auto");
ac.begin();
delay(200);
ac.setSwingVertical(kMitsubishiHeavy152SwingVAuto);
delay(200);
ac.send();
delay(1200);
}
else if(messageTemp == "Vswing/highest"){
Serial.println("Set Vswing/Highest");
ac.begin();
delay(200);
ac.setSwingVertical(kMitsubishiHeavy152SwingVHighest);
delay(200);
ac.send();
delay(1200);
}
else if(messageTemp == "Vswing/high"){
Serial.println("Set Vswing/High");
ac.begin();
delay(200);
ac.setSwingVertical(kMitsubishiHeavy152SwingVHigh);
delay(200);
ac.send();
delay(1200);
}
else if(messageTemp == "Vswing/middle"){
Serial.println("Set Vswing/Middle");
ac.begin();
delay(200);
ac.setSwingVertical(kMitsubishiHeavy152SwingVMiddle);
delay(200);
ac.send();
delay(1200);
}
else if(messageTemp == "Vswing/low"){
Serial.println("Set Vswing/Low");
ac.begin();
delay(200);
ac.setSwingVertical(kMitsubishiHeavy152SwingVLow);
delay(200);
ac.send();
delay(1200);
}
else if(messageTemp == "Vswing/low"){
Serial.println("Set Vswing/Lowest");
ac.begin();
delay(200);
ac.setSwingVertical(kMitsubishiHeavy152SwingVLowest);
delay(200);
ac.send();
delay(1200);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Subscribe
client.subscribe("airco/output");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 5000) {
lastMsg = now;
/*
// Temperature in Celsius
temperature = bme.readTemperature();
// Uncomment the next line to set temperature in Fahrenheit
// (and comment the previous temperature line)
//temperature = 1.8 * bme.readTemperature() + 32; // Temperature in Fahrenheit
// Convert the value to a char array
char tempString[8];
dtostrf(temperature, 1, 2, tempString);
Serial.print("Temperature: ");
Serial.println(tempString);
client.publish("esp32/temperature", tempString);
humidity = bme.readHumidity();
// Convert the value to a char array
char humString[8];
dtostrf(humidity, 1, 2, humString);
Serial.print("Humidity: ");
Serial.println(humString);
client.publish("esp32/humidity", humString); */
}
}
, tested with a bit from here, left a bit of the temperature and humidity reading in place in case you want the ESP to read that for you, , installed mosquitto on a RPI and made 2 switches. (example like I mentioned before) Then added some dzvents code for the switches.
Code: Select all
return {
on = {
devices = {'Airco cooling',},
},
data = {},
logging = {
-- Level can be domoticz.LOG_INFO, domoicz.LOG_MODULE_EXEC_INFO, domoticz.LOG_DEBUG, domoticz.LOG_ERROR or domoticz.LOG_FORCE
--level = domoticz.LOG_INFO,
level = domoticz.LOG_DEBUG,
marker = '---- AIRCO ON/OF SWITCH ----',
},
execute = function(domoticz, triggerObject)
local OnOffSwitch = domoticz.devices('Airco cooling')
local VSwing = domoticz.devices('Airco VSwing')
domoticz.log('1 OnOffSwitch state :' .. OnOffSwitch.state,domoticz.LOG_DEBUG)
if OnOffSwitch.state == 'On' then
domoticz.log('2 OnOffSwitch is on :' .. OnOffSwitch.state,domoticz.LOG_DEBUG)
domoticz.executeShellCommand('mosquitto_pub -d -t airco/output -h 192.168.1.144 -m "on"')
domoticz.devices('Airco settings').rename('$Airco settings')
elseif OnOffSwitch.state == 'Off' then
domoticz.log('3 OnOffSwitch is off :' .. OnOffSwitch.state,domoticz.LOG_DEBUG)
domoticz.executeShellCommand('mosquitto_pub -d -t airco/output -h 192.168.1.144 -m "off"')
domoticz.devices('$Airco settings').rename('Airco settings')
end
end
}
Code: Select all
return {
on = {
devices = {'Airco VSwing',},
},
data = {},
logging = {
-- Level can be domoticz.LOG_INFO, domoicz.LOG_MODULE_EXEC_INFO, domoticz.LOG_DEBUG, domoticz.LOG_ERROR or domoticz.LOG_FORCE
--level = domoticz.LOG_INFO,
level = domoticz.LOG_DEBUG,
marker = '--------------MQTT--------------',
},
execute = function(domoticz, triggerObject)
local OnOffSwitch = domoticz.devices('Airco cooling')
local VSwing = domoticz.devices('Airco VSwing')
domoticz.log('1 OnOffSwitch state :' .. OnOffSwitch.state,domoticz.LOG_DEBUG)
if OnOffSwitch.state == 'On' then
if VSwing.state == 'Auto' then
domoticz.log('4 Vswing set to auto :' .. VSwing.state,domoticz.LOG_DEBUG)
domoticz.executeShellCommand('mosquitto_pub -d -t airco/output -h 192.168.1.144 -m "Vswing/auto"')
elseif VSwing.state == 'Highest' then
domoticz.log('5 Vswing set to highest :' .. VSwing.state,domoticz.LOG_DEBUG)
domoticz.executeShellCommand('mosquitto_pub -d -t airco/output -h 192.168.1.144 -m "Vswing/highest"')
elseif VSwing.state == 'High' then
domoticz.log('6 Vswing set to high :' .. VSwing.state,domoticz.LOG_DEBUG)
domoticz.executeShellCommand('mosquitto_pub -d -t airco/output -h 192.168.1.144 -m "Vswing/high"')
elseif VSwing.state == 'Middle' then
domoticz.log('7 Vswing set to middle :' .. VSwing.state,domoticz.LOG_DEBUG)
domoticz.executeShellCommand('mosquitto_pub -d -t airco/output -h 192.168.1.144 -m "Vswing/middle"')
elseif VSwing.state == 'Low' then
domoticz.log('8 Vswing set to low :' .. VSwing.state,domoticz.LOG_DEBUG)
domoticz.executeShellCommand('mosquitto_pub -d -t airco/output -h 192.168.1.144 -m "Vswing/low"')
elseif VSwing.state == 'Lowest' then
domoticz.log('9 Vswing set to lowest :' .. VSwing.state,domoticz.LOG_DEBUG)
domoticz.executeShellCommand('mosquitto_pub -d -t airco/output -h 192.168.1.144 -m "Vswing/lowest"')
end
end
end
}
The code works like expected. Test while the ESP is attached to Arduino and you see what happens in the serial monitor.
The reach from this IR transmitter is only 3 or 4 meters max.
I left the part of the code where a led is switched to supervise the Domoticz command has arrived at the ESP.
The LED on the transmitter lights up when a signal is sent.
Pick up your own airo from the list and test.
Contributers who can improve the code or add new features are invited to share their thoughts.