2021 Aizhi Pioneer - infrared learning module (air conditioner remote control)

[this article is participating in the "2021 Aizhi Pioneer - Essay Contest"] https://mp.weixin.qq.com/s/I2s99dZpbP0QpMiKFneYJA

preface

Two sensors were made before 2021 Aizhi Pioneer - human presence sensor - CSDN community and DIY smart home from scratch - intelligent light sensor based on ESP32_ Inspired desktop blog - CSDN blog As a result, control the light? That's too wasteful. This time I'm going to forget to close the door. I'm second in frequency! The air conditioner that wastes the first place in electricity will be rectified, so as not to go home and find that the air conditioner is not turned off, it will burst even more!

However, my air conditioner switch is the same button, and I can't directly obtain the air conditioner status. The air conditioner is too expensive, and I dare not dismantle and transform it (seconds)/ ω\))

So I used what I did before Infrared temperature measurement gun, super evolution! – intelligent temperature sensor! Intelligent temperature measurement module based on arduino inspired desktop blog - CSDN blog Judge whether I turn off the air conditioner by the temperature.

I've calculated that my broken air conditioner costs more than one yuan an hour, and it's enough for me to eat a meal a day. For a poor man like me, this is unacceptable! • ́ へ• ́ ╬)

But now this kind of manual switch is also stupid. Then write an intelligent control application and try automatic control. Of course, I don't mind if the boss has time to help me write a more NB one ω・` )

Hardware selection

First, the edge computer of Yihui Spirit 1 Edge computer, this environment is based on this thing.

And the ancestral anxinko ESP32S Anxin can see if you can give me some sponsorship? It's OK to reimburse me for the board money. You see, Aizhi gave me this opportunity!

Infrared learning module It's a gadget bought on Taobao for 20. You can learn and store 16 key values, but I used one.

The pins are controlled by UART (p16 TX, p17 Rx, 3V3-3V3, GND-GND)

Attention!: this infrared diode must be placed flat, or it will be blocked by the nearby receiving module and affect the signal angle. I didn't pay attention to it during welding, but it was hard broken at the back.


Code parsing

Get code

In order to facilitate the explanation of logic, I will disrupt the order of the code and may cut it. If you want to run directly with the code, you can go directly The secret treasure house of inspiration desktop Get the code, or directly clone:

https://gitee.com/inspiration-desktop/DEV-lib-arduino.git

In fact, friends who have read my previous articles should have found that it is basically my SDK template. Friends who have read my previous articles can directly see the device control commands and device control processes.

If you are not very clear about the SDK development I wrote, you can continue to read it.

After downloading or clone code, these three folders will be used this time:

Cjson: the cjson library I transplanted is the standard cjson library. Put it in the libraries folder under the arduino installation directory and Baidu can use the cjson functions.

libsddc: I transplanted it from the official SDDC library and the SDK I wrote, and just put it in the libraries folder. There are SDDC protocol processing functions, which we don't care about.

In the demo folder are the demo codes of our various sensors:

In the infrared_learning_sddc_sdk_demo folder in the red circle is our code. Click in to see the infrared_learning_sddc_sdk_demo.ino file. Double clicking the file will automatically start Arduino ide to open the code. Select the corresponding COM port in tools - > port, and then click upload to burn the code into the board:

For the specific arduino tutorial, see my previous articles arduino Development Guide and arduino development: the first application based on ESP32S - infrared temperature measuring gun (with pin diagram)

Via Spirit 1's application or Sniffer Commands sent to sensor devices:

{
  "method": "set",
  "air_conditioning_switch": "ON"       // Because the on and off of my air conditioner remote control are the same key, one command controls the on and off. Judge whether the air conditioner is on or off through the previous temperature sensor
}

{
  "method": "set",
  "air_conditioning_switch": "study"    // Let the sensor activate the learning function
}

Device and protocol initialization process:

It is written based on the official demo and does not need to be modified. It is mainly about device initialization, pin configuration, and protocol initialization.

/*
 * Initialize sensor
 */
void sensor_init()
{
    // The sensor is initialized using a serial port
    Serial2.begin(9600);
    Serial2.setDebugOutput(true);
    Serial2.println();
​
}
​
void setup() {
    byte mac[6];
    Serial.begin(115200);
    Serial.setDebugOutput(true);
    Serial.println();
​
    // Initialize the control module
    sensor_init();
    
    // Clear the state of the key state machine
    button.reset();
  
    // Create a key scanning thread, long press the IO0 key, and the ESP32 will enter the SmartConfig mode after releasing it
    sddc_printf("Press and hold the key to enter Smartconfig...\n");
    button.attachLongPressStop(esp_io0_key_task);
    xTaskCreate(esp_tick_task, "button_tick", ESP_TASK_STACK_SIZE, NULL, ESP_TASK_PRIO, NULL);
​
    // Start WiFi and connect to the network
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) 
    {
        delay(500);
        Serial.print(".");
    }
  
    // Get and print IP address
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.print("'ip :");
    Serial.print(WiFi.localIP());
    Serial.println("' to connect");
​
​
    // sddc protocol initialization
    sddc_lib_main(&sys_cfg);
​
    // Obtain and print the mac address of the network card
    WiFi.macAddress(mac);
    sddc_printf("MAC addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
              mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
    // Use the network card mac address to set the device unique ID UID
    sddc_set_uid(G_sddc, mac);
}
​
void loop() {
    // Run SDDC protocol cycle
    while (1) 
    {
        sddc_printf("SDDC running...\n");
        sddc_run(G_sddc);
        sddc_printf("SDDC quit!\n");
    }
​
    // Destroy SDDC protocol
    sddc_destroy(G_sddc);
}

Configure device information

This part of the code can configure the WiFi name and WiFi password, the pin to be used, and configure the information displayed by the device on Spirit 1:

#Define sddc_cfg_port 680u / / port number used by SDDC protocol
#Define pin_input 0 / / select IO0 to control
#define ESP_TASK_STACK_SIZE           4096
#define ESP_TASK_PRIO                 25
​
static const char* ssid = "EOS-Tenda";             // WiFi name
static const char* password = "1234567890";        // WiFi password
​
/*
 *  Information definition of the current device
 */
DEV_INFO    dev_info = {
            .name     = "Air conditioning control switch",
            .type     = "device.hwyk",
            .excl     = SDDC_FALSE,
            .desc     = "ESP-32S",
            .model    = "IDHWYK01B",
            .vendor   = "inspiration-desktop",
};
​
/*
 *   System registration object aggregation
 */
SDDC_CONFIG_INFO sys_cfg = {
        .token             = "1234567890",            // Device password
        .devinfo           = &dev_info,               
        .io_dev_reg        = io_dev,
        .io_dev_reg_num    = ARRAY_SIZE(io_dev),
        .num_dev_reg       = num_dev,
        .num_dev_reg_num   = ARRAY_SIZE(num_dev),
        .state_get_reg     = dev_state_get_reg,
        .state_get_reg_num = ARRAY_SIZE(dev_state_get_reg),
        .dis_dev_reg       = dis_dev,
        .dis_dev_num       = ARRAY_SIZE(dis_dev),
};

Callback function registration
This is the location where the callback function is registered after receiving the command. Only the functions registered here can be called correctly by the SDK and perform correct actions.

For specific SDK analysis, please refer to sddc_sdk_lib analysis of SDK framework based on sddc protocol and The official series of forcing people to death! Data analysis from DDC sniffer to sddc_sdk_lib

/* 
 *  Registration of object function and processing method of digital quantity equipment
 */
NUM_DEV_REGINFO num_dev[] = {
//        {"set_num_demo", demo}, / / string is the input command, and demo is the command processing function
};
​
/*
 *  Display device object function and processing method registration
 */
DIS_DEV_REGINFO dis_dev[] = {
//        {"set_dis_demo", demo}, / / string is the input command, and demo is the command processing function
};
​
/*
 * IO Device object setting function and processing method registration
 */
IO_DEV_REGINFO io_dev[] = {
        {"air_conditioning_switch",air_conditioning_switch_set},
//        {"set_io_demo", demo}, / / string is the input command, and demo is the command processing function
};
​
/*
 *  System object status get registration
 */
DEV_STATE_GET  dev_state_get_reg[] = {
//        {"demo", dev_num_type, num_get_demo}, / / demo is the input command, and the string is the command processing function
//        {"demo",   DEV_IO_TYPE,  io_get_demo},
//        {"demo", DEV_DISPLAY_TYPE, dis_get_demo},
};

Equipment control process
Here is our own processing flow, which can be changed according to your needs. After receiving set or get, enter the corresponding processing function according to the previously registered function. The device will detect the sensor output, and then regularly report the light intensity data according to the set reporting interval. It can also actively send the get command to actively query the current data of the sensor:

static unsigned char command_buf[] = {0X55, 0XAA, 0X03, 0X00, 0X02};    // dispatch orders
static unsigned char study_buf[] = {0X55, 0XAA, 0X02, 0X00, 0X01};      // Learning command
​
​
/* 
 *  Infrared learning module control function
 */
sddc_bool_t air_conditioning_switch_set(const char* value) {
    if(strcmp(value,"study"))
    {
        Serial2.write(study_buf, 5);
        return SDDC_TRUE;
    } else 
    {
        Serial2.write(command_buf, 5);
        return SDDC_TRUE;
    }
}

summary

OK! So far, the temperature sensor, light sensor, human presence sensor, remote control lamp and remote control air conditioner have been built! Next, we'll integrate these applications through Aizhi! A super energy-saving and consumption reduction scenario with super cow force and super money saving is completed!!!

Tags: IoT arduino

Posted on Sun, 21 Nov 2021 20:34:49 -0500 by natefanaro