Play OpenHarmony smart home: How to realize Raspberry Pi "touch" device control

Table of contents

1. Introduction

2. "Touch" device control scene

3. Realization of NFC pull-up application function

4. Realization of application functions of small LED lights

V. Summary

reference address


1. Introduction

"One touch" device control, relying on the NFC short-distance communication protocol, connects OpenAtom OpenHarmony (referred to as "OpenHarmony") standard system equipment and all-scenario equipment through the interactive method of touch, solving the connection between applications and devices The problem of slow and difficult transmission is achieved, and the application can be launched with one touch, bringing users a smooth experience of seamless switching.

2. "Touch" device control scene

(1) Use effect

When the Raspberry Pi development board is close to the Bear Pi development board, the application in the Raspberry Pi is pulled up and enters the LED light control interface. Then, the control end and the device end complete the distribution network communication connection, click to turn on the small LED light of the Bear Pie to light up, and click to turn off the small LED light to turn off.

(2) Operation process

• Write the application information of the control terminal in the NFC passive patch of Bear Pi

• Put the PN532 active board of the Raspberry Pi close to the Bear Pi NFC patch, read the application information in the patch, and pull up the corresponding small light control application

• The control end and the device end complete the distribution network communication connection

• Click to turn on the LED light of the Bear Pie, and click to turn off the LED light to turn off

3. Realization of NFC pull-up application function

Near Field Communication (NFC for short) is an emerging technology. Devices using NFC technology can exchange data when they are close to each other. It is evolved from non-contact radio frequency identification (RFID). NFC technology is widely used in real scenarios. By integrating inductive card readers, inductive cards and point-to-point communication functions on a single chip, mobile terminals are used to realize mobile payment, access control, mobile identification, anti-counterfeiting and other applications. The point-to-point mode of NFC requires two modules, the active board and the passive board. In this case, the PN532 module of the Raspberry Pi is the active board for reading information, and the NFC patch of the Bear Pi development board is the passive board for storing information.

int main(int argc, char **argv) {
        
        
    uint8_t buff[255];
    uint8_t uid[MIFARE_UID_MAX_LENGTH];
    int32_t uid_len = 0;
    PN532 pn532;
    PN532_I2C_Init(&pn532);
    if (PN532_GetFirmwareVersion(&pn532, buff) == PN532_STATUS_ERROR) {
        
        
        return -1;
    }
    PN532_SamConfiguration(&pn532);
    while (1) {
        
        
        while (1) {
        
        
            // 判断NFC模块是否靠近
            if (PN532_ReadPassiveTarget(&pn532, uid, PN532_MIFARE_ISO14443A, 1000) != PN532_STATUS_ERROR) {
        
        
                break;
            }
            usleep(100);
        }

        HILOGI("开始调起应用\r\n");
        pthread_t id1;
        int ret = pthread_create(&id1, NULL, (void *)mythread1, NULL);
        if (ret) {
        
        
            HILOGE("创建线程失败\r\n");
        }

        sleep(4);
    }
}
/*
 *拉起设备控制应用的线程
*/
void *mythread1(void) {
        
        
    char arg[500] = "aa start -d 1 -a com.huawei.ohos_car_controller.default -b ohos.samples.jshelloworld";
    system(arg);
    return NULL;
}

In the above code, the implementation of the NFC function needs to call the corresponding interface function in the driver file. The I2C_Init function implements the initialization of the Raspberry Pi PN532 module. The PN532_ReadPassiveTarget function is used to read the UID information of the NFC patch cyclically. When the information of the NFC patch of the Bear Pi is read, a new thread is created to launch the corresponding LED light application.

4. Realization of application functions of small LED lights

The application of LED lights in this case is mainly based on the TCP communication protocol to realize the on and off control of the bear pie LED lights.

import led_controller from '@ohos.led_controller';
export default {
        
        
    onShow() {
        
        
        this.tcpConnect();
    },
    onDestroy() {
        
        
        this.tcpDistroy();
    },
    tcpConnect() {
        
        
        let promise_connect = led_controller.Connect();
        promise_connect.then((results) => {
        
        
            setTimeout(this.changeText(), 9000);
        }).catch(err => {
        
        
            console.log('[led Controller]' + err)
        })
    },
    tcpSend(message) {
        
        
        let promise_send = led_controller.Send({
        
        
            data: message
        })
        promise_send.then((results) => {
        
        
            if (results.send_status == 1) {
        
        
                console.log("[led Controller] send success")
            }
        }).catch(err => {
        
        
            console.log("[led Controller]" + err)
        })
    },
    tcpDistroy() {
        
        
        let promise_disconnect = led_controller.Close()
        promise_disconnect.then((results) => {
        
        
            if (results.close_status == 1) {
        
        
                ConnectionStatus = 0
                prompt.showToast({
        
        
                    message: "网络断开",
                });
            }
        }).catch(err => {
        
        
            console.log("[led Controller]" + err)
        })
    },
    ledOpen() {
        
        
       this.tcpSend("1")
    },
    changeText() {
        
        
        prompt.showToast({
        
        
            message: "配网成功",
        });
    },
    ledClose() {
        
        
        this.tcpSend("0")
    }
}

In the above code, the NAPI dynamic library is imported through import led_controller from '@ohos.led_controller', and the interface functions related to TCP communication are encapsulated in the led_controller dynamic library.

V. Summary

This article describes how to implement OpenHarmony "touch" device control using NFC. First of all, it is necessary to develop NFC intelligent sensing applications based on the Raspberry Pi PN532 module driver to realize the function of touching and discovering between devices. Then, develop the TCP communication service between devices based on NAPI, and call the interface function at the application layer to realize the switching on and off of the LED light. In addition to the samples shared in this article, developers can also implement more fun and high-performance samples by expanding other related attributes and methods.

Code address:

led-controller-device: device: device-side application code in the NFC framework

led-controller-application: application: application code of the control terminal in the NFC framework

reference address

Device source code

led-controller-device: device: device-side application code in the NFC framework

Application source code

led-controller-application: application: application code of the control terminal in the NFC framework

Knowledge System

knowledge: Co-construct learning paths, development samples, third-party libraries, in-depth articles, live courses, material expansion and other content for the OpenHarmony community, and present them to developers on the official website of OpenHarmony, helping developers quickly find learning resources and jointly prospering the OpenHarmony community.

Guess you like

Origin blog.csdn.net/OpenHarmony_dev/article/details/128453555
Recommended