OpenHarmony开发11 —— 在Hispark上复现MQTT

OpenHarmony开发11 —— 在Hispark上复现MQTT

  1. 首先,将BearPI的库函数,导入hisparkmy_first_appBUILD.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的库函数的路径没有进行调整。

  1. 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自带的数据类型,说明没有正确的导入库函数

  1. 参考华为开发者论坛内的"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"
  1. 将其他部分都复制粘贴,项目结构如下

    image

  2. 再次编译,发现缺少MQTTClient.h,经过终端查找,我们找到了文件的位置

image

  1. 参照上述步骤,不断地更新文件位置,最终确定的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",
    ]
    
}
  1. 最后的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);
  1. 编译,烧录后,运行

image

wifi能成功连接

image

成功连接MQTT服务

image

能不断收到主题为pubtopic的消息

image

image

可以订阅主题:substopic
posted @   ZzTzZ  阅读(284)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示