如何在鴻蒙系統(tǒng)中移植Paho-MQTT實(shí)現(xiàn)MQTT協(xié)議
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz
MQTT 是當(dāng)前最主流的物聯(lián)網(wǎng)通信協(xié)議,需要物聯(lián)網(wǎng)云平臺(tái),例如華為云、阿里云、移動(dòng)OneNET都支持mqtt。而Hi3861則是一款專為IoT應(yīng)用場(chǎng)景打造的芯片。本節(jié)主要講如何在鴻蒙系統(tǒng)中通過移植第3方軟件包 paho mqtt去實(shí)現(xiàn)MQTT協(xié)議功能,最后會(huì)給出測(cè)試驗(yàn)證。為后續(xù)的物聯(lián)網(wǎng)項(xiàng)目打好基礎(chǔ)。
友情預(yù)告,本節(jié)內(nèi)容較多,源碼也貼出來了,大家最好先看一遍,然后再操作一次。
相關(guān)源碼已經(jīng)打包上傳,順便上傳了一個(gè)測(cè)試OK的固件,大家可以直接下載附件直接測(cè)試。解壓后會(huì)得到5個(gè)壓縮包,繼續(xù)解壓即可

3.9.1 MQTT介紹
MQTT 全稱為 Message Queuing Telemetry Transport(消息隊(duì)列遙測(cè)傳輸)是一種基于發(fā)布/訂閱范式的二進(jìn)制“輕量級(jí)”消息協(xié)議,由IB公司發(fā)布。針對(duì)于網(wǎng)絡(luò)受限和嵌入式設(shè)備而設(shè)計(jì)的一種數(shù)據(jù)傳輸協(xié)議。MQTT最大優(yōu)點(diǎn)在于,可以以極少的代碼和有限的帶寬,為連接遠(yuǎn)程設(shè)備提供實(shí)時(shí)可靠的消息服務(wù)。作為一種低開銷、低帶寬占用的即時(shí)通訊協(xié)議,使其在物聯(lián)網(wǎng)、小型設(shè)備、移動(dòng)應(yīng)用等方面有較廣泛的應(yīng)用。MQTT模型如圖所示。
更多MQTT協(xié)議的介紹見這篇文章: MQTT 協(xié)議開發(fā)入門

3.9.2 移植 paho mqtt軟件包
1. 下載paho mqtt軟件包,添加到鴻蒙代碼中
paho mqtt-c 是基于C語言實(shí)現(xiàn)的MQTT客戶端,非常適合用在嵌入式設(shè)備上。首先下載源碼:
https://github.com/eclipse/paho.mqtt.embedded-c
下載之后解壓,會(huì)得到這么一個(gè)文件夾:

我們?cè)邙櫭上到y(tǒng)源碼的 third_party 文件夾下創(chuàng)建一個(gè) pahomqtt 文件夾,然后把解壓后的所有文件都拷貝到 pahomqtt 文件夾下,目錄結(jié)構(gòu)大致如下:

下一步,我們?cè)趐ahomqtt 文件夾下面新建BUILD.gn文件,用來構(gòu)建編譯。其內(nèi)容如下:
- # Copyright (c) 2020 Huawei Device Co., Ltd.
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- 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"
- ]
- }
2. 讓hi3861編譯的時(shí)候,編譯 paho mqtt軟件包
打開vendor\hisi\hi3861\hi3861\BUILD.gn 文件,在lite_component("sdk") 中增加 "//third_party/pahomqtt:pahomqtt_static",
修改后文件內(nèi)容如下:

完成以上修改后,就可以開始編譯了,然而很不幸的。。。你會(huì)發(fā)現(xiàn)好多編譯報(bào)錯(cuò)。
不過沒事,我們來一個(gè)一個(gè)解決。
3. 移植,修改編譯報(bào)錯(cuò)
打開 third_party\pahomqtt\MQTTPacket\samples\transport.c 文件,這個(gè)文件也是我們主要移植的文件,我們需要實(shí)現(xiàn) socket相關(guān)的操作,包括發(fā)送、接收數(shù)據(jù)。其實(shí)移植就3步。
(1)首先我們導(dǎo)入幾個(gè)頭文件
- #include "lwip/ip_addr.h"
- #include "lwip/netifapi.h"
- #include "lwip/sockets.h"
(2)其次修改 transport_sendPacketBuffer 函數(shù),內(nèi)容修改后如下:
- int transport_sendPacketBuffer(int sock, unsigned char* buf, int buflen)
- {
- int rc = 0;
- rc = send(sock, buf, buflen, 0);
- return rc;
- }
(3)后面編譯的時(shí)候會(huì)報(bào)錯(cuò)說 close 函數(shù)不存在,我們修改 transport_close 函數(shù),修改后內(nèi)容如下:
- int transport_close(int sock)
- {
- int rc;
- rc = shutdown(sock, SHUT_WR);
- rc = recv(sock, NULL, (size_t)0, 0);
- rc = lwip_close(sock);
- return rc;
- }
(4)修改完 transport.c 文件后,大家編譯的時(shí)候估計(jì)會(huì)遇到很多編譯錯(cuò)誤,都是某個(gè)局部變量未使用那種,大家可以修改就行。
類似于這樣的,提示 buflen 未使用的錯(cuò)誤,大家只需要在代碼中隨便寫個(gè)buflen = buflen ; 即可。
3.9.3 編寫測(cè)試代碼
測(cè)試代碼比較好寫。主要是3個(gè)文件,內(nèi)容我都貼出來了:

(1)BUILD.gn文件內(nèi)容:
- # Copyright (c) 2020 Huawei Device Co., Ltd.
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- 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"
- ]
- }
(2)at_entry.c文件主要是注冊(cè)了一個(gè)AT指令,后面大家可以使用 AT+MQTTTEST 指令來測(cè)試MQTT功能。代碼內(nèi)容如下:
- #include <stdio.h>
- #include <unistd.h>
- #include "ohos_init.h"
- #include "cmsis_os2.h"
- #include <unistd.h>
- #include <at.h>
- #include <hi_at.h>
- #include "hi_wifi_api.h"
- #include "mqtt_test.h"
- void mqtt_test_thread(void * argv)
- {
- argv = argv;
- mqtt_test();
- }
- hi_u32 at_exe_mqtt_test_cmd(void)
- {
- osThreadAttr_t attr;
- 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] Falied to create LedTask!\n");
- }
- AT_RESPONSE_OK;
- return HI_ERR_SUCCESS;
- }
- const at_cmd_func g_at_mqtt_func_tbl[] = {
- {"+MQTTTEST", 9, HI_NULL, HI_NULL, HI_NULL, (at_call_back_func)at_exe_mqtt_test_cmd},
- };
- void AtExampleEntry(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);
(3)mqtt_test.c 文件則是編寫了一個(gè)簡(jiǎn)單的MQTT測(cè)試代碼,具體代碼講解,后面會(huì)重新開一篇。其中測(cè)試用的mqtt服務(wù)器是我自己的服務(wù)器:106.13.62.194
大家也可以改成自己的,也可以直接用我個(gè)人的mqtt服務(wù)器 。
- #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 toStop = 0;
- int mqtt_connect(void)
- {
- MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
- int rc = 0;
- int mysock = 0;
- unsigned char buf[200];
- int buflen = sizeof(buf);
- int msgid = 1;
- MQTTString topicString = MQTTString_initializer;
- int req_qos = 0;
- char* payload = "hello HarmonyOS";
- int payloadlen = strlen(payload);
- int len = 0;
- char *host = "106.13.62.194";
- //char *host = "192.168.1.102";
- int port = 1883;
- mysock = transport_open(host, port);
- if(mysock < 0)
- return mysock;
- printf("Sending to hostname %s port %d\n", host, port);
- data.clientID.cstring = "me";
- data.keepAliveInterval = 20;
- data.cleansession = 1;
- data.username.cstring = "testuser";
- data.password.cstring = "testpassword";
- len = MQTTSerialize_connect(buf, buflen, &data);
- rc = transport_sendPacketBuffer(mysock, buf, len);
- /* wait for connack */
- if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK)
- {
- unsigned char sessionPresent, connack_rc;
- if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0)
- {
- printf("Unable to connect, return code %d\n", connack_rc);
- goto exit;
- }
- }
- else
- goto exit;
- /* subscribe */
- topicString.cstring = "substopic";
- len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos);
- rc = transport_sendPacketBuffer(mysock, buf, len);
- if (MQTTPacket_read(buf, buflen, transport_getdata) == SUBACK) /* wait for suback */
- {
- unsigned short submsgid;
- int subcount;
- int granted_qos;
- rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen);
- if (granted_qos != 0)
- {
- printf("granted qos != 0, %d\n", granted_qos);
- goto exit;
- }
- }
- else
- goto exit;
- /* loop getting msgs on subscribed topic */
- topicString.cstring = "pubtopic";
- while (!toStop)
- {
- /* transport_getdata() has a built-in 1 second timeout,
- your mileage will vary */
- if (MQTTPacket_read(buf, 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, buf, buflen);
- printf("message arrived %.*s\n", payloadlen_in, payload_in);
- rc = rc;
- }
- printf("publishing reading\n");
- len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, (unsigned char*)payload, payloadlen);
- rc = transport_sendPacketBuffer(mysock, buf, len);
- }
- printf("disconnecting\n");
- len = MQTTSerialize_disconnect(buf, buflen);
- rc = transport_sendPacketBuffer(mysock, buf, len);
- exit:
- transport_close(mysock);
- rc = rc;
- return 0;
- }
- void mqtt_test(void)
- {
- mqtt_connect();
- }
mqtt_test.h文件內(nèi)容:
- #ifndef __MQTT_TEST_H__
- #define __MQTT_TEST_H__
- void mqtt_test(void);
- #endif /* __MQTT_TEST_H__ */
到這里就完成了代碼部分,可以開始編譯了。
3.9.4 MQTT實(shí)驗(yàn)
這里我們需要先下載一個(gè) Windows電腦端的 MQTT客戶端,這樣我們就可以用電腦訂閱開發(fā)板的MQTT主題信息了。
電腦版的mqtt客戶端下載鏈接: https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.ui.app/1.1.1/
我們選擇這一個(gè):
弄完后打開軟件,按圖操作:

操作完后,我們把編譯后程序燒寫到開發(fā)板,輸入如下串口指令,讓開發(fā)板連接上網(wǎng)絡(luò),因?yàn)镸QTT功能需要網(wǎng)絡(luò)支持。輸入如下串口指令:
- AT+STARTSTA 開啟STA模式
- AT+CONN="12-203",,2,"07686582488" 連接到路由器,注意wifi熱點(diǎn)名和密碼用自己的
- AT+DHCP=wlan0,1 獲取IP地址
- AT+IFCFG 打印查看IP地址
串口指令的應(yīng)答應(yīng)該如下:

成功連接上路由器后,請(qǐng)確保路由器是可以上網(wǎng)的。
然后我們輸入我們的 MQTT測(cè)試的AT指令: AT+MQTTTEST
應(yīng)該可以看到如下打?。?/p>

此時(shí)我們?nèi)ゲ榭?我們電腦端的MQTT客戶端軟件,可以看到右邊已經(jīng)有接收MQTT信息了,主題未 pubtopic,消息內(nèi)容為 hello HarmonyOS ! ,說明實(shí)驗(yàn)成功。

3.9.5 總結(jié)
這一次的內(nèi)容比較多,其中總結(jié)起來就4步:
(1)添加第三方軟件包 paho mqtt,關(guān)于如何添加第3方軟件包,我之前有一篇文章已經(jīng)講了。
可以參考:如何往鴻蒙系統(tǒng)源碼中添加第三方軟件包
(2)移植 paho mqtt
(3)編寫測(cè)試代碼,這里我們用的是注冊(cè)AT指令的方式,方便大家使用AT指令測(cè)試。
(4)測(cè)試,這里用電腦裝mqtt客戶端程序,去驗(yàn)證。
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz