Infra távirányító ESP8266-tal ioBrokerhez MQTT-vel

Egy infravörös távirányítót készítünk egy NodeMCU ESP8266 és infra LED segítségével. Ezzel az ioBrokerből MQTT protokollon bármilyen infravörös vevővel rendelkező eszköz, pl: TV, Erősitő, SetTopBox, Klíma, stb. vezérelhető.

Nem szaporítom tovább a szót, lássuk az áramkört.
Csak egy NodeMCU ESP8266-ra és egy Infravörös LED-re lesz szükségünk.

NodeMCU ESP8266 és InfraLED
NodeMCU ESP8266 és InfraLED

Ennyi, ez gyorsan elkészült.
Most jöhet az Arduino vázlat:

/****************************************************************/
//  NodeMCU 1.0(ESP-12E Module)
//  Flash size: 4M (1M SPIFFS)
/****************************************************************/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
const char* ssid = "WIFI-ssid";
const char* password = "WIFI-jelszó";
const char* mqtt_server = "192.168.xx.xx";  // Az ioBroker ip címe
int IrLed = D2;
WiFiClient espClient;
PubSubClient client(espClient);
IRsend irsend(IrLed);
void setup()
{
 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED)
 {
   delay(500);
 }
 client.setServer(mqtt_server, 1883);
 client.setCallback(callback);
 irsend.begin();
}
void loop()
{
 if (!client.connected())
 {
   reconnect();
 }
 client.loop();
}
void callback(char* topic, byte* payload, unsigned int length)
{
 payload[length] = '\0';
 String strTopic = String(topic);
 String strPayload = String((char * ) payload);
 if(strTopic == "Infrared_topic")
 {
   if(strPayload == "false")
   {
     irsend.sendRC5(0x4C, 12); // ide kell beillesztened a megfelelő IR kódot
   }
   if (strPayload == "true")
   {
     irsend.sendRC5(0x4C, 12);
   }
 }
}
void reconnect()
{
 while (!client.connected())
 {
   String clientId = "ESP8266_IR";
   if (client.connect(clientId.c_str()))
   {
     client.subscribe("Infrared_topic");
   }
   else
   {
     delay(6000);
   }
 }
}

Most be kell szerezni az infra kódokat az eszközök távirányítójáról.
Ehhez az alábbi bonyolult áramkört fogjuk használni. Arduino Nano és egy infra vevő

Arduino NANO és az infravevő
Arduino NANO és az infravevő

A következő kódot töltsük fel a Nanora:

/*
 *    IrReceiver pinout:
       ___    
      |(X)|   1. pin =>  adat-- D11
      |___|   2. pin =>  gnd -- D10
      | | |   3. pin =>  5 V -- D9
      | | |
      1.2.3
*/
#include <IRremote.h>
int recvVcc = 9;
int recvGnd = 10;
int recvPin = 11;
IRrecv irrecv(recvPin);
decode_results results;
void setup()
{
 Serial.begin(9600);
 irrecv.enableIRIn();
 
 pinMode(recvGnd, OUTPUT);
 pinMode(recvVcc, OUTPUT);
 digitalWrite(recvGnd, LOW);
 digitalWrite(recvVcc, HIGH);
}
void dump(decode_results *results) 
{
 int count = results->rawlen;
 if (results->decode_type == UNKNOWN) {
   Serial.print("Unknown encoding: ");
 }
 else if (results->decode_type == NEC) {
   Serial.print("Decoded NEC: ");
 }
 else if (results->decode_type == SONY) {
   Serial.print("Decoded SONY: ");
 }
 else if (results->decode_type == RC5) {
   Serial.print("Decoded RC5: ");
 }
 else if (results->decode_type == RC6) {
   Serial.print("Decoded RC6: ");
 }
 else if (results->decode_type == PANASONIC) {
   Serial.print("Decoded PANASONIC - Address: ");
   Serial.print(results->address, HEX);
   Serial.print(" Value: ");
 }
 else if (results->decode_type == LG) {
   Serial.print("Decoded LG: ");
 }
 else if (results->decode_type == JVC) {
   Serial.print("Decoded JVC: ");
 }
 else if (results->decode_type == AIWA_RC_T501) {
   Serial.print("Decoded AIWA RC T501: ");
 }
 else if (results->decode_type == WHYNTER) {
   Serial.print("Decoded Whynter: ");
 }
 Serial.print(results->value, HEX);
 
 Serial.println();
}
void loop() 
{
 if (irrecv.decode(&results)) 
 {
   Serial.println(results.value, HEX);
   dump(&results);
   irrecv.resume();
 }
}

Ha kész inditsuk el a soros monitort az Arduinoban, majd nyomjunk meg a távirányító azon gombját, amelyik kódját szeretnénk beilleszteni a NodeMCU ESP8266 vázlatába. Valami ilyesminek kell történni:

Arduino IrDump, IR kódok a soros monitoron
Arduino IrDump, IR kódok a soros monitoron

Remélem tetszett a cikk, és hasznos információ volt a számodra.