如何在鸿蒙系统中移植Paho-MQTT实现MQTT协议

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

[[356033]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

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

 MQTT 是当前最主流的物联网通信协议,需要物联网云平台,例如华为云、阿里云、移动OneNET都支持mqtt。而Hi3861则是一款专为IoT应用场景打造的芯片。本节主要讲如何在鸿蒙系统中通过移植第3方软件包 paho mqtt去实现MQTT协议功能,最后会给出测试验证。为后续的物联网项目打好基础。

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

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


3.9.1 MQTT介绍

MQTT 全称为 Message Queuing Telemetry Transport(消息队列遥测传输)是一种基于发布/订阅范式的二进制“轻量级”消息协议,由IB公司发布。针对于网络受限和嵌入式设备而设计的一种数据传输协议。MQTT最大优点在于,可以以极少的代码和有限的带宽,为连接远程设备提供实时可靠的消息服务。作为一种低开销、低带宽占用的即时通讯协议,使其在物联网、小型设备、移动应用等方面有较广泛的应用。MQTT模型如图所示。

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


3.9.2 移植 paho mqtt软件包

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

paho mqtt-c 是基于C语言实现的MQTT客户端,非常适合用在嵌入式设备上。首先下载源码:

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

下载之后解压,会得到这么一个文件夹:


我们在鸿蒙系统源码的 third_party 文件夹下创建一个 pahomqtt 文件夹,然后把解压后的所有文件都拷贝到 pahomqtt 文件夹下,目录结构大致如下:


下一步,我们在pahomqtt 文件夹下面新建BUILD.gn文件,用来构建编译。其内容如下:

# 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" 
 

 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.

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

打开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" 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

 (2)其次修改 transport_sendPacketBuffer 函数,内容修改后如下:

int transport_sendPacketBuffer(int sock, unsigned char* buf, int buflen) 
 

 
int rc = 0; 
 
rc = send(sock, buf, buflen, 0); 
 
return rc; 
 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

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

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; 
 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

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

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

 3.9.3 编写测试代码

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

 (1)BUILD.gn文件内容:

# 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" 
 

 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.

 (2)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" 
 
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); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.

 (3)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" 
 
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(); 
 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.

 mqtt_test.h文件内容:

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

 到这里就完成了代码部分,可以开始编译了。

3.9.4 MQTT实验

这里我们需要先下载一个 Windows电脑端的 MQTT客户端,这样我们就可以用电脑订阅开发板的MQTT主题信息了。

电脑版的mqtt客户端下载链接: https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.ui.app/1.1.1/

我们选择这一个: 

 弄完后打开软件,按图操作: 

 

操作完后,我们把编译后程序烧写到开发板,输入如下串口指令,让开发板连接上网络,因为MQTT功能需要网络支持。输入如下串口指令:

AT+STARTSTA 开启STA模式

AT+CONN="12-203",,2,"07686582488" 连接到路由器,注意wifi热点名和密码用自己的

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

AT+IFCFG 打印查看IP地址

串口指令的应答应该如下:

 

成功连接上路由器后,请确保路由器是可以上网的。

然后我们输入我们的 MQTT测试的AT指令: AT+MQTTTEST

应该可以看到如下打印:


此时我们去查看 我们电脑端的MQTT客户端软件,可以看到右边已经有接收MQTT信息了,主题未 pubtopic,消息内容为 hello HarmonyOS ! ,说明实验成功。


3.9.5 总结

这一次的内容比较多,其中总结起来就4步:

(1)添加第三方软件包 paho mqtt,关于如何添加第3方软件包,我之前有一篇文章已经讲了。

可以参考:如何往鸿蒙系统源码中添加第三方软件包

(2)移植 paho mqtt

(3)编写测试代码,这里我们用的是注册AT指令的方式,方便大家使用AT指令测试。

(4)测试,这里用电脑装mqtt客户端程序,去验证。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

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

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2020-11-18 11:36:35

鸿蒙系统

2020-11-17 08:59:28

MQTT

2022-02-09 19:45:41

MQTTOpenHarmon鸿蒙

2022-09-26 11:30:40

MQTT协议客户端协议

2018-08-17 06:13:16

物联网协议MQTTMQTT-SN

2022-05-17 11:06:52

车联网通信协议MQTT

2021-08-04 10:22:27

鸿蒙HarmonyOS应用

2024-03-26 11:52:13

2023-03-20 16:16:40

MQTT传输协议

2022-06-27 10:41:45

MQTT物联网协议

2022-02-25 07:34:36

MQTT协议RabbitMQ

2023-09-24 23:18:50

2020-11-24 09:52:22

MQTT

2023-09-07 14:59:42

物联网MQTTCoAP

2023-03-13 15:27:48

2021-07-05 22:22:24

协议MQTT

2022-08-30 21:47:03

MQTT ProtoOthers

2016-11-28 14:40:00

MQTT消息协议

2013-04-28 10:29:07

MQTT物联网消息队列遥测传输

2019-12-27 10:42:45

HTTPMQTT物联网
点赞
收藏

51CTO技术栈公众号