自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在鴻蒙系統(tǒng)中移植Paho-MQTT實現(xiàn)MQTT協(xié)議

系統(tǒng)
本節(jié)主要講如何在鴻蒙系統(tǒng)中通過移植第3方軟件包 paho mqtt去實現(xiàn)MQTT協(xié)議功能,最后會給出測試驗證。為后續(xù)的物聯(lián)網(wǎng)項目打好基礎(chǔ)。

[[356033]]

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com/#zz

 MQTT 是當前最主流的物聯(lián)網(wǎng)通信協(xié)議,需要物聯(lián)網(wǎng)云平臺,例如華為云、阿里云、移動OneNET都支持mqtt。而Hi3861則是一款專為IoT應(yīng)用場景打造的芯片。本節(jié)主要講如何在鴻蒙系統(tǒng)中通過移植第3方軟件包 paho mqtt去實現(xiàn)MQTT協(xié)議功能,最后會給出測試驗證。為后續(xù)的物聯(lián)網(wǎng)項目打好基礎(chǔ)。

友情預(yù)告,本節(jié)內(nèi)容較多,源碼也貼出來了,大家最好先看一遍,然后再操作一次。

相關(guān)源碼已經(jīng)打包上傳,順便上傳了一個測試OK的固件,大家可以直接下載附件直接測試。解壓后會得到5個壓縮包,繼續(xù)解壓即可。


3.9.1 MQTT介紹

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

更多MQTT協(xié)議的介紹見這篇文章: MQTT 協(xié)議開發(fā)入門


3.9.2 移植 paho mqtt軟件包

1. 下載paho mqtt軟件包,添加到鴻蒙代碼中

paho mqtt-c 是基于C語言實現(xiàn)的MQTT客戶端,非常適合用在嵌入式設(shè)備上。首先下載源碼:

https://github.com/eclipse/paho.mqtt.embedded-c

下載之后解壓,會得到這么一個文件夾:


我們在鴻蒙系統(tǒng)源碼的 third_party 文件夾下創(chuàng)建一個 pahomqtt 文件夾,然后把解壓后的所有文件都拷貝到 pahomqtt 文件夾下,目錄結(jié)構(gòu)大致如下:


下一步,我們在pahomqtt 文件夾下面新建BUILD.gn文件,用來構(gòu)建編譯。其內(nèi)容如下:

  1. # Copyright (c) 2020 Huawei Device Co., Ltd. 
  2.  
  3. # Licensed under the Apache License, Version 2.0 (the "License"); 
  4.  
  5. # you may not use this file except in compliance with the License. 
  6.  
  7. # You may obtain a copy of the License at 
  8.  
  9.  
  10. # http://www.apache.org/licenses/LICENSE-2.0 
  11.  
  12.  
  13. # Unless required by applicable law or agreed to in writing, software 
  14.  
  15. # distributed under the License is distributed on an "AS IS" BASIS, 
  16.  
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  18.  
  19. # See the License for the specific language governing permissions and 
  20.  
  21. # limitations under the License. 
  22.  
  23. import("//build/lite/config/component/lite_component.gni"
  24.  
  25. import("//build/lite/ndk/ndk.gni"
  26.  
  27. config("pahomqtt_config") { 
  28.  
  29. include_dirs = [ 
  30.  
  31. "MQTTPacket/src"
  32.  
  33. "MQTTPacket/samples"
  34.  
  35. "//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include"
  36.  
  37. "//kernel/liteos_m/components/cmsis/2.0"
  38.  
  39.  
  40.  
  41. pahomqtt_sources = [ 
  42.  
  43. "MQTTPacket/samples/transport.c"
  44.  
  45. "MQTTPacket/src/MQTTConnectClient.c"
  46.  
  47. "MQTTPacket/src/MQTTConnectServer.c"
  48.  
  49. "MQTTPacket/src/MQTTDeserializePublish.c"
  50.  
  51. "MQTTPacket/src/MQTTFormat.c"
  52.  
  53. "MQTTPacket/src/MQTTPacket.c"
  54.  
  55. "MQTTPacket/src/MQTTSerializePublish.c"
  56.  
  57. "MQTTPacket/src/MQTTSubscribeClient.c"
  58.  
  59. "MQTTPacket/src/MQTTSubscribeServer.c"
  60.  
  61. "MQTTPacket/src/MQTTUnsubscribeClient.c"
  62.  
  63. "MQTTPacket/src/MQTTUnsubscribeServer.c"
  64.  
  65.  
  66. lite_library("pahomqtt_static") { 
  67.  
  68. target_type = "static_library" 
  69.  
  70. sources = pahomqtt_sources 
  71.  
  72. public_configs = [ ":pahomqtt_config" ] 
  73.  
  74.  
  75. lite_library("pahomqtt_shared") { 
  76.  
  77. target_type = "shared_library" 
  78.  
  79. sources = pahomqtt_sources 
  80.  
  81. public_configs = [ ":pahomqtt_config" ] 
  82.  
  83.  
  84. ndk_lib("pahomqtt_ndk") { 
  85.  
  86. if (board_name != "hi3861v100") { 
  87.  
  88. lib_extension = ".so" 
  89.  
  90. deps = [ 
  91.  
  92. ":pahomqtt_shared" 
  93.  
  94.  
  95. else { 
  96.  
  97. deps = [ 
  98.  
  99. ":pahomqtt_static" 
  100.  
  101.  
  102.  
  103. head_files = [ 
  104.  
  105. "//third_party/pahomqtt" 
  106.  
  107.  

 2. 讓hi3861編譯的時候,編譯 paho mqtt 軟件包

打開vendor\hisi\hi3861\hi3861\BUILD.gn 文件,在lite_component("sdk") 中增加 "//third_party/pahomqtt:pahomqtt_static",

修改后文件內(nèi)容如下:

 完成以上修改后,就可以開始編譯了,然而很不幸的。。。你會發(fā)現(xiàn)好多編譯報錯。

不過沒事,我們來一個一個解決。

3. 移植,修改編譯報錯

打開 third_party\pahomqtt\MQTTPacket\samples\transport.c 文件,這個文件也是我們主要移植的文件,我們需要實現(xiàn) socket相關(guān)的操作,包括發(fā)送、接收數(shù)據(jù)。其實移植就3步。

(1)首先我們導(dǎo)入幾個頭文件

  1. #include "lwip/ip_addr.h" 
  2.  
  3. #include "lwip/netifapi.h" 
  4.  
  5. #include "lwip/sockets.h" 

 (2)其次修改 transport_sendPacketBuffer 函數(shù),內(nèi)容修改后如下:

  1. int transport_sendPacketBuffer(int sock, unsigned char* buf, int buflen) 
  2.  
  3.  
  4. int rc = 0; 
  5.  
  6. rc = send(sock, buf, buflen, 0); 
  7.  
  8. return rc; 
  9.  

 (3)后面編譯的時候會報錯說 close 函數(shù)不存在,我們修改 transport_close 函數(shù),修改后內(nèi)容如下:

  1. int transport_close(int sock) 
  2.  
  3.  
  4. int rc; 
  5.  
  6. rc = shutdown(sock, SHUT_WR); 
  7.  
  8. rc = recv(sock, NULL, (size_t)0, 0); 
  9.  
  10. rc = lwip_close(sock); 
  11.  
  12. return rc; 
  13.  

 (4)修改完 transport.c 文件后,大家編譯的時候估計會遇到很多編譯錯誤,都是某個局部變量未使用那種,大家可以修改就行。

類似于這樣的,提示 buflen 未使用的錯誤,大家只需要在代碼中隨便寫個buflen = buflen ; 即可。

 3.9.3 編寫測試代碼

測試代碼比較好寫。主要是3個文件,內(nèi)容我都貼出來了:

 (1)BUILD.gn文件內(nèi)容:

  1. # Copyright (c) 2020 Huawei Device Co., Ltd. 
  2.  
  3. # Licensed under the Apache License, Version 2.0 (the "License"); 
  4.  
  5. # you may not use this file except in compliance with the License. 
  6.  
  7. # You may obtain a copy of the License at 
  8.  
  9.  
  10. # http://www.apache.org/licenses/LICENSE-2.0 
  11.  
  12.  
  13. # Unless required by applicable law or agreed to in writing, software 
  14.  
  15. # distributed under the License is distributed on an "AS IS" BASIS, 
  16.  
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  18.  
  19. # See the License for the specific language governing permissions and 
  20.  
  21. # limitations under the License. 
  22.  
  23. static_library("mqtt_test_at") { 
  24.  
  25. sources = [ 
  26.  
  27. "mqtt_test.c"
  28.  
  29. "at_entry.c" 
  30.  
  31.  
  32. include_dirs = [ 
  33.  
  34. "//utils/native/lite/include"
  35.  
  36. "//kernel/liteos_m/components/cmsis/2.0"
  37.  
  38. "//base/iot_hardware/interfaces/kits/wifiiot_lite"
  39.  
  40. "//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include"
  41.  
  42. "//foundation/communication/interfaces/kits/wifi_lite/wifiservice"
  43.  
  44. "//third_party/pahomqtt/MQTTPacket/src"
  45.  
  46. "//third_party/pahomqtt/MQTTPacket/samples"
  47.  
  48. "//vendor\hisi\hi3861\hi3861\components\at\src" 
  49.  
  50.  

 (2)at_entry.c文件主要是注冊了一個AT指令,后面大家可以使用 AT+MQTTTEST 指令來測試MQTT功能。代碼內(nèi)容如下:

  1. #include 
  2.  
  3. #include 
  4.  
  5. #include "ohos_init.h" 
  6.  
  7. #include "cmsis_os2.h" 
  8.  
  9. #include 
  10.  
  11. #include 
  12.  
  13. #include 
  14.  
  15. #include "hi_wifi_api.h" 
  16.  
  17. #include "mqtt_test.h" 
  18.  
  19. void mqtt_test_thread(void * argv) 
  20.  
  21.  
  22. argv = argv; 
  23.  
  24. mqtt_test(); 
  25.  
  26.  
  27. hi_u32 at_exe_mqtt_test_cmd(void) 
  28.  
  29.  
  30. osThreadAttr_t attr; 
  31.  
  32. attr.name = "wifi_config_thread"
  33.  
  34. attr.attr_bits = 0U; 
  35.  
  36. attr.cb_mem = NULL
  37.  
  38. attr.cb_size = 0U; 
  39.  
  40. attr.stack_mem = NULL
  41.  
  42. attr.stack_size = 4096; 
  43.  
  44. attr.priority = 36; 
  45.  
  46. if (osThreadNew((osThreadFunc_t)mqtt_test_thread, NULL, &attr) == NULL) { 
  47.  
  48. printf("[LedExample] Falied to create LedTask!\n"); 
  49.  
  50.  
  51. AT_RESPONSE_OK; 
  52.  
  53. return HI_ERR_SUCCESS; 
  54.  
  55.  
  56. const at_cmd_func g_at_mqtt_func_tbl[] = { 
  57.  
  58. {"+MQTTTEST", 9, HI_NULL, HI_NULL, HI_NULL, (at_call_back_func)at_exe_mqtt_test_cmd}, 
  59.  
  60. }; 
  61.  
  62. void AtExampleEntry(void) 
  63.  
  64.  
  65. hi_at_register_cmd(g_at_mqtt_func_tbl, sizeof(g_at_mqtt_func_tbl)/sizeof(g_at_mqtt_func_tbl[0])); 
  66.  
  67.  
  68. SYS_RUN(AtExampleEntry); 

 (3)mqtt_test.c 文件則是編寫了一個簡單的MQTT測試代碼

具體代碼講解,后面會重新開一篇。其中測試用的mqtt服務(wù)器是我自己的服務(wù)器:106.13.62.194

大家也可以改成自己的,也可以直接用我個人的mqtt服務(wù)器。

  1. #include 
  2.  
  3. #include 
  4.  
  5. #include "ohos_init.h" 
  6.  
  7. #include "cmsis_os2.h" 
  8.  
  9. #include 
  10.  
  11. #include "hi_wifi_api.h" 
  12.  
  13. //#include "wifi_sta.h" 
  14.  
  15. #include "lwip/ip_addr.h" 
  16.  
  17. #include "lwip/netifapi.h" 
  18.  
  19. #include "lwip/sockets.h" 
  20.  
  21. #include "MQTTPacket.h" 
  22.  
  23. #include "transport.h" 
  24.  
  25. int toStop = 0; 
  26.  
  27. int mqtt_connect(void) 
  28.  
  29.  
  30. MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 
  31.  
  32. int rc = 0; 
  33.  
  34. int mysock = 0; 
  35.  
  36. unsigned char buf[200]; 
  37.  
  38. int buflen = sizeof(buf); 
  39.  
  40. int msgid = 1; 
  41.  
  42. MQTTString topicString = MQTTString_initializer; 
  43.  
  44. int req_qos = 0; 
  45.  
  46. char* payload = "hello HarmonyOS"
  47.  
  48. int payloadlen = strlen(payload); 
  49.  
  50. int len = 0; 
  51.  
  52. char *host = "106.13.62.194"
  53.  
  54. //char *host = "192.168.1.102"
  55.  
  56. int port = 1883; 
  57.  
  58. mysock = transport_open(host, port); 
  59.  
  60. if(mysock < 0) 
  61.  
  62. return mysock; 
  63.  
  64. printf("Sending to hostname %s port %d\n", host, port); 
  65.  
  66. data.clientID.cstring = "me"
  67.  
  68. data.keepAliveInterval = 20; 
  69.  
  70. data.cleansession = 1; 
  71.  
  72. data.username.cstring = "testuser"
  73.  
  74. data.password.cstring = "testpassword"
  75.  
  76. len = MQTTSerialize_connect(buf, buflen, &data); 
  77.  
  78. rc = transport_sendPacketBuffer(mysock, buf, len); 
  79.  
  80. /* wait for connack */ 
  81.  
  82. if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK) 
  83.  
  84.  
  85. unsigned char sessionPresent, connack_rc; 
  86.  
  87. if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0) 
  88.  
  89.  
  90. printf("Unable to connect, return code %d\n", connack_rc); 
  91.  
  92. goto exit; 
  93.  
  94.  
  95.  
  96. else 
  97.  
  98. goto exit; 
  99.  
  100. /* subscribe */ 
  101.  
  102. topicString.cstring = "substopic"
  103.  
  104. len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos); 
  105.  
  106. rc = transport_sendPacketBuffer(mysock, buf, len); 
  107.  
  108. if (MQTTPacket_read(buf, buflen, transport_getdata) == SUBACK) /* wait for suback */ 
  109.  
  110.  
  111. unsigned short submsgid; 
  112.  
  113. int subcount; 
  114.  
  115. int granted_qos; 
  116.  
  117. rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen); 
  118.  
  119. if (granted_qos != 0) 
  120.  
  121.  
  122. printf("granted qos != 0, %d\n", granted_qos); 
  123.  
  124. goto exit; 
  125.  
  126.  
  127.  
  128. else 
  129.  
  130. goto exit; 
  131.  
  132. /* loop getting msgs on subscribed topic */ 
  133.  
  134. topicString.cstring = "pubtopic"
  135.  
  136. while (!toStop) 
  137.  
  138.  
  139. /* transport_getdata() has a built-in 1 second timeout, 
  140.  
  141. your mileage will vary */ 
  142.  
  143. if (MQTTPacket_read(buf, buflen, transport_getdata) == PUBLISH) 
  144.  
  145.  
  146. unsigned char dup; 
  147.  
  148. int qos; 
  149.  
  150. unsigned char retained; 
  151.  
  152. unsigned short msgid; 
  153.  
  154. int payloadlen_in; 
  155.  
  156. unsigned char* payload_in; 
  157.  
  158. int rc; 
  159.  
  160. MQTTString receivedTopic; 
  161.  
  162. rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic, 
  163.  
  164. &payload_in, &payloadlen_in, buf, buflen); 
  165.  
  166. printf("message arrived %.*s\n", payloadlen_in, payload_in); 
  167.  
  168. rc = rc; 
  169.  
  170.  
  171. printf("publishing reading\n"); 
  172.  
  173. len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, (unsigned char*)payload, payloadlen); 
  174.  
  175. rc = transport_sendPacketBuffer(mysock, buf, len); 
  176.  
  177.  
  178. printf("disconnecting\n"); 
  179.  
  180. len = MQTTSerialize_disconnect(buf, buflen); 
  181.  
  182. rc = transport_sendPacketBuffer(mysock, buf, len); 
  183.  
  184. exit: 
  185.  
  186. transport_close(mysock); 
  187.  
  188. rc = rc; 
  189.  
  190. return 0; 
  191.  
  192.  
  193. void mqtt_test(void) 
  194.  
  195.  
  196. mqtt_connect(); 
  197.  

 mqtt_test.h文件內(nèi)容:

  1. #ifndef __MQTT_TEST_H__ 
  2.  
  3. #define __MQTT_TEST_H__ 
  4.  
  5. void mqtt_test(void); 
  6.  
  7. #endif /* __MQTT_TEST_H__ */ 

 到這里就完成了代碼部分,可以開始編譯了。

3.9.4 MQTT實驗

這里我們需要先下載一個 Windows電腦端的 MQTT客戶端,這樣我們就可以用電腦訂閱開發(fā)板的MQTT主題信息了。

電腦版的mqtt客戶端下載鏈接: https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.ui.app/1.1.1/

我們選擇這一個: 

 弄完后打開軟件,按圖操作: 

 

操作完后,我們把編譯后程序燒寫到開發(fā)板,輸入如下串口指令,讓開發(fā)板連接上網(wǎng)絡(luò),因為MQTT功能需要網(wǎng)絡(luò)支持。輸入如下串口指令:

AT+STARTSTA 開啟STA模式

AT+CONN="12-203",,2,"07686582488" 連接到路由器,注意wifi熱點名和密碼用自己的

AT+DHCP=wlan0,1 獲取IP地址

AT+IFCFG 打印查看IP地址

串口指令的應(yīng)答應(yīng)該如下:

 

成功連接上路由器后,請確保路由器是可以上網(wǎng)的。

然后我們輸入我們的 MQTT測試的AT指令: AT+MQTTTEST

應(yīng)該可以看到如下打?。?/p>


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


3.9.5 總結(jié)

這一次的內(nèi)容比較多,其中總結(jié)起來就4步:

(1)添加第三方軟件包 paho mqtt,關(guān)于如何添加第3方軟件包,我之前有一篇文章已經(jīng)講了。

可以參考:如何往鴻蒙系統(tǒng)源碼中添加第三方軟件包

(2)移植 paho mqtt

(3)編寫測試代碼,這里我們用的是注冊AT指令的方式,方便大家使用AT指令測試。

(4)測試,這里用電腦裝mqtt客戶端程序,去驗證。

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com/#zz

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2020-11-18 11:36:35

鴻蒙系統(tǒng)

2020-11-17 08:59:28

MQTT

2022-02-09 19:45:41

MQTTOpenHarmon鴻蒙

2022-09-26 11:30:40

MQTT協(xié)議客戶端協(xié)議

2018-08-17 06:13:16

物聯(lián)網(wǎng)協(xié)議MQTTMQTT-SN

2022-05-17 11:06:52

車聯(lián)網(wǎng)通信協(xié)議MQTT

2021-08-04 10:22:27

鴻蒙HarmonyOS應(yīng)用

2022-06-27 10:41:45

MQTT物聯(lián)網(wǎng)協(xié)議

2022-02-25 07:34:36

MQTT協(xié)議RabbitMQ

2023-03-20 16:16:40

MQTT傳輸協(xié)議

2024-03-26 11:52:13

2023-09-24 23:18:50

2020-11-24 09:52:22

MQTT

2023-09-07 14:59:42

物聯(lián)網(wǎng)MQTTCoAP

2023-03-13 15:27:48

2021-07-05 22:22:24

協(xié)議MQTT

2022-08-30 21:47:03

MQTT ProtoOthers

2016-11-28 14:40:00

MQTT消息協(xié)議

2023-04-19 15:02:01

MQTT人工智能物聯(lián)網(wǎng)

2019-12-27 10:42:45

HTTPMQTT物聯(lián)網(wǎng)
點贊
收藏

51CTO技術(shù)棧公眾號