hwcoap-a component implementation of the Coap protocol that takes up very little resources to connect to the Huawei IoT platform


The previous blog introduced the process of Huawei IoT platform coap protocol docking . This blog introduces the code implementation and introduces a code implementation of using coap to dock Huawei IoT.
Why do I like coap so much. I think the MQTT protocol is still too big after all. It is still very resource consuming to run on some IoT terminals, and most IoT platforms require MQTT to be online in real time, which is a big waste of power consumption and traffic. . In many places, due to poor NB signals, only 4G and cat1 networks can be used, but 4G and cat1 are now recommended to use MQTT docking platform, which is difficult to be compatible with previous NB devices; for both, running SDK on MCU takes up resources too much Yes, like some STM32F103C8T6 is difficult to run. In this case, hwcoap comes.

hwcoap——A simple coap component for connecting to Huawei’s IoT platform.

A very, very simple implementation of the coap component of the Huawei IoT platform. The user only needs to implement the UDP transceiver function and a delay function.
The author analyzes the process of Huawei IoT COAP protocol docking through wireshark packet capture. The code only implements the docking platform function simply in the form of UDP packets. It strives to be simple and slightly rough, but it is also enough for general scenarios. As long as your platform supports udp communication, you can use this code to communicate with Huawei IoT. There is no need to use the complicated and space-consuming MQTT protocol.
The sending and picking up buffer space used in the code operation is first applied for by the user, and you can create a data according to your own situation. I tested using only 1K RAM, and the sending and receiving buffers were 512B respectively. If the amount of data sent and received is not large, the RAM can also be set to 256B. If the sending and receiving are in the same thread, the sending and receiving Buffer can be set to the same to save RAM.
The code can be downloaded on github and gitee:
github address : https://github.com/llb126yx/hwcoap
code cloud address : https://gitee.com/libolian/hwcoap

Instructions for use

File description

  • hwcoap.c code implementation
  • hwcoap.h header file, included in the user program
  • test.c A test program implemented under linux
  • testapplication The application compiled on the Raspberry Pi


  1. Add hwcoap.c to the user project, and include the header file hwcoap.h in the application.
  2. The three interfaces defined in hwcoap.h are implemented in the user program: the udp transceiver function and a delay function.
    brief: send data through udp.
    paras: data: data to be sent
            len: data length to be sent
    return: 0-success , other -failed
    uint8_t UDP_Send(uint8_t *data,uint16_t len);
    brief: receive data through udp.
    paras: data,receive buffer
            maxLen, max length of receiving buff.
    return: receiving data length. 
    uint16_t UDP_Receive(uint8_t *data,uint16_t maxLen);
    brief: delay some time. unit:ms
    void  DelayMs(uint16_t ms);
  3. The user program calls cHWRegisterWithCoap(char *ep,uint8_t epLen)to initiate registration to the platform, and needs to pass in the device IMEI (you need to create a profile on the IoT platform and add the device) and the length of the IMEI; report data to the platform
    through the cHWReportData(uint8_t *data,uint16_t len)interface; and receive the data sent
    by the cuint16_t HWProcessRxData(uint8_t *data,uint16_t maxLen)platform.

Demo use

test.c is a test program implemented under linux. The testapp application can be generated by executing make in the linux environment:

./testapp 868681049496159

Among them, 868681049496159 is the identification number (IMEI) of the device I registered on the platform, which needs to be modified according to my own device.

The running effect of testapplication compiled on the Raspberry Pi is as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/llb19900510/article/details/108665713