Generally speaking, an iot product should include devices, cloud platforms and mobile apps. I will transplant MQTT protocol and OneNET access protocol on hongmeng system, so that both mobile APP and web page can access data of development board remotely (cross network, not LAN) and control the function of development board.

Theoretically, any MQTT-based iot cloud platform can support access.

Phomqtt and Onenet packages are available for download, stating that all source code complies with the open source license ~~.

Harmony_mqtt code repository for The Hongmeng system:

Gitee.com/qidiyun/har…

Onenet access Package repository supporting Hongmeng System:

Gitee.com/qidiyun/har…

3.11.1 effect

First take a look at the effect. I use OneNET iot cloud platform. After entering the application, you can see the following webpage interface. The temperature and humidity data of this webpage are uploaded by Hi3861, and there is a switch button which can control the LED light of the development board.

There’s also a mobile APP,

The above interface is relatively simple, but it does not hinder our use. The main reason for choosing OneNET cloud platform is that the access method is relatively simple and convenient, and it is easy to learn. Another reason is that OneNET provides the Cloud platform of Internet of Things and mobile phone APP, so we do not need to realize it by ourselves, so we can pay more attention to the development of Hongmeng system.

When we press the switch button, we can see the following information printed on the development board:

The key is “ledSwitch”. When the value is 1, you can see that the LED light of the development board is on; when the value is 0, the LED light of the development board is off.

3.11.2 package

I have released MQTT and Onenet in the form of software packages, which are respectively

(1) ONENET — realize onenET access capability

(2) PahomQTT — realize MQTT protocol function

Simply place the two packages in the third_party folder. Then change

Code -1.0\vendor\hisi\hi3861\hi3861\ build. gn file, add pahomqtt and onenet to compile

Let’s look at the onenet folder:

Where onenet.h is the header file

Onenet_mqtt. c is the full source code, it is based on PAHO MQTT MQTTClient programming model.

In addition, under the samples folder is a sample code, which is as follows:

#include <stdio.h> #include <unistd.h> #include “MQTTClient.h” #include “onenet.h”

#define ONENET_INFO_DEVID “597952816” #define ONENET_INFO_AUTH “202005160951” #define ONENET_INFO_APIKEY “zgQdlB5y3Bi9pNd2bUYmS8TJHIY=” #define ONENET_INFO_PROID “345377” #define ONENET_MASTER_APIKEY “gwaK2wJT5wgnSbJYz67CVRGvwkI=”

extern int rand(void);

void onenet_cmd_rsp_cb(uint8_t *recv_data, size_t recv_size, uint8_t **resp_data, size_t *resp_size) { printf(“recv data is %.*s\n”, recv_size, recv_data);

*resp_data = NULL;
*resp_size = 0;
Copy the code

}

int mqtt_test(void) {

device_info_init(ONENET_INFO_DEVID, ONENET_INFO_PROID, ONENET_INFO_AUTH, ONENET_INFO_APIKEY, ONENET_MASTER_APIKEY);
onenet_mqtt_init();

onenet_set_cmd_rsp_cb(onenet_cmd_rsp_cb);

while (1)
{
	int value = 0;
	
    value = rand() % 100;

    if (onenet_mqtt_upload_digit("temperature", value) < 0)
    {
        printf("upload has an error, stop uploading");
        //break;
    }
    else
    {
        printf("buffer : {\"temperature\":%d} \r\n", value);
    }
    sleep(1);
}
return 0;
Copy the code

} the phone APP download: open. Iot. 10086. Cn/doc/art656….

Due to the large content of this section, it will be divided into several articles and released in succession. The current plan is as follows:

(1) PaHO MQTT client transplantation. In fact, I have a previous article on the migration of PAHO MQTT, but that article was a simple migration and did not support multitasking, this MQTT migration will support multitasking.

(2) Transplantation and implementation of ONenet protocol. It mainly describes how to realize onenet access based on MQTT.

(3) How to use onenet cloud platform

(4) How to access the equipment (Hongmeng development board) to Onenet to realize data exchange.