mqtt-ir-remote/mqtt_ir.ino

176 lines
4.2 KiB
C++

#include <IRremoteESP8266/src/IRremoteESP8266.h>
#include <IRremoteESP8266/src/IRsend.h>
#include <IRremoteESP8266/src/IRrecv.h>
#include <IRremoteESP8266/src/IRutils.h>
#include <time.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
//#include <IRremoteESP8266/src/>
#define IRLED_PIN 4
#define RECV_PIN 5
#define TOPIC "foobar/aerie/lounge/hifi"
// Update these with values suitable for your network.
const char* ssid = "foobar";
const char* password = "";
const char* mqtt_server = "10.42.0.244";
IRsend irsend(IRLED_PIN);
IRrecv irrecv(RECV_PIN); //1024, 15U, true);
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);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
randomSeed(micros());
/*
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
*/
}
uint16_t input[100] = {0};
int check(char * buf, int length){
char * a = buf;
for (;a-buf < length;a++){
Serial.println(a);
if (*a >= 58 || *a <= 47)
return 0;
}
return 1;
}
int checkHex(char * buf, int length){
char * a = buf;
for (;a-buf < length;a++){
//Serial.println(a);
if (!(*a < 58 && *a > 47 || *a > 64 && *a < 71 || *a > 96 && *a < 103))
return 0;
}
return 1;
}
void callback(char* topic, byte* p, unsigned int length) {
char *payload = (char *)p;
if ( length >= 6 ){
if ( strncmp(payload, "beamer", 6) == 0){
irsend.sendNEC(0x5ea1936cUL,32);
}
if ( strncmp(payload, "lauter", 6) == 0){
irsend.sendNEC(0x5EA158A7UL,32);
}
if ( strncmp(payload, "leiser", 6) == 0){
irsend.sendNEC(0x5EA1D827UL,32);
}
}
if ( length >= 7 ){
if ( strncmp(payload, "standby", 7) == 0){
irsend.sendNEC(0x7E81FE01UL,32);
}
}
if ( length >= 5 ){
if ( strncmp(payload, "power", 5) == 0){
irsend.sendNEC(0x7e817e81UL,32);
}
}
if ( length >= 3 ){
if ( strncmp(payload, "dvd", 3) == 0){
irsend.sendNEC(0x5ea1837cUL,32);
}
if ( strncmp(payload, "mpd", 3) == 0){
irsend.sendNEC(0x5ea1c837UL, 32);
}
if ( strncmp(payload, "dtv", 3) == 0){
irsend.sendNEC(0x5ea12ad5UL,32);
}
}
if ( length > 11 && strncmp(payload, "nec:", 4) == 0){ //custom:9999 FF
//Serial.println("Custom0");
if ( checkHex(payload+4, 8)){
char *start = payload + 4;
char *end = payload + 7 + 4;
*(end+1) = ' ';
unsigned int nec_code = strtoul (start, &end, 16);
irsend.sendNEC(nec_code, 32);
char buff[20];
char * pos = buff;
Serial.println(nec_code);
pos += sprintf(pos, "%s","Sent NEC Code: ");
pos += sprintf(pos, "%X", nec_code);
Serial.println(buff);
client.publish(TOPIC, buff);
}
}
}
void reconnect() {
while (!client.connected()) {
String clientId = "HIFI LOUNGE CTRL";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str())) {
client.publish(TOPIC, "HIFI Control Online");
client.subscribe(TOPIC);
} else {
delay(5000);
}
}
}
void setup() {
pinMode(IRLED_PIN, OUTPUT);
digitalWrite(IRLED_PIN, LOW); // Turn the LED on (Note that LOW is the voltage level// Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
irrecv.enableIRIn(); // Start the receiver
}
decode_results results;
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
if (irrecv.decode(&results)) {
Serial.println("entered irrecv decoding phase");
if (results.overflow){
client.publish(TOPIC, "Parsing failed!");
} else {
char msg_buf[51] = "32bit Representation NEC Code:\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
itoa(results.value, (msg_buf+30), 16);
serialPrintUint64(results.value, HEX);
client.publish(TOPIC, msg_buf);
}
irrecv.resume();
}
}