We are using the PAHO MQTT package. Here is how to program using the MQTT protocol. For the MQTT ported package of Hongmeng system, the relevant Github link is as follows:

Gitee.com/qidiyun/har…

Here is a simple programming example:

Here we use MQTTClient programming model, he supports multi-task multithreading, very suitable for hongmeng system.

  1. Network Initialization

Here we define a Network structure and then specify the IP and port number of our MQTT server.

Network n; // Initialize the struct NetworkInit(&n); // Connect to the specified MQTT server IP, port number NetworkConnect(&n, “xxx.XXX.xxx.xxx”, XXXX);

  1. Set up the MQTT cache and start the MQTT thread

We use the MQTT threading capability here.

MQTTClientInit(&c, &n, 1000, buf, 100, readbuf, 100); MQTTStartTask(&c);

  1. Set MQTT parameters

Next we set the parameters for the MQTT, including the version number, client ID, account password, and so on

MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.willFlag = 0; // the MQTT version is v3 data.mqttversion = 3; // set the clientID data.clientid. cstring = opts.clientid; // Set the client account data.username. Cstring = opts.username; // Set the client password data.password.cstring = opts.password; data.keepAliveInterval = 10; data.cleansession = 1;

// Connect to MQTT server rc = MQTTConnect(&c, &data);

  1. Subscribe to topics and receive messages

You can subscribe to a topic using the following functions

MQTTSubscribe(&c, topic, opts.qos, messageArrived); Its function prototype is as follows:

DLLExport int MQTTSubscribe(MQTTClient* client, const char* topicFilter, enum QoS, messageHandler); Among them:

MQTTClient* C: The MQTTClient structure we defined earlier

Const char* topicFilter: The topic to subscribe to

MessageHandler messageHandler: A callback handler that receives subject information.

For example, our callback above is messageArrived, which has the following prototype:

void messageArrived(MessageData* md) { MQTTMessage* message = md->message; Printf (“%.s”, (int)message->payloadlen, (char)message->payload); }

  1. Send a message

Sending messages is also relatively easy, we just need to set up our subject and message content

memset(&pubmsg, ‘\0’, sizeof(pubmsg)); // The message is hello harmonyOS! pubmsg.payload = (void*)”hello harmonyOS !” ; // Message length pubmsg.payloadlen = strlen((char*)pubmsg.payload); pubmsg.qos = QOS0; pubmsg.retained = 0; pubmsg.dup = 0;

MQTTPublish(&c, “pubtest”, &pubmsg);

The complete source code is as follows:

#include <stdio.h>

#include <unistd.h>

#include “ohos_init.h” #include “cmsis_os2.h”

#include <unistd.h> #include “hi_wifi_api.h” //#include “wifi_sta.h” #include “lwip/ip_addr.h” #include “lwip/netifapi.h”

#include “lwip/sockets.h”

#include “MQTTClient.h”

/ * *

  • MQTT URI farmat:
  • domain mode
  • tcp://iot.eclipse.org:1883
  • ipv4 mode
  • TCP: / / 192.168.10.1:1883
  • SSL: / / 192.168.10.1:1884
  • ipv6 mode
  • tcp://[fe80::20c:29ff:fe9a:a07e]:1883
  • ssl://[fe80::20c:29ff:fe9a:a07e]:1884

* / # define MQTT_URI “TCP: / / 106.13.62.194:1883”

struct opts_struct { char* clientid; int nodelimiter; char* delimiter; enum QoS qos; char* username; char* password; char* host; int port; int showtopics; } opts = {(char *) “stdout – subscriber”, 0, (char *) “\ n”, QOS2, NULL, NULL, “106.13.62.194” (char *), 1883, 1};

void messageArrived(MessageData* md) { MQTTMessage* message = md->message;

if (opts.showtopics)
	printf("%.*s\t", md->topicName->lenstring.len, md->topicName->lenstring.data);
if (opts.nodelimiter)
	printf("%.*s", (int)message->payloadlen, (char*)message->payload);
else
	printf("%.*s%s", (int)message->payloadlen, (char*)message->payload, opts.delimiter);
//fflush(stdout);
Copy the code

}

unsigned char buf[100]; unsigned char readbuf[100];

int mqtt_test(void) { int rc = 0;

MQTTMessage pubmsg; char* topic = "test"; if (strchr(topic, '#') || strchr(topic, '+')) opts.showtopics = 1; if (opts.showtopics) printf("topic is %s\n", topic); Network n; MQTTClient c; NetworkInit(&n); NetworkConnect(&n, opts.host, opts.port); MQTTClientInit(&c, &n, 1000, buf, 100, readbuf, 100); MQTTStartTask(&c); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.willFlag = 0; data.MQTTVersion = 3; data.clientID.cstring = opts.clientid; data.username.cstring = opts.username; data.password.cstring = opts.password; data.keepAliveInterval = 10; data.cleansession = 1; printf("Connecting to %s %d\n", opts.host, opts.port); rc = MQTTConnect(&c, &data); printf("Connected %d\n", rc); printf("Subscribing to %s\n", topic); rc = MQTTSubscribe(&c, topic, opts.qos, messageArrived); printf("Subscribed %d\n", rc); memset(&pubmsg, '\0', sizeof(pubmsg)); pubmsg.payload = (void*)"hello harmonyOS !" ; pubmsg.payloadlen = strlen((char*)pubmsg.payload); pubmsg.qos = QOS0; pubmsg.retained = 0; pubmsg.dup = 0; while (1) { MQTTPublish(&c, "pubtest", &pubmsg); sleep(1); } printf("Stopping\n"); MQTTDisconnect(&c); NetworkDisconnect(&n); return 0;Copy the code

}