IR remote using MQTT and NodeMCU

Hey!
I just wanted to share my first DIY project with home assistant. This project controls my Sony TV and Pioneer audio amplifier on and off via 2 ir LED’s.
49
First I used one of the IRremote librarys demo’s with an Arduino nano and an IR receiver to figure out the correct IR codes for my remotes. Then I installed the proper IRremote library for the NodeMCU and connected 2 infrared LED.

I had problems with the IR LED’s range, so my Ghetto solution was to tape them on to the TV and Pioneer amlifier. I was a little worried about how wife-proof this solution would be, but to my surprise her comment was “what a nice little antennas you got going on there”. :joy:[picture coming soon]

Next I used this guy’s awesome guide to connect my node MCU to Home assistant via MQTT. I just changed the cover to switch in configuration.yaml as shown in here:

switch:

  • platform: mqtt
    name: Televisio
    friendly_name: Garage
    state_topic: “garage/door”
    command_topic: “garage/button”
    payload_on: “OPEN”
    payload_off: “OPEN”
    optimistic: false
    retain: false
    value_template: ‘{{ value }}’

In the future I also plan to add volume and channel controls. Please feel free to ask anything. I will add more photos of this tommorrow. :slight_smile:

2 Likes

Can you share your sketch?

Well… I rather not but here it is. All credit goes to the guy linked above :slight_smile: The code is he’s. I am sorry about the messiness of the code.

As you can see, I have added the volume/ channel up and down functions since the last post.

#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //GitHub - tzapu/WiFiManager: ESP8266 WiFi Connection manager with web captive portal WiFi Configuration Magic
#include <PubSubClient.h> //MQTT
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>

// includes for the IR functions
#ifndef UNIT_TEST
#include <Arduino.h>
#endif
#include <IRremoteESP8266.h>
#include <IRsend.h>

////START CUSTOM PARAMS********//

//Define parameters for the http firmware update
const char* host = “TELEVISIO”;
const char* update_path = “/WebFirmwareUpgrade”;
const char* update_username = “admin”;
const char* update_password = “YourPassWordHere”;

//Define the pins

IRsend irsend(4);

//Define MQTT Params.
#define mqtt_server “yourIP”
#define door_topic “olohuone/tv” // Change these to yours!
#define button_topic “olohuone/tv” // Change these to yours!
const char* mqtt_user = “xxx”; // Change these to yours!
const char* mqtt_pass = “yyy”;// Change these to yours!

//END CUSTOM PARAMS********//
//This can be used to output the date the code was compiled
const char compile_date = DATE " " TIME;

//Setup the web server for http OTA updates.
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;

WiFiClient espClient;

//Initialize MQTT
PubSubClient client(espClient);

//Setup Variables
String switch1;
String strTopic;
String strPayload;
char* door_state = “UNDEFINED”;
char* last_state = “”;

//Wifi Manager will try to connect to the saved AP. If that fails, it will start up as an AP
//which you can connect to and setup the wifi
WiFiManager wifiManager;
long lastMsg = 0;

void setup() {
//Set Relay(output) and Door(input) pins
pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
irsend.begin();
Serial.begin(115200);
digitalWrite(0, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(2, HIGH);
//Set the wifi config portal to only show for 3 minutes, then continue.
wifiManager.setConfigPortalTimeout(180);
wifiManager.autoConnect(host);

//sets up the mqtt server, and sets callback() as the function that gets called
//when a subscribed topic has data
client.setServer(mqtt_server, 1883);
client.setCallback(callback); //callback is the function that gets called for a topic sub

//setup http firmware update page.
MDNS.begin(host);
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();
MDNS.addService(“http”, “tcp”, 80);
Serial.printf(“HTTPUpdateServer ready! Open http://%s.local%s in your browser and login with username ‘%s’ and your password\n”, host, update_path, update_username);
}

void loop() {
//If MQTT client can’t connect to broker, then reconnect
if (!client.connected()) {
reconnect();
}

client.loop(); //the mqtt function that processes MQTT messages
httpServer.handleClient(); //handles requests for the firmware update page
}

void callback(char* topic, byte* payload, unsigned int length) {
//if the ‘garage/button’ topic has a payload “OPEN”, then ‘click’ the relay
payload[length] = ‘\0’;
strTopic = String((char*)topic);
if (strTopic == button_topic)
{
switch1 = String((char*)payload);
if (switch1 == “OPEN”)
{
// Send the ir codes
Serial.println(“Sony”);
irsend.sendSony(0x540C, 15);
delay(10);
irsend.sendSony(0x540C, 15);
delay(500);

Serial.println(“Sony”);
irsend.sendSony(0xa90, 12, 2);
delay(10);
irsend.sendSony(0xa90, 12, 2);
delay(1000);
}

// Switch the tv channel to HDMI1
else if (switch1 == “HDMI”) {

irsend.sendSony(0xa50, 12, 2); // Paina lähteen valinta nappia
delay (300);
for (int i = 1; i <=5; i++){
irsend.sendSony(0xaF0, 12, 2); // Paina alaspäin nappia 5 kertaa
delay(250);  }
irsend.sendSony(0xa70, 12, 2); // PAINA OK
delay(1000);

}

// Change volume UP
// VAhvistimen koodi olisi: NEC: A55A50AF
else if (switch1 == “VOLUMEUP”) {
irsend.sendSony(0x490, 12, 2);
delay(500);
}

// Change volume DOWN
// VAhvistimen koodi olisi: NEC: A55AD02F
else if (switch1 == “VOLUMEDOWN”) {
irsend.sendSony(0xc90, 12, 2);
delay(500);
}
// Seuraava kanava
else if (switch1 == “CHUP”) {
irsend.sendSony(0x90, 12, 2);
delay(500);
}
// Edellinen kanava
else if (switch1 == “CHDOWN”) {
irsend.sendSony(0x890, 12, 2);
delay(500);
}
// MUTE
else if (switch1 == “MUTE”) {
irsend.sendSony(0x290, 12, 2);
delay(500);
}

}
}

void reconnect() {
//Reconnect to Wifi and to MQTT. If Wifi is already connected, then autoconnect doesn’t do anything.
wifiManager.autoConnect(host);
Serial.print(“Attempting MQTT connection…”);
if (client.connect(host, mqtt_user, mqtt_pass)) {
Serial.println(“connected”);
client.subscribe(“olohuone/#”);
} else {
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}

I gave up with my node in the end and used Lirc on an old zero - set up was seamless!