Hello everyone! I’ve been diving into the world of home automation lately and wanted to share my experience with integrating MQTT using an ESP8266 module. It’s been an exciting journey, and I hope this post can inspire others to explore similar projects.
I recently set up a simple MQTT IR transmitter/receiver using an ESP8266 module. The setup involved connecting an IR LED and an IR receiver to the ESP8266, which was then integrated with MQTT for communication. The code was straightforward, leveraging libraries like IRremoteESP8266 and PubSubClient. I’ve included the code snippet below for those interested in replicating the setup.
cpp
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <IRremoteESP8266.h>
#include <PubSubClient.h>
// Configuration details
const char* ssid = “MY-SSID”;
const char* password = “MY-WIFI-PSK”;
const char* mqtt_server = “10.10.10.10”;
const char* mqtt_user = “mqtt_user”;
const char* mqtt_pass = “mqtt_pass”;
// MQTT topics
const char* topicSubscribe = “esp8266/02/sender/#”;
const char* topicPrefix = “esp8266/02/”;
const char* topicRaportPrefix = “esp8266/02/info/”;
// IR components
int RECV_PIN = 0;
IRrecv irrecv(RECV_PIN);
IRsend irsend(3);
// MQTT client
PubSubClient client(mqtt_server, 1883);
void callback(char* topic, byte* payload, unsigned int length) {
// Payload processing and IR sending logic
}
void connect_to_MQTT() {
// MQTT connection logic
}
void setup() {
// Initialization and setup code
}
void loop() {
// Main loop for MQTT and IR handling
}
One of the challenges I faced was ensuring stable communication between the ESP8266 and the MQTT broker. I found that using pull-up resistors and ensuring proper power supply (8V to 3.3V conversion) was crucial. Additionally, I had to experiment with different GPIO pins to avoid stability issues, eventually settling on GPIO-3 for the IR LED output.
For enclosure, I opted for a sleek solution using an old IR extender’s casing. This not only provided a neat finish but also allowed me to utilize the existing power supply, minimizing clutter.
I also integrated this setup with OpenHAB, creating items to receive and send IR codes over MQTT. For example, I added an item to detect NEC and RC5 codes and mapped them to specific actions. This integration allowed me to control various devices seamlessly through my smartphone.
While the project is functional, I’m planning to migrate to a Homie-aware codebase with OTA support for easier updates. However, the current setup works perfectly for my needs, so this task has taken a backseat for now.
I’d love to hear from others who have undertaken similar projects or have suggestions for improvement. Feel free to share your experiences or ask questions below! Happy coding and automating!