Wednesday 19 April 2017
IR2MQTT
I wanted a good solution for using my Harmony IR remote with my automation PC for controlling lights and other functions but there isn't a great low cost option since the actual PC is in the basement and the receiver is in my living room...
I have a USB extender and a USB receiver and a remote that only works with the 10 button remote it came with, it also acts as a HID device and Windows won't let me see which keyboard sent the keystrokes or capture them without being in the foreground...
Enjoying my Wemos D1 clone so much, I ordered 4 more :P I also found out how to properly address the pins, you use D# instead of just an int with a standard Arduino.
I also got this IR receiver kit for less than $2.
Connecting it with the included wires was simple:
+ to 5v, - to GND and S to D4
The following sketch will broadcast the codes as numeric string to the MQTT topic IR2MQTT
You need to set your server IP in the code, then power it up, and it will broadcast a WiFi network for initial setup.
You just need to build a receiver to find codes you care about and then preform an action.
Just subscribe to the topic and press the button on your remote a few times until you see a common code...
/*
* IR 2 MQTT with $2 IR module kit
*
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <IRremoteESP8266.h>
const char* ssid = "..."; // Not used using WiFiManager
const char* password = "..."; // Not used using WiFiManager
int RECV_PIN = D4; //an IR detector connected to D4
IRrecv irrecv(RECV_PIN);
decode_results results;
//const char* mqtt_server = "broker.mqtt-dashboard.com"; // Public Broker
const char* mqtt_server = "YOUR LOCAL SERVER IP HERE";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
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);
WiFiManager wifiManager;
wifiManager.autoConnect("AutoConnectAP");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
irrecv.enableIRIn(); // Start the receiver
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
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())) {
Serial.println("connected");
// Once connected, publish an announcement...
//client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
// Bring up MQTT
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
// MQTT
if (!client.connected()) {
reconnect();
}
client.loop();
// IR
if (irrecv.decode(&results)) {
//Serial.println(results.value, HEX);
unsigned long buffer;
buffer = results.value;
String numberString = String(buffer);
char charBuf[50];
numberString.toCharArray(charBuf, 50);
Serial.println(buffer);
client.publish("IR2MQTT", charBuf);
irrecv.resume(); // Receive the next value
}
}
Subscribe to:
Post Comments (Atom)
Great post and very helpful! Here you will get all kind of solution like
ReplyDeleteRouterlogin
Routerlogin Net