For quite a while I wanted to integrate wifi infrared device, that could control all my IR devices around the home. So I finally bought a IR transmitter(KY-005) and a IR receiver(KY-022). To use those modules I also bought a ESP8266 NodeMCU. This whole project cost less than 10$, and require minimal electronic experience. The IR transmitter can also be salvaged from old TV remotes or other IR devices, if you really wanna spare a few bucks.
Both modules works perfect for capturing and transmitting IR signals. They are very simple to use, along with the IRremoteESP8266 library.
With the: IRremoteESP8266/examples/IRrecvDumpV2, i started recording every single button press from our JVC LT-40E71(A) TV.
From every single IR signal it receives it recorded something like this:
Timestamp : 000015.615
Encoding : NEC
Code : 5FA38C7 (32 bits)
Library : v2.3.0
Raw Timing[67]:
+ 9012, - 4506, + 602, - 540, + 590, - 538, + 590, - 538,
+ 590, - 538, + 590, - 538, + 554, - 1702, + 588, - 540,
+ 590, - 1670, + 590, - 1670, + 590, - 1666, + 592, - 1668,
+ 590, - 1668, + 616, - 1642, + 588, - 540, + 592, - 1666,
+ 590, - 538, + 590, - 538, + 588, - 538, + 592, - 1666,
+ 590, - 1668, + 572, - 1688, + 588, - 540, + 588, - 540,
+ 560, - 568, + 590, - 1668, + 560, - 1696, + 590, - 540,
+ 590, - 540, + 618, - 510, + 562, - 1696, + 560, - 1696,
+ 562, - 1696, + 562
uint16_t rawData[67] = {9012, 4506, 602, 540, 590, 538, 590, 538, 590, 538, 590, 538, 554, 1702, 588, 540, 590, 1670, 590, 1670, 590, 1666, 592, 1668, 590, 1668, 616, 1642, 588, 540, 592, 1666, 590, 538, 590, 538, 588, 538, 592, 1666, 590, 1668, 572, 1688, 588, 540, 588, 540, 560, 568, 590, 1668, 560, 1696, 590, 540, 590, 540, 618, 510, 562, 1696, 560, 1696, 562, 1696, 562}; // NEC 5FA38C7
uint32_t address = 0xA0;
uint32_t command = 0x1C;
uint64_t data = 0x5FA38C7;
To transmit the same signal I sent the following code: 5FA38C7
in the function irsend.sendNEC(5FA38C7, 32);
After figuring out how to recieve and transmit IR codes I combined it with a modifed verson of MQTT Light for homeassistant by: “Samuel M.”(Correct me if I’m wrong), so I could send the IRcode over MQTT to my ESP8266, then make it transmit it. (My modifications aren’t the best, still have left over unused code from the original version)
/*
MQTT Light for Home-Assistant - NodeMCU (ESP8266)
https://home-assistant.io/components/light.mqtt/
Libraries :
- ESP8266 core for Arduino : https://github.com/esp8266/Arduino
- PubSubClient : https://github.com/knolleary/pubsubclient
Sources :
- File Examples ES8266WiFi WiFiClient
- File Examples PubSubClient mqtt_auth
- File Examples PubSubClient mqtt_esp8266
Schematic :
- https://github.com/mertenats/open-home-automation/blob/master/ha_mqtt_light/Schematic.png
- GND - LED - Resistor 220 Ohms - D1/GPIO5
Configuration (HA) :
light:
platform: mqtt
name: Office light'
state_topic: 'office/light1/status'
command_topic: 'office/light1/switch'
optimistic: false
Samuel M. - v1.1 - 08.2016
If you like this example, please add a star! Thank you!
https://github.com/mertenats/open-home-automation
*/
#include <ESP8266WiFi.h
#include <PubSubClient.h
#ifndef UNIT_TEST
#include <Arduino.h
#endif
#include <IRremoteESP8266.h
#include <IRsend.h
#define MQTT_VERSION MQTT_VERSION_3_1_1
// Wifi: SSID and password
const char* WIFI_SSID = "*****";
const char* WIFI_PASSWORD = "*****";
// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "IRBlaster";
const PROGMEM char* MQTT_SERVER_IP = "192.168.0.5";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "******";
const PROGMEM char* MQTT_PASSWORD = "******";
// MQTT: topics
const char* MQTT_LIGHT_STATE_TOPIC = "irblaster1/recieve";
const char* MQTT_LIGHT_COMMAND_TOPIC = "irblast/send";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
IRsend irsend(4); // An IR LED is controlled by GPIO pin 4 (D2)
// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
// concat the payload into a string
String payload;
for (uint8_t i = 0; i < p_length; i++) {
payload.concat((char)p_payload[i]);
}
// handle message topic
if (String(MQTT_LIGHT_COMMAND_TOPIC).equals(p_topic)) {
// test if the payload is equal to "ON" or "OFF"
if (payload.equals(String(""))) {
}
else
{
Serial.print("payload:");
Serial.println(payload);
// Convert from String HEX to Hex long
long hexCmd;
hexCmd = strtol(&payload[0], NULL, 16);
irsend.sendNEC(hexCmd, 32);
Serial.print("INT:");
Serial.println(hexCmd);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("INFO: Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
Serial.println("INFO: connected");
// Once connected, publish an announcement...
//publishLightState();
// ... and resubscribe
client.subscribe(MQTT_LIGHT_COMMAND_TOPIC);
} else {
Serial.print("ERROR: failed, rc=");
Serial.print(client.state());
Serial.println("DEBUG: try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// init the serial
//Serial.begin(115200);
irsend.begin();
Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
// init the WiFi connection
Serial.println();
Serial.println();
Serial.print("INFO: Connecting to ");
WiFi.mode(WIFI_STA);
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("INFO: WiFi connected");
Serial.print("INFO: IP address: ");
Serial.println(WiFi.localIP());
// init the MQTT connection
client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
To send MQTT messages to the ESP8266 I used this Home Assistant configuration:
switch:
- platform: mqtt
name: "TV Toggle"
command_topic: "irblast/send"
payload_on: "5FA38C7"
optimistic: true
qos: 0
retain: true
Issues
- The first issue I have stumbled upon, during this small project is that the remote I’ve used, only have a Power Toggle button, which means it both turns the TV On and Off. Which makes it hard to fully automate it, since Home Assistant don’t know if it turns it OFF or ON. I’ve looked up IR codes for my TV, but all the results comes in raw data, but I rather have it in a simple HEX from like: “5FA38C7”.
I tried looking them up using: http://irdb.tk/db/, but the information I get is this:
"VOLUME UP","sendir,1:1,1,38000,1,37,319,160,20,60,20,60,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,60,20,60,20,60,20,60,20,20,20,20,20,20,20,898,20,60,20,60,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,60,20,60,20,60,20,60,20,20,20,20,20,20,20,898","0000 006D 0012 0011 013F 00A0 0014 003C 0014 003C 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 003C 0014 003C 0014 003C 0014 003C 0014 0014 0014 0014 0014 0014 0014 0382 0014 003C 0014 003C 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 003C 0014 003C 0014 003C 0014 003C 0014 0014 0014 0014 0014 0014 0014 0382",,
And I’ve no idea how to convert it to something like this: “5FA38C7”
- The second issue is that I can’t really find a single action button in Homeassistant that doesn’t have a toggle ON/OFF mode.
I need a way to only have it show one state, also with some style. Something like this:
https://community-home-assistant-assets.s3-us-west-2.amazonaws.com/optimized/3X/b/1/b1c9881a27bcacd6239b446ffff00a126c80466b_1_417x500.png
Future Plans
I plan to expand the project, so that I also can receive IR signals and make them toggle certain events in homeautomation, like turn ON/OFF lights, or maybe keep track on the television states(when the normal remote is used) like volume/channel/mode, so I can from Home Assistant see what channel is being watch, when the TV last were being used.
Conclusion
I hope that some could use this to get started making their own, simple WIFI IR transmitter or get inspired to. My code isn’t great at all, this is nothing but my approach to this project.