当前位置:网站首页>【RK2206】4. Mqtt example
【RK2206】4. Mqtt example
2022-06-09 19:55:00 【Birds hate fish】
1 Preface
This chapter describes the use of mqtt Example send message .
2 Code
2.1 Example bug
The official sample :https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk2206-openharmony3.0lts/blob/master/vendor/lockzhiner/rk2206/samples/d1_iot_mqtt/iot_mqtt.c

Here's one bug, I wonder if it has been changed back in the future , Pay attention to the .
2.2 main.c
/* * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved. * 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. */
#include "los_tick.h"
#include "los_task.h"
#include "los_config.h"
#include "los_interrupt.h"
#include "los_debug.h"
#include "los_compiler.h"
#include "lz_hardware.h"
#include "config_network.h"
#define MAIN_TAG "MAIN"
int DeviceManagerStart();
void IotInit(void);
// void nfc_example2();
// void uart_example();
void iot_mqtt_example();
/***************************************************************************** Function : main Description : Main function entry Input : None Output : None Return : None *****************************************************************************/
LITE_OS_SEC_TEXT_INIT int Main(void)
{
int ret;
LZ_HARDWARE_LOGD(MAIN_TAG, "%s: enter ...", __func__);
HalInit();
ret = LOS_KernelInit();
if (ret == LOS_OK) {
IotInit();
printf("\n");
printf("----------- test start -----------\n");
// task_example2();
// nfc_example2();
iot_mqtt_example();
// uart_example();
printf("----------- test end -----------\n");
printf("\n");
OHOS_SystemInit();
ClkDevInit();
/* Start the driver management service */
//DeviceManagerStart();
// ExternalTaskConfigNetwork();
LZ_HARDWARE_LOGD(MAIN_TAG, "%s: LOS_Start ...", __func__);
LOS_Start();
}
while (1) {
__asm volatile("wfi");
}
}
PS:ExternalTaskConfigNetwork Need to be in main Note out .
2.3 change wifi Account and password
lockzhiner-rk2206-openharmony3.0lts\device\rockchip\rk2206\sdk_liteos\board\src\config_network.c

2.4 MQTT Account and password settings
initialization
typedef struct
{
/** The eyecatcher for this structure. must be MQTC. */
char struct_id[4];
/** The version number of this structure. Must be 0 */
int struct_version;
/** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1 */
unsigned char MQTTVersion;
MQTTString clientID;
unsigned short keepAliveInterval;
unsigned char cleansession;
unsigned char willFlag;
MQTTPacket_willOptions will;
MQTTString username;
MQTTString password;
} MQTTPacket_connectData;
#define MQTTPacket_connectData_initializer {
{
'M', 'Q', 'T', 'C'}, 0, 4, {
NULL, {
0, NULL}}, 60, 1, 0, \ MQTTPacket_willOptions_initializer, {
NULL, {
0, NULL}}, {
NULL, {
0, NULL}} }
Copy and add mqtt Account and password
Here is the example code . Add an account and password
begin:
printf("NetworkConnect...\n");
rc = NetworkConnect(&network, MQTT_HOST, MQTT_PORT);
printf("MQTTClientInit...\n");
MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));
clientId.cstring = "RK2206-youkai";
data.clientID = clientId;
data.willFlag = 0;
data.MQTTVersion = 3;
data.keepAliveInterval = 0;
data.cleansession = 1;
// youkai add
MQTTString username_t = MQTTString_initializer;
username_t.cstring = "mqtt_id";
MQTTString password_t = MQTTString_initializer;
password_t.cstring = "mqtt_pw";
data.password = password_t;
data.username = username_t;
2.5 Send interface
while (1)
{
sprintf(payload, "publish test");
message.qos = 2;
message.retained = 0;
message.payload = payload;
message.payloadlen = strlen(payload);
if ((rc = MQTTPublish(&client, "IOT_MQTT", &message)) != 0){
printf("Return code from MQTT publish is %d\n", rc);
NetworkDisconnect(&network);
MQTTDisconnect(&client);
goto begin;
}
LOS_Msleep(5000);
}
The interface is relatively simple ,MQTTPublish For sending messages , among IOT_MQTT That is, the message itself , You can pay attention to the contents received by the server .
3 result
Xiaoling sect log
The server log
边栏推荐
- Use the code migration tool to migrate the source code to Kunpeng processor
- Sqlsugar help class
- 高通:将坚持多元化的代工战略,关注英特尔代工商务合作条件
- 2018年全国职业院校技能大赛中职组“网络空间安全”正式赛卷及评分标准
- SSM驾校管理系统
- 护网面试(杂)
- 浅谈Go语言反射
- Shell script installing Prometheus and node_ exporter
- Fast finding the number of nodes in a complete binary tree
- Xcode 14 带来全新提升,Xcode Cloud 正式推出!
猜你喜欢

2022 "network security" competition of secondary vocational group freshmen test of Shandong Zibo Industrial school competition assignment

Set creation and traversal methods

mysql数据类型

The number of developers soared by 1.8 million in 24 months, and rust ushered in a highlight moment

SMART PLC多次调用同一个子程序(FC)

范伟OA8前台SQL注入

What operation and maintenance software and tools will be used for online operation and maintenance?

护网面试(杂)

2022 XX information security management and evaluation competition

Visual display of cool 3D charts
随机推荐
paramiko和线程池Demo(快速批量操作服务器)
Mobile application testing (6) - App Testing Technology (4)
Electron FAQ 60 - error: could not start audio source
Unity将Project升级至URP
C语言实现电脑自动关机程序--可以用来恶搞舍友电脑
2020年“磐云杯”网络空间安全技能竞赛全国拉赛
【RK2206】4. MQTT示例
Odoo passed ir model. access. CSV modify other module permissions
Leetcode 1984. 學生分數的最小差值(可以,已解决)
MySQL optimization tutorial: slow query log practice
驱动开发—基础
Set creation and traversal methods
普通三进制逻辑数学世界先驱者——伍耀晖先生面向全球公布普通三进制逻辑数学全部真值表科研成果(伍氏定律)
Tidb single machine and cluster environment installation, single machine rapid experience
被年轻人冷落的小龙虾,只剩亏钱引流的命?
ASP Err. Detailed description of number error
官宣!博通将以610亿美元收购VMware,并承担80亿美元债务
Anlu technology released new SF1 series fpsoc products: high integration and low power consumption, helping to realize a variety of application scenarios
Logback日志配置文件添加颜色区分
马斯克怎么成了币圈靳东?