MQTT IR Blaster for my OpenHab Setup (nodemcu+MQTT+openhab+IR)

A few months back I started to setup a dedicated home automation system to control my ever-growing number of “smart” devices.

Smart devices are great but they usually have separate interfaces and mobile programs to install and control each device. What is really needed is a single program to control all of my devices… from a single interface.

Well that in a nutshell is Openhab, a software suite developed specifically for controlling smart devices from a range of manufacturers and industries, utilizing many different communication protocols.

One of these communication protocols is MQTT. I wont go into the specifics of this protocol, only that it is widely used in industrial applications and has even been used for face-books messenger app.

Now the reason for designing an IR blaster that uses MQTT to trigger the IR LED’s is that I’m already using it to control several appliances in my apartment.

Specifically I’m using Nodemcu boards with Arduino compatible code to receive an MQTT command and do some action… i.e. turn on or off a relay

Except this time instead of turning on and off a relay I will use a separate Arduino library called IRsend.h to activate the IR leds through a digital pin on the nodemcu board.

Here is the Arduino code for the Nodemcu boards:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <IRsend.h>

IRsend irsend(14);  // An IR LED is controlled by GPIO pin 14 (D5) on nodemcu boards

// Update these with values suitable for your network.

//openhab MQQT settings
const char* ssid = "********";
const char* password = "********";
const char* mqtt_server = "";
const char* brokerusername = "********";
const char* brokerpassword = "********";

IPAddress ip(192, 168, 0, 114); // where xx is the desired IP Address
IPAddress gateway(192, 168, 0, 1); // set gateway to match your network
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");

  WiFi.config(ip, gateway, subnet);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {


  Serial.println("WiFi connected");
  Serial.println("IP address: ");

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print("] ");
  String p_payload;
  for (int i = 0; i < length; i++) {

    // Convert from String HEX to Hex long
    long hexCmd;
    hexCmd = strtol(&p_payload[0], NULL, 16);
    irsend.sendNEC(hexCmd, 32);

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), brokerusername, brokerpassword)) {
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void setup() {
  client.setServer(mqtt_server, 1883);
  ArduinoOTA.onStart([]() {
  ArduinoOTA.onEnd([]() {
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");

void loop() {

  if (!client.connected()) {

The code posted above is pretty simple, its using an MQTT communication library for receiving commands from Openhab. It subscribes to the “apartment/livingroom/ir_blaster/state” topic. Openhab publishes an IR code to this topic and the code here receives that command, and uses the IRsend library to pulse pin 14 (D5) on the nodemcu boards.

I decided on using 6 IR LED’s arranged in a semicircular pattern to entirely cover my living room. Each LED is 30 degrees from the next, with 6 LED’s its able to cover more than 180 degrees. I used a single PN222A transistor driven through pin 14, which switches the IR LED’s. There are three series pairs of IR leds in parallel running through the PN222A, with this configuration I do not require current limiting resistors.

Here is the circuit:

And then I designed and 3D printed this case:

Now all that’s left to do is send some IR codes to the device through the subscribed MQQT channel.

I recorded the IR codes from several of my favourite devices remotes and then set up some items and button in my Openhab UI.

UI currently looks like this:

It works perfectly!! Next I need to design some nice UI’s to allow control of my home entertainment system through a tablet.

If anyone wants a tutorial on how to record IR codes from remotes let me know.

Here is a shot of the IR codes being recorded:

3 Replies to “MQTT IR Blaster for my OpenHab Setup (nodemcu+MQTT+openhab+IR)”

  1. Hi,

    Can I ask you for help if I paste raw codes from the air conditioner? Can I use nodeMCY v2 for this project? Please help me.

  2. Just a short question, how did you know that every LED can handle 2.5V? I`m also planning a multi LED setup, but using BC547 transistors. If I use the 5V output and are only using 4 LEDs (2-in-a-row), will this also work?
    Thanks for your answer in advance.

Leave a Reply

Your email address will not be published.