了解最新你所在地址试管婴儿费用试管婴儿中介公司试管婴儿资讯以及其它试管相关技术。
在鸿蒙系统中移植Paho
试管需知 凡是以任何理由向无故收费的机构均有欺诈嫌疑,请保持警惕!建议多家咨询对比,寻找有通过身份证+执照验证的招聘信息。切勿个人与个人合作。
会员级别: 免费会员(到期时间:终身)
置顶情况: 未置顶
公司名称: 神州中泰国际医疗集团
认证情况:

未上传身份证认证 未上传身份证认证

未上传营业执照认证 未上传营业执照认证

咨询电话:
18908074581
联系微信: 18908074581
  • 想要入驻中泰忧孕网站可以点击右侧立即入驻 →


MQTT 是当前最主流的物联网通信协议,需要物联网云平台,例如华为云、阿里云、移动 OneNET 都支持 MQTT。而 Hi3861则是一款专为 IoT 应用场景打造的芯片。

本节主要讲如何在鸿蒙系统中通过移植第 3 方软件包 Paho-MQTT 去实现 MQTT 协议功能,最后会给出测试验证。为后续的物联网项目打好基础。

友情预告,本节内容较多,源码也贴出来了,大家最好先看一遍,然后再操作一次。

相关源码已经打包上传,顺便上传了一个测试 OK 的固件,大家可以直接下载附件直接测试。解压后会得到 5 个压缩包,继续解压即可:

MQTT 介绍

MQTT 全称为 Message Queuing Telemetry Transport(消息队列遥测传输)是一种基于发布/订阅范式的二进制 轻量级 消息协议,由 IBM 公司发布。针对于网络受限和嵌入式设备而设计的一种数据传输协议。

MQTT 最大优点在于,可以以极少的代码和有限的带宽,为连接远程设备提供实时可靠的消息服务。

作为一种低开销、低带宽占用的即时通讯协议,使其在物联网、小型设备、移动应用等方面有较广泛的应用。

MQTT 模型如图所示:

更多 MQTT 协议的介绍见上篇文章:MQTT 协议开发入门。

移植 Paho-MQTT 软件包

下载 paho mqtt 软件包,添加到鸿蒙代码中

paho mqtt-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_sources public_configs=[":pahomqtt_config"] } lite_library("pahomqtt_shared"){ target_type="shared_library" sources=pahomqtt_sources public_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" ] }

让 hi3861编译的时候,编译 paho mqtt软件包

打开 vendorhisihi3861hi3861BUILD.gn 文件。

在 lite_component("sdk") 中增加 "//third_party/pahomqtt:pahomqtt_static"。

修改后文件内容如下:

完成以上修改后,就可以开始编译了,然而很不幸的...你会发现好多编译报错。

不过没事,我们来一个一个解决。

移植,修改编译报错

打开 third_partypahomqttMQTTPacketsamples ransport.c 文件,这个文件也是我们主要移植的文件,我们需要实现 socket 相关的操作,包括发送、接收数据。

其实移植就 3 步:

①首先我们导入几个头文件:

#include"lwip/ip_addr.h" #include"lwip/netifapi.h" #include"lwip/sockets.h" ②其次修改 transport_sendPacketBuffer 函数,内容修改后如下:inttransport_sendPacketBuffer(intsock,unsignedchar*buf,intbuflen) { intrc=0; rc=send(sock,buf,buflen,0); returnrc; }

③后面编译的时候会报错说 close 函数不存在,我们修改 transport_close 函数,修改后内容如下:

inttransport_close(intsock) { intrc; rc=shutdown(sock,SHUT_WR); rc=recv(sock,NULL,(size_t)0,0); rc=lwip_close(sock); returnrc; }

④修改完 transport.c 文件后,大家编译的时候估计会遇到很多编译错误,都是某个局部变量未使用那种,大家可以修改就行。

类似于这样的,提示 buflen 未使用的错误,大家只需要在代码中随便写个buflen = buflen; 即可。

编写测试代码

测试代码比较好写。主要是 3 个文件,内容我都贴出来了:

①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. 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", "//vendorhisihi3861hi3861componentsatsrc" ] }

②at_entry.c 文件主要是注册了一个 AT 指令,后面大家可以使用 AT+MQTTTEST 指令来测试 MQTT 功能

代码内容如下:

#include #include #include"ohos_init.h" #include"cmsis_os2.h" #include #include #include #include"hi_wifi_api.h" #include"mqtt_test.h" voidmqtt_test_thread(void*argv) { argv=argv; mqtt_test(); } hi_u32at_exe_mqtt_test_cmd(void) { osThreadAttr_tattr; attr.name="wifi_config_thread"; attr.attr_bits=0U; attr.cb_mem=NULL; attr.cb_size=0U; attr.stack_mem=NULL; attr.stack_size=4096; attr.priority=36; if(osThreadNew((osThreadFunc_t)mqtt_test_thread,NULL,&attr)==NULL){ printf("[LedExample]FaliedtocreateLedTask! "); } AT_RESPONSE_OK; returnHI_ERR_SUCCESS; } constat_cmd_funcg_at_mqtt_func_tbl[]={ {"+MQTTTEST",9,HI_NULL,HI_NULL,HI_NULL,(at_call_back_func)at_exe_mqtt_test_cmd}, }; voidAtExampleEntry(void) { hi_at_register_cmd(g_at_mqtt_func_tbl,sizeof(g_at_mqtt_func_tbl)/sizeof(g_at_mqtt_func_tbl[0])); } SYS_RUN(AtExampleEntry);

③mqtt_test.c 文件则是编写了一个简单的 MQTT 测试代码,具体代码讲解,后面会重新开一篇

其中测试用的 mqtt 服务器是我自己的服务器:106.13.62.194,大家也可以改成自己的,也可以直接用我个人的 mqtt 服务器。

#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"MQTTPacket.h" #include"transport.h" inttoStop=0; intmqtt_connect(void) { MQTTPacket_connectDatadata=MQTTPacket_connectData_initializer; intrc=0; intmysock=0; unsignedcharbuf[200]; intbuflen=sizeof(buf); intmsgid=1; MQTTStringtopicString=MQTTString_initializer; intreq_qos=0; char*payload="helloHarmonyOS"; intpayloadlen=strlen(payload); intlen=0; char*host="106.13.62.194"; //char*host="192.168.1.102"; intport=1883; mysock=transport_open(host,port); if(mysock打开APP阅读更多精彩内容