大家好,我是你的好朋友思创斯。网站金沙1005首页:https://ispacesoft.com
我们使用的是paho mqtt软件包,这里介绍一下怎么使用mqtt协议编程。关于鸿蒙系统的mqtt移植好的软件包,相关github链接如下:
https://gitee.com/qidiyun/harmony_mqtt
这里提供一个简单的编程示例:
这里我们使用mqttclient编程模型,他支持多任务多线程,非常适合用在鸿蒙系统上。
1. 网络初始化
这里定义一个 network 结构体,然后指定我们的mqtt服务器的ip和端口号。
network n;
//初始化结构体
networkinit(&n);
//连接到指定的mqtt服务器ip、端口号
networkconnect(&n, “xxx.xxx.xxx.xxx”, xxxx);
2. 设置mqtt缓存和启动mqtt线程
我们这里使用的是mqtt线程功能。
mqttclientinit(&c, &n, 1000, buf, 100, readbuf, 100);
mqttstarttask(&c);
3. 设置mqtt相关参数
接下来我们设置mqtt的相关参数,包括版本号、客户端id、账户密码等
mqttpacket_connectdata data = mqttpacket_connectdata_initializer;
data.willflag = 0;
//mqtt版本为 v3
data.mqttversion = 3;
//设置客户端id
data.clientid.cstring = opts.clientid;
//设置客户端账户
data.username.cstring = opts.username;
//设置客户端密码
data.password.cstring = opts.password;
data.keepaliveinterval = 10;
data.cleansession = 1;
//连接到mqtt服务器
rc = mqttconnect(&c, &data);
4. 订阅主题和接收消息
订阅主题可以使用如下函数
mqttsubscribe(&c, topic, opts.qos, messagearrived);
它的函数原型如下:
dllexport int mqttsubscribe(mqttclient* client, const char* topicfilter, enum qos, messagehandler);
其中:
mqttclient* c :我们前面定义的mqttclient结构体
const char* topicfilter:订阅的主题
messagehandler messagehandler :接收到主题信息后的回调处理函数。
例如上面我们的回调函数是 messagearrived ,它的原型如下:
void messagearrived(messagedata* md)
{
mqttmessage* message = md->message;
//打印接收到的消息的长度、和消息内容
printf(“%.*s”, (int)message->payloadlen, (char*)message->payload);
}
5. 发送消息
发送消息也比较简单,我们只需要设置好我们的主题和消息内容即可
memset(&pubmsg, ‘\0’, sizeof(pubmsg));
//消息内容为 hello harmonyos !
pubmsg.payload = (void*)”hello harmonyos !”;
//消息长度
pubmsg.payloadlen = strlen((char*)pubmsg.payload);
pubmsg.qos = qos0;
pubmsg.retained = 0;
pubmsg.dup = 0;
//推送消息,主题为 pubtest
mqttpublish(&c, “pubtest”, &pubmsg);
完整源码如下:
#include
#include
#include “ohos_init.h”
#include “cmsis_os2.h”
#include
#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, (char*)”106.13.62.194″, 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);
}
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;
}
文章由思创斯整理,转载请注明出处:https://ispacesoft.com/4660.html