点击蓝字 ╳ 关注我们
王维平
OpenHarmony知识体系工作组
什么是MQTT
MQTT的基本原理
MQTT客户端开发流程
//订阅的回调函数
void messageArrived(MessageData *data)
{
printf("Message arrived on topic %.*s: %.*s
", data->topicName->lenstring.len,
data->topicName->lenstring.data, data->message->payloadlen, data->message->payload);
}
//主流程函数
static void MQTTDemoTask(void)
{
WifiConnect("BearPi", "123456789");
printf("Starting ...
");
int rc, count = 0;
MQTTClient client;
NetworkInit(&network);
printf("NetworkConnect ...
");
NetworkConnect(&network, MQTT_SERVERIP, MQTT_SERVERPORT);//本地电脑作为消息代理 此处为电脑IP
printf("MQTTClientInit ...
");
//1-------------创建客户端对象
MQTTClientInit(&client, &network, MQTT_CMD_TIMEOUT_MS, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));
MQTTString clientId = MQTTString_initializer;
clientId.cstring = "bearpi";
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.clientID = clientId;
data.willFlag = 0;
data.MQTTVersion = MQTT_VERSION;
data.keepAliveInterval = MQTT_KEEP_ALIVE_MS;
data.cleansession = 1;
printf("MQTTConnect ...
");
//2-------------连接服务端
rc = MQTTConnect(&client, &data);
if (rc != 0) {
printf("MQTTConnect: %d
", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
osDelay(MQTT_DELAY_2S);
}
printf("MQTTSubscribe ...
");
//3-------------订阅主题substopic
rc = MQTTSubscribe(&client, "substopic", MQTT_QOS, messageArrived);
if (rc != 0) {
printf("MQTTSubscribe: %d
", rc);
osDelay(MQTT_DELAY_2S);
}
while (++count) {
MQTTMessage message;
char payload[30];
message.qos = MQTT_QOS;
message.retained = 0;
message.payload = payload;
(void)sprintf_s(payload, sizeof(payload), "message number %d", count);
message.payloadlen = strlen(payload);
//4------------发布pubtopic主题
if ((rc = MQTTPublish(&client, "pubtopic", &message)) != 0) {
printf("Return code from MQTT publish is %d
", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
}
osDelay(MQTT_DELAY_500_MS);
}
}
MQTT实操验证
NetworkConnect(&network, 192.168.120.137, 1883);//本地电脑作为消息代理 此处为电脑IP Mosquitto相匹配
printf("Starting ...
");
NetworkInit(&network);
printf("NetworkConnect ...
");
NetworkConnect(&network, MQTT_SERVERIP, MQTT_SERVERPORT);//本地电脑作
printf("MQTTClientInit ...
");
//1-------------创建客户端对象
MQTTClientInit(&client, &network, MQTT_CMD_TIMEOUT_MS, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));
printf("MQTTConnect ...
");
//2-------------连接服务端
rc = MQTTConnect(&client, &data);
printf("MQTTSubscribe ...
");
//3-------------订阅主题substopic
rc = MQTTSubscribe(&client, "substopic", MQTT_QOS, messageArrived);
(void)sprintf_s(payload, sizeof(payload), "message number %d", count);
//4------------循环发布pubtopic主题 内容为message number+connt的计数值
MQTTPublish(&client, "pubtopic", &message)
//订阅的回调函数输出以下内容
printf("Message arrived on topic %.*s: %.*s
", data->topicName->lenstring.len,
data->topicName->lenstring.data, data->message->payloadlen, data->message->payload);
总结
相关链接
小熊派开发板学习路径:
https://growing.openharmony.cn/mainPlay/learnPathMaps?id=19
小熊派开发板MQTT文档:
https://gitee.com/bearpi/bearpi-hm_nano/blob/master/applications/BearPi/BearPi-HM_Nano/sample/D5_iot_mqtt/README.md
Windows + mosquitto搭建MQTT Broker:
https://blog.csdn.net/wallace89/article/details/125617330
OpenHarmony源码获取:
https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/get-code/sourcecode-acquire.md
OpenHarmony三方库MQTT:
https://gitee.com/openharmony-tpc/talkweb_mqtt
OpenHarmony知识体系工作组智慧家居开发样例
https://gitee.com/openharmony-sig/knowledge_demo_smart_home
使用MQTT协议连华为IOT平台
https://gitee.com/bearpi/bearpi-hm_nano/blob/master/applications/BearPi/BearPi-HM_Nano/sample/D6_iot_cloud_oc/README.md
原文标题:OpenHarmony开发之MQTT讲解
文章出处:【微信公众号:OpenAtom OpenHarmony】欢迎添加关注!文章转载请注明出处。
全部0条评论
快来发表一下你的评论吧 !