Use ESP8266 to access tmall Genie control TV / air conditioner through Blinker platform

Catalog

A simple remote air conditioning controller is made in the front( Check me out. )At present, there is an idle tmall Genie at home, so I want to let the TV set also access voice control. How to connect tmall genie is introduced in detail on Blinker's official website. With the production basis of the simple remote air conditioning controller in front, this routine is very good. Just a few lines of simple code
Remarks:

Demo video

ESP8266 access to tmall Genie control TV

1. Preparation

1.1 principle

----The principle of this experiment is relatively simple, using the example of the third-party library (irremotees8266) of Arduino development software to read the original data of air conditioner / TV and infrared remote control, and save the original data of the infrared key value of the key to be controlled into the array (the length of the original data varies with the infrared protocol).

----Connect ESP8266 to Blinker (lighting technology), use Blinker's app to configure UI interface, then write simple control logic in Arduino development software, send specified infrared instruction or accept the instruction of tmall Genie through the button of app, indirectly achieve the purpose of control.

1.2 hardware used and hardware connection diagram

  • Tmall Fairy (sugar)
  • ESP8266 NodeMCU
  • Infrared transmitting module, infrared receiving module (HX1838)
  • TV receiving infrared control
  • TV set top box
    Note: let's explain here, because the TV at home is connected to tmall's magic box, so if you want to turn on or off the TV, you must turn on / off the TV and tmall's magic box at the same time (so you need to send two infrared instructions, and two infrared emission tubes are used here (not required). One is aimed at the TV infrared receiver, the other is aimed at the tmall Genie infrared receiver).
    The hardware used here is very simple (the infrared receiver is not given). As follows:

1.3 development environment preparation

  • Arduino development environment
    Installation address: Arduino IDE

Note: install version 1.8.7 or later. This software is free to use. Download and install the latest version directly on the official website.

  • Installing extensions for ESP8266
    Method 1: if your computer can access the Internet scientifically, it is very easy to use this method. Enter a website address in the preferences, and then add a library. Otherwise, refer to method 2.
  1. Start Arduino and open the preferences window.
  2. Enter https://arduino.esp8266.com/stable/package/esp8266com/index.json
  3. Open the Boards Manager from the Tools > motherboard menu and install the esp8266 platform (do not forget to select the esp8266 motherboard from the Tools > motherboard menu after installation).

    Method 2: download the packaged ESP 8266 installation package, run it directly and extract it.

Link: https://pan.baidu.com/s/1Eu0qhlP5xu6GgQMdwflfAw extraction code: 3x1f

Note: if you have installed other versions of esp8266sdk, please delete it first, and then use this installation package. After decompression, open the Arduino IDE, and you can find the development board of esp8266 in the menu bar > Tools > development board

  • Install the irremotees8266 Library
    This library contains functions for ESP 8266 to receive infrared decoding and send infrared data. Is a core function of this routine
    Installation method: if you install it directly in the Anduino IDE, you also need to surf the Internet scientifically, otherwise the speed will be very slow.
    Method 1: install directly in IDE

    Method 2: offline installation
    Download the irremotees8266 installation package

Link: https://pan.baidu.com/s/1rlhppts_dnj6mvr9parxlua extraction code: 0n6t

When the download is complete, extract it into the library folder of the project folder

  • Install blinker Arduino Library
    The installation method is the same as that of IRmoteESP8266 library installation method 2.

Link: https://pan.baidu.com/s/1k5tED9HxgiRaqNtKrTJcRg extraction code: 87lw

After installation, it is shown as follows:

Restart Arduino software after successful installation, which can be found in the third-party library of the example.

  • Install blinker APP
  • Android Download: Point me in.
    Or search "blinker" in android store to download and install
  • IOS Download: search app store for "blinker" Download

Summary: after finishing the preparatory work, you can go to the next step. You can use the example of irremotees8266 to decode the switch infrared of the TV.

2. Decode the infrared key value of air conditioner

2.1 upload the ESP 8266 infrared receiving instance to nodemu

  • Example of turning on infrared reception
  • Select the development version used and the COM port connected (according to the actual situation)
  • Connect the data segment of infrared receiving module to GPIO14(Node MCU corresponds to D5 pin)

  • Upload program to Node MCU

2.2 read the infrared key value

  • Open Arduino to debug the serial port monitor
  • The TV remote control is aimed at the infrared receiving tube (here, TCL TV and tmall Genie prevail)
  • Capture infrared key value
    Read two on keys (through serial port printing, it is found that the infrared instructions sent by TV / tmall magic box are the same whether it is on or off).
    The rowData data obtained by printing in the serial port is as follows:
/*TV power supply*/
uint16_t tv_power[207] = {3976, 4024,  500, 2008,  498, 2006,  476, 2032,  476, 2030,  474, 1030,  476, 1030,  500, 2010,  476, 1030,  474, 2032,  478, 1030,  474, 2034,  498, 1006,  474, 1032,  474, 1032,  474, 1032,  474, 1030,  476, 2030,  476, 2032,  474, 1030,  500, 2010,  474, 1032,  474, 2032,  474, 1032,  476, 2028,  476, 8494,  3976, 4026,  500, 2006,  476, 2032,  476, 2030,  476, 2030,  476, 1032,  474, 1032,  476, 2032,  498, 1030,  450, 2034,  474, 1028,  476, 2034,  498, 1008,  498, 1008,  476, 1032,  472, 1032,  474, 1032,  474, 2032,  502, 2008,  474, 1030,  476, 2032,  500, 1030,  452, 2032,  474, 1030,  476, 2026,  498, 8476,  3972, 4024,  476, 2032,  476, 2032,  474, 2034,  472, 2056,  452, 1032,  474, 1056,  450, 2034,  500, 1030,  452, 2056,  450, 1032,  474, 2034,  472, 1056,  450, 1032,  474, 1056,  450, 1034,  472, 1034,  472, 2034,  500, 2006,  474, 1032,  496, 2012,  474, 1056,  448, 2058,  476, 1006,  474, 2028,  476, 8494,  3998, 4000,  474, 2036,  472, 2032,  474, 2056,  474, 2008,  474, 1056,  450, 1056,  448, 2036,  474, 1034,  472, 2036,  472, 1056,  450, 2036,  470, 1034,  472, 1058,  450, 1032,  474, 1032,  498, 1030,  450, 2032,  474, 2034,  498, 1032,  448, 2036,  472, 1058,  450, 2034,  478, 1054,  472, 2008,  472};  // NIKAI D5F2A

/*Tmall Genie power supply*/
uint16_t timo_power[67] = {9010, 4476,  580, 552,  580, 554,  578, 550,  578, 552,  582, 552,  578, 554,  572, 558,  578, 552,  582, 1656,  582, 1656,  582, 1654,  580, 1658,  580, 1658,  580, 554,  578, 552,  578, 1660,  580, 1658,  580, 1656,  580, 1658,  584, 552,  578, 1658,  582, 552,  578, 1656,  582, 554,  576, 554,  580, 552,  578, 554,  580, 1658,  580, 552,  554, 1682,  582, 552,  580, 1658,  560};  // NEC F9EA15


Note: it is saved and needed in the program to be held.

3. Configure the UI of Blinker App

  • Registered account number
    Use your mobile number to sign up.
  • Add device - > WiFi access - > get product key


  • Modify device name and Icon
  • Edit UI interface
    Add two buttons here, one to turn on / off the TV and one to turn on / off the air conditioner.

    Set key properties

    The effect is as follows:

    In this way, a simple control interface is set up.

4. The writing of Arduino program

How to write Arduino program can be described in Blinker website: Check me out.

4.1 control logic

The logic of the control is very simple. When the Node MCU connects to WiFi and displays the online status, press the button on the mobile app. The infrared designation of the program sending instruction achieves the purpose of controlling TV. There is also the control of tmall elves. On that day, the elves receive the instructions, and analyze them in the program, and then send the specified infrared instructions to control the TV. Later, it introduces how to operate in detail.
Note: before operation, connect the IO port connected to the infrared emission module, and gpio4 (D2 pin of Node MCU) is used here

4.2 create a new Arduino program and write control logic

Create a new file - > Save as mirremote? Control

According to the course of accessing tmall Genie given by Blinker official website

Paste the program directly. The program is simple and Annotated:

//Serial printing related
#define BLINKER_PRINT Serial
//Blinker uses wifi access
#define BLINKER_WIFI
//Socket (single) controlled by tmall Genie
#define BLINKER_ALIGENIE_OUTLET
//Infrared receiving parsing header file
#include <IRremoteESP8266.h>
//Infrared sending header file
#include <IRsend.h>
//Blinker related program header file
#include <Blinker.h>

//Here, enter your own key, wifi account password (pay attention to case).
char auth[] = "*****";
char ssid[] = "*****";
char pswd[] = "*****";
//Define pins for infrared emission
const uint16_t kIrLed = 4;  // ESP8266 GPIO pin to use. Recommended: 4 (D2).
//Define GPIO4 pin as infrared emission pin
IRsend irsend(kIrLed);  // Set the GPIO to be used to sending the message.

/*Original infrared data of TV power supply*/
uint16_t tv_power[207] = {3976, 4024,  500, 2008,  498, 2006,  476, 2032,  476, 2030,  474, 1030,  476, 1030,  500, 2010,  476, 1030,  474, 2032,  478, 1030,  474, 2034,  498, 1006,  474, 1032,  474, 1032,  474, 1032,  474, 1030,  476, 2030,  476, 2032,  474, 1030,  500, 2010,  474, 1032,  474, 2032,  474, 1032,  476, 2028,  476, 8494,  3976, 4026,  500, 2006,  476, 2032,  476, 2030,  476, 2030,  476, 1032,  474, 1032,  476, 2032,  498, 1030,  450, 2034,  474, 1028,  476, 2034,  498, 1008,  498, 1008,  476, 1032,  472, 1032,  474, 1032,  474, 2032,  502, 2008,  474, 1030,  476, 2032,  500, 1030,  452, 2032,  474, 1030,  476, 2026,  498, 8476,  3972, 4024,  476, 2032,  476, 2032,  474, 2034,  472, 2056,  452, 1032,  474, 1056,  450, 2034,  500, 1030,  452, 2056,  450, 1032,  474, 2034,  472, 1056,  450, 1032,  474, 1056,  450, 1034,  472, 1034,  472, 2034,  500, 2006,  474, 1032,  496, 2012,  474, 1056,  448, 2058,  476, 1006,  474, 2028,  476, 8494,  3998, 4000,  474, 2036,  472, 2032,  474, 2056,  474, 2008,  474, 1056,  450, 1056,  448, 2036,  474, 1034,  472, 2036,  472, 1056,  450, 2036,  470, 1034,  472, 1058,  450, 1032,  474, 1032,  498, 1030,  450, 2032,  474, 2034,  498, 1032,  448, 2036,  472, 1058,  450, 2034,  478, 1054,  472, 2008,  472};  // NIKAI D5F2A

/*Tmall Genie power source raw data*/
uint16_t timo_power[67] = {9010, 4476,  580, 552,  580, 554,  578, 550,  578, 552,  582, 552,  578, 554,  572, 558,  578, 552,  582, 1656,  582, 1656,  582, 1654,  580, 1658,  580, 1658,  580, 554,  578, 552,  578, 1660,  580, 1658,  580, 1656,  580, 1658,  584, 552,  578, 1658,  582, 552,  578, 1656,  582, 554,  576, 554,  580, 552,  578, 554,  580, 1658,  580, 552,  554, 1682,  582, 552,  580, 1658,  560};  // NEC F9EA15


// New Blinker component object (key on app)
BlinkerButton Button3("btn-pwr2");
BlinkerButton Button4("btn-pwr3");
//Status of tmall Genie socket
bool oState = false;

//Status of tmall Genie power supply
void aligeniePowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {
        digitalWrite(LED_BUILTIN, HIGH);

        BlinkerAliGenie.powerState("off");
        BlinkerAliGenie.print();
        //Turn on the TV and tmall's box
        irsend.sendRaw(tv_power, 207, 38);  // Send a raw data capture at 38kHz.
        Button3.print("TV on");
        irsend.sendRaw(timo_power, 67, 38);  // Send a raw data capture at 38kHz.
        Button4.print("Tmall's box opens");
        oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
        digitalWrite(LED_BUILTIN, LOW);

        BlinkerAliGenie.powerState("on");
        BlinkerAliGenie.print();
        
        //Turn off TV and tmall's box
        irsend.sendRaw(tv_power, 207, 38);  // Send a raw data capture at 38kHz.
        Button3.print("TV off");
        irsend.sendRaw(timo_power, 67, 38);  // Send a raw data capture at 38kHz.
        Button4.print("Tmall's box closed");
        oState = false;
    }
}
//Tmall status query
void aligenieQuery(int32_t queryCode)
{
    BLINKER_LOG("AliGenie Query codes: ", queryCode);

    switch (queryCode)
    {
        case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("AliGenie Query All");
            BlinkerAliGenie.powerState(oState ? "on" : "off");
            BlinkerAliGenie.print();
            break;
        case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("AliGenie Query Power State");
            BlinkerAliGenie.powerState(oState ? "on" : "off");
            BlinkerAliGenie.print();
            break;
        default :
            BlinkerAliGenie.powerState(oState ? "on" : "off");
            BlinkerAliGenie.print();
            break;
    }
}

void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);

    Blinker.vibrate();

    uint32_t BlinkerTime = millis();
    Blinker.print(BlinkerTime);
    Blinker.print("millis", BlinkerTime);
}

void setup()
{
    Serial.begin(115200);

    #if defined(BLINKER_PRINT)
        BLINKER_DEBUG.stream(BLINKER_PRINT);
    #endif

    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);

    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);
    //Register callback function/
    Button3.attach(Button3_callback);
    Button4.attach(Button4_callback);

    //Infrared initialization
    irsend.begin();
    
    BlinkerAliGenie.attachPowerState(aligeniePowerState);
    BlinkerAliGenie.attachQuery(aligenieQuery);
    //Register heartbeat package
    Blinker.attachHeartbeat(heartbeat);
}

void loop()
{
    Blinker.run();
}

//Heartbeat packet callback function
void heartbeat()
{
  if (oState){
      Button3.print("on");
   } 
  else {
    BUILTIN_SWITCH.print("off");
  }
}

//Callback function
void Button3_callback(const String &state)
{
  BLINKER_LOG("get button state: ", state);
  //BTN pwroff button press detected
  if (state == BLINKER_CMD_BUTTON_TAP)
  {
   
    Button2.print("TV power down");
     //Serial port printing
    BLINKER_LOG("TV power supply", "Press down");
    //open
    irsend.sendRaw(tv_power, 207, 38);  // Send a raw data capture at 38kHz.
  }

}
//Callback function
void Button4_callback(const String &state)
{
  BLINKER_LOG("get button state: ", state);
  //BTN pwroff button press detected
  if (state == BLINKER_CMD_BUTTON_TAP)
  {
   
    Button2.print("Tmall box press");
     //Serial port printing
    BLINKER_LOG("Tmall magic box power supply", "Press down");
     //open
    irsend.sendRaw(timo_power, 67, 38);  // Send a raw data capture at 38kHz.
  }

}

Matters needing attention

1. Change the device key and wifi account password to your own

2. Change the rowDate array of the read infrared key value to the infrared key value array that you need to control (the array name can be arbitrary, pay attention to the data length when sending, here is 199).
3. Use the function irsend.sendRaw(power_off, 199, 38) to send the infrared command, which means to send the power_off array at the frequency of 38kHz. The array length is 199

4.3 upload program to ESP8266


After the program is uploaded successfully, the Blinker App can be used for operation after the Node MCU is reset. (pay attention to the connection of the infrared emission module, the pin is GPIO4(D2)).

5.App control

This routine is mainly to verify the control mode of tmall genie. Here is also a simple app control mode

  • Open Blinker App
    If the device key and Wifi account password in the program are correct, you can see that the device status is online in the app at this time.

  • Operation equipment
    Click the TV switch button: if there is a callback message, it indicates that the infrared command is sent successfully, and you can see the TV on / off.

    TV control can be realized through this app. At that time, this is not the ultimate goal. It needs to be connected to tmall genie.

6. Tmall control


Step details:

After returning to the device interface, you can see the Blinker device that has been bound.

At this time, if your tmall genie is connected to the Internet normally, say to the tmall Genie:

Me: tmall genie, open the socket in the living room. Tmall Genie: the socket has been turned on. Pay attention to the operation.
Me: tmall genie, turn off the socket in the living room. Tmall: the living room socket has been closed for you.

Eh, but the TV we control, it seems a little low to call the socket. OK, you can alias the device in the tmall Genie app. When we call the alias of tmall genie, you can also operate the device.
I renamed this device TV (I also want to name it TV, but it should be an official restriction or a bug)

  • Alias modification

    After modifying the alias, you can say to tmall: tmall, turn on the TV. At this time, tmall will help you turn on the TV.

summary

This routine is over here. In fact, it's just a good example. With a small example, you can expand a lot of things, such as the air conditioning control, or any place that needs infrared control. Because the irremotees8266 infrared decoding example can support the analysis of many kinds of infrared protocols, with this example, we do not need to care about the protocols of different infrared signals, just read the original data of the infrared key value, and then send out the same infrared data.

95 original articles published, 300 praised, 210000 visitors+
Private letter follow

Tags: socket Android Mobile JSON

Posted on Sat, 14 Mar 2020 00:37:44 -0400 by ud2008