OpenHarmony开发11 —— 在Hispark上复现MQTT
OpenHarmony开发11 —— 在Hispark上复现MQTT
- 首先,将BearPI的库函数,导入hispark的
my_first_app
的BUILD.gn
中。
static_library("myapp") {
sources = [
"hello_world.c",
]
include_dirs = [
"//utils/native/lite/include",
"//commonlibrary/utils_lite/include",
"//kernel/liteos_m/components/cmsis/2.0",
"//kernel/liteos_m/components/cmsis/2.0",
"//base/iot_hardware/interfaces/kits/wifiiot_lite",
"//foundation/communication/interfaces/kits/wifi_lite/wifiservice",
"//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/",
"//third_party/cJSON",
"//third_party/paho_mqtt/MQTTPacket/src",
"//third_party/paho_mqtt/MQTTClient-C/src",
"include",
]
}
编译之后,没有问题,说明两个版本OpenHarmony的库函数的路径没有进行调整。
- 将BearPi的主要执行函数
MQTT_demo
进行移植
#include <stdio.h>
#include "ohos_init.h"
#include "ohos_types.h"
static void MQTT_DemoTask(void)
{
}
void HelloWorld(void)
{
osThreadAttr_t attr;
attr.name = "MQTT_DemoTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 10240;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)MQTT_DemoTask, NULL, &attr) == NULL) {
printf("[MQTT_Demo] Falied to create MQTT_DemoTask!\n");
}
}
//SYS_RUN(HelloWorld);
APP_FEATURE_INIT(HelloWorld);
编译之后发现报错error: unknown type name 'osThreadAttr_t'
,而osThreadAttr_t
是OpenHarmony自带的数据类型,说明没有正确的导入库函数
-
参考华为开发者论坛内的"OpenHarmony" v3.1项目结构,和线程相关的一定在
kernel/
,而Hi3861是内存相对不丰富的开发板,所以在文件夹liteos_m
下,在其中寻找cmsis
。最终在
kernel/liteos_m/kal/cmsis/cmsis_os2.h
找到如下内容
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "../../../../third_party/cmsis/CMSIS/RTOS2/Include/cmsis_os2.h"
-
将其他部分都复制粘贴,项目结构如下
-
再次编译,发现缺少
MQTTClient.h
,经过终端查找,我们找到了文件的位置
- 参照上述步骤,不断地更新文件位置,最终确定的
BUILD.gn
如下:
static_library("myapp") {
sources = [
"hello_world.c",
"src/wifi_connect.c",
]
include_dirs = [
"//utils/native/lite/include",
"//commonlibrary/utils_lite/include",
"//base/iot_hardware/interfaces/kits/wifiiot_lite",
"//foundation/communication/wifi_lite/interfaces/wifiservice",
"//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/",
"//third_party/cJSON",
"//third_party/paho_mqtt/MQTTPacket/src",
"//third_party/paho_mqtt/MQTTClient-C/src",
"//third_party/paho_mqtt/MQTTClient-C/src/liteOS",
"//third_party/cmsis/CMSIS/RTOS2/Include",
"include",
]
deps = [
"//third_party/paho_mqtt:pahomqtt_static",
]
}
- 最后的
hello_world.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifi_connect.h"
#include "MQTTClient.h"
#include "MQTTLiteOS.h"
#define CONNECT_SSID ""
#define CONNECT_PASSWORD ""
#define CONNECT_IP ""
#define CONNECT_PORT 1883
static unsigned char sendBuf[1000];
static unsigned char readBuf[1000];
Network network;
void messageArrived(MessageData* data)
{
printf("Message arrived on topic %.*s: %.*s\n", data->topicName->lenstring.len, data->topicName->lenstring.data,
data->message->payloadlen, data->message->payload);
}
/* */
static void MQTT_DemoTask(void)
{
WifiConnect(CONNECT_SSID,CONNECT_PASSWORD);
printf("Starting ...\n");
int rc, count = 0;
MQTTClient client;
NetworkInit(&network);
printf("NetworkConnect ...\n");
begin:
NetworkConnect(&network, CONNECT_IP, CONNECT_PORT);
printf("MQTTClientInit ...\n");
MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));
MQTTString clientId = MQTTString_initializer;
clientId.cstring = "bearpi";
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.clientID = clientId;
data.willFlag = 0;
data.MQTTVersion = 3;
data.keepAliveInterval = 0;
data.cleansession = 1;
printf("MQTTConnect ...\n");
rc = MQTTConnect(&client, &data);
if (rc != 0) {
printf("MQTTConnect: %d\n", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
osDelay(200);
goto begin;
}
printf("MQTTSubscribe ...\n");
rc = MQTTSubscribe(&client, "substopic", 2, messageArrived);
if (rc != 0) {
printf("MQTTSubscribe: %d\n", rc);
osDelay(200);
goto begin;
}
while (++count)
{
MQTTMessage message;
char payload[30];
message.qos = 2;
message.retained = 0;
message.payload = payload;
sprintf(payload, "message number %d", count);
message.payloadlen = strlen(payload);
if ((rc = MQTTPublish(&client, "pubtopic", &message)) != 0){
printf("Return code from MQTT publish is %d\n", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
goto begin;
}
osDelay(50);
}
}
void HelloWorld(void)
{
osThreadAttr_t attr;
attr.name = "MQTT_DemoTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 10240;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)MQTT_DemoTask, NULL, &attr) == NULL) {
printf("[MQTT_Demo] Falied to create MQTT_DemoTask!\n");
}
}
//SYS_RUN(HelloWorld);
APP_FEATURE_INIT(HelloWorld);
- 编译,烧录后,运行
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了