更多内容请访问:与华为官方共建的鸿蒙技术社区https://harmonyos.51cto.com/#zzMQTT是目前最主流的互联网物联网通信协议需要物联网云平台,比如华为云、阿里云、移动OneNET都支持mqtt。Hi3861是一款专为物联网应用场景设计的芯片。本节主要讲如何通过在鸿蒙系统中移植第三方软件包pahomqtt实现MQTT协议功能,最后给出测试验证。为后续的物联网项目打下坚实的基础。友情提示,本节内容很多,源码也贴出来了。你最好先看一下,然后再操作。相关源码已打包上传。顺便上传了一个test-OK的固件。可以直接下载附件直接测试。解压后会得到5个压缩包,继续解压。3.9.1MQTT简介MQTT全称为消息队列遥测传输(MessageQueueTelemetryTransport),是一种基于发布/订阅范式的二进制“轻量级”消息协议。由IB公司出版。专为网络受限和嵌入式设备设计的数据传输协议。MQTT最大的优势在于它可以以极少的代码和有限的带宽为连接的远程设备提供实时可靠的消息服务。作为一种低开销、低带宽的即时通讯协议,它在物联网、小型设备和移动应用中有着广泛的应用。MQTT模型如图所示。关于MQTT协议的更多信息,请参见这篇文章:MQTT协议开发介绍3.9.2移植pahomqtt软件包1、下载pahomqtt软件包,添加到鸿蒙代码中。pahomqtt-c是一个基于C语言的MQTT客户端。非常适合在嵌入式设备上使用。首先下载源码:https://github.com/eclipse/paho.mqtt.embedded-c下载解压后会得到这样一个文件夹:我们在鸿蒙系统源码的third_party文件夹下新建一个pahomqtt文件夹,然后把所有解压后的文件复制到pahomqtt文件夹下,目录结构大致如下:接下来我们在pahomqtt文件夹下新建BUILD.gn文件进行构建编译。其内容如下:#Copyright(c)2020HuaweiDeviceCo.,Ltd.#LicensedundertheApacheLicense,Version2.0(the"License");#youmaynotusethisfileexceptincompliancewiththeLicense.#YoumayobtainacopyoftheLicenseat##http://www.apache.org/licenses/LICENSE-2.0##Unlessrequiredbyapplicablelaworagreedtoinwriting,software#distributedundertheLicenseisdistributedonan"ASIS"BASIS,#WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.#SeetheLicenseforthespecificlanguagegoverningpermissionsand#limitationsundertheLicense.import("//build/lite/config/component/lite_component.gni")import("//build/lite/ndk目录ndk.gni")config("pahomqtt_config"){include_dirs=["MQTTPacket/src","MQTTPacket/samples","//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include","//kernel/liteos_m/components/cmsis/2.0",]}pahomqtt_sources=["MQTTPacket/samples/transport.c","MQTTPacket/src/MQTTConnectClient.c","MQTTPacket/src/MQTTConnectServer.c","MQTTPacket/src/MQTTDeserializePublish.c","MQTTPacket/src/MQTTFormat.c","MQTTPacket/src/MQTTPacket.c","MQTTPacket/src/MQTTSerializePublish.c","MQTTPacket/src/MQTTSubscribeClient.c","MQTTPacket/src/MQTTSubscribeServer.c","MQTTPacket/src/MQTTUnsubscribeClient.c","MQTTPacket/src/MQTTUnsubscribeServer.c",]lite_library("pahomqtt_static"){target_type="static_library"sources=pahomqtt_sourcespublic_configs=[":pahomqtt_config"]}lite_library("pahomqtt_shared"){target_type="shared_library"sources=pahomqtt_sourcespublic_configs=[":pahomqtt_config"]}ndk_lib("pahomqtt_ndk"){if(board_name!="hi3861v100"){lib_extension=".so"deps=[":pahomqtt_shared"]}else{deps=[":pahomqtt_static"]}head_files=["//third_party/pahomqtt"]}2。编译hi3861时,编译pahomqtt包,打开vendor\hisi\hi3861\hi3861\BUILD.gn文件,在lite_component("sdk")中添加"//third_party/pahomqtt:pahomqtt_static",修改后文件内容如下:完成以上修改后,就可以开始编译了,可惜。.你会发现很多编译错误。不过没关系,我们一一解决。3、移植,修改编译错误,打开third_party\pahomqtt\MQTTPacket\samples\transport.c文件。这个文件也是我们主要的移植文件。我们需要实现socket相关的操作,包括发送和接收数据。其实移植只要3步。(1)首先,我们导入几个头文件#include"lwip/ip_addr.h"#include"lwip/netifapi.h"#include"lwip/sockets.h"(2)其次,修改transport_sendPacketBuffer函数,修改内容如下:inttransport_sendPacketBuffer(intsock,unsignedchar*buf,intbuflen){intrc=0;rc=send(sock,buf,buflen,0);returnrc;}(3)后面编译的时候会报错说关闭功能不存在。我们修改transport_close函数,修改内容如下:inttransport_close(intsock){intrc;rc=shutdown(sock,SHUT_WR);rc=recv(sock,NULL,(size_t)0,0);rc=lwip_close(sock);returnrc;}(4)修改transport.c文件后,编译的时候可能会遇到很多编译错误,因为有些局部变量没有用到,可以修改。类似这样,提示没有使用buflen的错误。你只需要写buflen=buflen;在代码中。3.9.3编写测试代码编写测试代码比较容易。主要是3个文件,内容我都贴出来了:(1)BUILD.gn文件内容:#Copyright(c)2020HuaweiDeviceCo.,Ltd.#LicensedundertheApacheLicense,Version2.0(the"License");#youmaynotusethisfileexceptincompliancewiththeLicensea.#oftheLicensea.##http://www.apache.org/licenses/LICENSE-2.0##Unlessrequiredbyapplicablelaworagreedtoinwriting,software#distributedundertheLicenseisdistributedonan"ASIS"BASIS,#WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.#SeetheLicenseforthespecificlanguagegoverningpermissionsand#limitationsundertheLicense.static_library_test("atmqt"){}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"]}(2)at_entry.c文件主要用于注册一个AT指令后,后面可以使用AT+MQTTTEST指令测试MQTT功能代码内容如下:#include
