鸿蒙硬件HI3861-MQTT鸿蒙其他教程请看https://blog.csdn.net/qq_33259323/category_10520249.html
在使用MQTT前,请看我上一篇文章:鸿蒙硬件HI3861-连接WIFI
https://blog.csdn.net/qq_33259323/article/details/109511005
需要连接WIFI成功之后才能使用MQTT
参考文档:https://bbs.elecfans.com/jishu_2004598_1_1.html
1.移植MQTT移植前请确保你的项目是编译成功的
点击这个链接下载移植好的PahoMqtt(不要积分免费):链接:https://pan.baidu.com/s/1ohH-kCYjiIOa-PkOxzuyEQ 提取码:gddy
点击这个链接下载移植好的PahoMqtt(不要积分免费):https://download.csdn.net/download/qq_33259323/13092141,然后把pahomqtt文件夹放到third_party目录下面
打开vendor\hisi\hi3861\hi3861\BUILD.gn 文件,在lite_component("sdk") 中增加 "//third_party/pahomqtt:pahomqtt_static"
之后就编译一下看看是否成功
2.编写测试代码BUILD.gn
- static_library("mqtt_test_at") {
- sources = [
- "mqtt_test.c",
- "at_entry.c"
- ]
-
- include_dirs = [
- "//utils/native/lite/include",
- "//kernel/liteos_m/components/cmsis/2.0",
- "//base/iot_hardware/inteRFaces/kits/wifiiot_lite",
- "//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include",
- "//foundation/communication/interfaces/kits/wifi_lite/wifiservice",
- "//third_party/pahomqtt/MQTTPacket/src",
- "//third_party/pahomqtt/MQTTPacket/samples",
- "//vendor/hisi/hi3861/hi3861/components/at/src"
- ]
- }
复制代码 mqtt_test.c
- #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 "MQTTPacket.h"
- #include "transport.h"
-
- int mqtt_rc = 0;
- int mqtt_sock = 0;
- int mqtt_len = 0;
- unsigned char mqtt_buf[200];
- int mqtt_buflen = sizeof(mqtt_buf);
- int mqtt_req_qos = 0;
- int mqtt_msgid = 1;
- int toStop = 0;
- MQTTString topicString = MQTTString_initializer;
-
- void mqtt_exit(void){
- transport_close(mqtt_sock);
- mqtt_rc = mqtt_rc;
- printf("[MQTT] ERROR EXIT\n");
- }
-
- void mqtt_task(void){
- while (!toStop){
- char* payload = "hello HarmonyOS 1122321321";
- int payloadlen = strlen(payload);
-
- if (MQTTPacket_read(mqtt_buf, mqtt_buflen, transport_getdata) == PUBLISH){
- unsigned char dup;
- int qos;
- unsigned char retained;
- unsigned short msgid;
- int payloadlen_in;
- unsigned char* payload_in;
- int rc;
- MQTTString receivedTopic;
- rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic,
- &payload_in, &payloadlen_in, mqtt_buf, mqtt_buflen); // 发送数据
- printf("message arrived %.*s\n", payloadlen_in, payload_in);
-
- mqtt_rc = rc;
- }
-
- printf("publishing reading\n");
- mqtt_len = MQTTSerialize_publish(mqtt_buf, mqtt_buflen, 0, 0, 0, 0, topicString, (unsigned char*)payload, payloadlen);
- mqtt_rc = transport_sendPacketBuffer(mqtt_sock, mqtt_buf, mqtt_len);
-
- osDelay(1000);
- }
- }
-
- int mqtt_subscribe(char * topic){ // MQTT订阅
- /* subscribe */
- topicString.cstring = topic;
- mqtt_len = MQTTSerialize_subscribe(mqtt_buf, mqtt_buflen, 0, mqtt_msgid, 1, &topicString, &mqtt_req_qos); // MQTT订阅
- mqtt_rc = transport_sendPacketBuffer(mqtt_sock, mqtt_buf, mqtt_len); // 传输发送缓冲区
- if (MQTTPacket_read(mqtt_buf, mqtt_buflen, transport_getdata) == SUBACK) /* wait for suback */ // 等待订阅返回
- {
- unsigned short submsgid;
- int subcount;
- int granted_qos;
-
- mqtt_rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, mqtt_buf, mqtt_buflen);
- if (granted_qos != 0){
- printf("granted qos != 0, %d\n", granted_qos);
- mqtt_exit();
- return 0;
- }
-
- return 1;
- }else{
- mqtt_exit();
- return 0;
- }
- }
-
- int mqtt_init(void){ // MQTT初始化开始连接
- MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
- char *host = "106.13.62.194"; // 地址
- int port = 1883; // 端口
-
-
- mqtt_sock = transport_open(host, port);
- if(mqtt_sock < 0){
- return mqtt_sock;
- }
-
- data.clientID.cstring = "me"; // ClientID
- data.keepAliveInterval = 20;
- data.cleansession = 1;
- data.username.cstring = "testuser"; // 用户名
- data.password.cstring = "testpassword"; // 密码
-
- printf("[MQTT]Sending to hostname %s port %d\n", host, port);
-
- mqtt_len = MQTTSerialize_connect(mqtt_buf, mqtt_buflen, &data); // 开始连接
- mqtt_rc = transport_sendPacketBuffer(mqtt_sock, mqtt_buf, mqtt_len); // 发送缓冲区
-
- if (MQTTPacket_read(mqtt_buf, mqtt_buflen, transport_getdata) == CONNACK){ // 等待链接返回
- unsigned char sessionPresent, connack_rc;
-
- if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, mqtt_buf, mqtt_buflen) != 1 || connack_rc != 0){
- printf("Unable to connect, return code %d\n", connack_rc);
- mqtt_exit();
- return 0;
- }
- }else{
- mqtt_exit();
- return 0;
- }
-
- return 1;
- }
-
-
- void mqtt_test(void){
- printf("[MQTT]Start MQTT\r\n");
- if(mqtt_init() == 1){
- printf("[MQTT]MQTT Connect\r\n");
- mqtt_subscribe("substopic"); //设置订阅
- mqtt_task();
- }
- }
复制代码 mqtt_test.h
- #ifndef __MQTT_TEST_H__
- #define __MQTT_TEST_H__
-
- void mqtt_test(void);
-
- #endif /* __MQTT_TEST_H__ */
复制代码 3.开始测试只要在主函数中调用mqtt_test.c中的mqtt_test()函数就可以开始测试了

如果有什么问题可以来B站问我
https://space.bilibili.com/309103931
|