Software and libraries for this project

First let's install the library IRremoteESP8266. For that go to Sketch | Include Library | Manage Libraries... and search for IRremoteESP8266 library like in the following screenshot:

After the library is installed and the connections to the ESP are done, let's use the following code to send over an MQTT topic a command to open or close a Panasonic TV. Since the same Infrared command is used to power on or power off, you just need to receive something on the MQTT topic to send an infrared command to the TV:

#include <ArduinoJson.h> 
#include <ESP8266WiFi.h> 
#include <ESP8266mDNS.h> 
#include <WiFiUdp.h> 
#include <PubSubClient.h> 
#include <IRremoteESP8266.h> 
#include <IRsend.h> 
 
#define PanasonicAddress      0x4004      
#define PanasonicPower        0x100BCBD  // Panasonic Power button 

Add your Wi-Fi and MQTT credentials in the next variables:

#define wifi_ssid "YOUR_WIFI_SSID" 
#define wifi_password "YOUR_WIFI_PASS" 
 
#define mqtt_server "YOUR_MQTT_SERVER" 
#define mqtt_user "YOUR_MQTT_USER" 
#define mqtt_password "YOUR_MQTT_PASSWORD" 
#define mqtt_port 1883 

Set the infrared pin to the GPIO12 or D6 on NodeMCU and WeMos boards and the topic on which will receive the messages to turn on/off the TV:

#define IR_PIN 12 
#define ir_topic "/62/ir/command" 
 
void rx_mqtt_callback(char* topic, byte* payload, unsigned int length); 
 
#define DEBUG false 
#define Serial if(DEBUG)Serial 
#define DEBUG_OUTPUT Serial 
 
IRsend irsend(IR_PIN); // IR led is connected to GPIO pin 
WiFiClient espClient; 
PubSubClient client(mqtt_server, mqtt_port, rx_mqtt_callback,espClient); 
 
StaticJsonBuffer<512> jsonDeviceStatus; 
JsonObject& jsondeviceStatus = jsonDeviceStatus.createObject(); 
 
char dev_name[50];  
char json_buffer_status[512]; 
char my_ip_s[16]; 

Start the Wi-Fi and connect to the Wi-Fi network:

void setup_wifi()  
{ 
  delay(10); 
  // We start by connecting to a WiFi network 
  Serial.println(); 
  Serial.print("Connecting to "); 
  Serial.println(wifi_ssid); 
 
  WiFi.mode(WIFI_STA); 
  WiFi.begin(wifi_ssid, wifi_password); 
  while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
  } 
 
  Serial.println(""); 
  Serial.println("WiFi connected"); 
  Serial.println("IP address: "); 
  Serial.println(WiFi.localIP()); 
} 

Connect and reconnect to the MQTT broker. In the event of an error try to reconnect every 5 seconds:

void reconnect() { 
  // Loop until we're reconnected 
  while (!client.connected()) { 
    Serial.print("Attempting MQTT connection..."); 
    if (client.connect(dev_name, mqtt_user, mqtt_password)) { 
      Serial.println("connected"); 
    } else { 
      Serial.print("failed, rc="); 
      Serial.print(client.state()); 
      Serial.println(" try again in 5 seconds"); 
      // Wait 5 seconds before retrying 
      delay(5000); 
    } 
  } 
} 

This is the function that will be called when a message is received from the MQTT broker. Get the message and send the infrared commands to the TV:

void rx_mqtt_callback(char* topic, byte* payload, unsigned int length) 
{ 
  //reserve space for incomming message 
  StaticJsonBuffer<256> jsonRxMqttBuffer; 
  int i = 0; 
  char rxj[256]; 
  Serial.println(dev_name); Serial.print(F("Topic:"));Serial.println(topic); 
  for(i=0;i<length;i++) 
  { 
    rxj[i] = payload[i]; 
  } 
 
  Serial.println(rxj); 
  JsonObject& root = jsonRxMqttBuffer.parseObject(rxj); 
  if (!root.success()) 
  { 
    Serial.println(F("parseObject() failed")); 
    return; 
  } 
 
  const char* device_name  = root["device_name"]; 
  const char* type         = root["type"]; 
  const char* value        = root["value"]; 
 
  Serial.println(device_name);  
  Serial.println(type); Serial.println(value); 
 
  sendIR(); 
 
  return; 
} 

Here we send the message code to the TV. If it doesn't work the first time, try going closer to the TV. It depends on the quality of your IR LEDs, the number of infrared LEDs, and the ambient light:

void sendIR() 
{ 
    int i = 0; 
    Serial.print("sendIR for 2 sec"); 
    for(i=0;i<20; i++) 
    {       
      irsend.sendPanasonic(PanasonicAddress,PanasonicPower); // This should turn your TV on and off 
      delay(100); 
    } 
} 
void setup()  
{ 
  delay(1000); 
  irsend.begin(); 
  Serial.begin(115200);  
  sprintf(dev_name, "ESP_%d", ESP.getChipId()); 
  setup_wifi(); 
  client.setServer(mqtt_server, mqtt_port); 
  client.connect(dev_name, mqtt_user, mqtt_password); 
  client.subscribe(ir_topic); 
  if (!client.connected())  
  { 
    reconnect(); 
  }  
} 
void loop() { 
  client.loop(); 
} 

Now, using the MyMQTT Android application, try to send a command to the ir_topic to start or stop the TV.

As you have probably already noticed I didn't use the WiFiManager to set up the ESP8266, but this is a very good exercise for you to complete the chapter and the project. Study the LIRC library from Linux and try to find other devices that can be controlled with Infrared and the ESP8266.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.98.166