小凌派鸿蒙社区
直播中

voidpbq

8年用户 131经验值
擅长:嵌入式技术,处理器/DSP
私信 关注

【小凌派RK2206开发板试用体验】4. mqtt发送消息到腾讯云

1 前言
本章介绍下使用mqtt示例发送消息。

2 代码
2.1 示例bug
官方示例:https://gitee.com/Lockzhiner-Ele ... iot_mqtt/iot_mqtt.c
1.png
这里有一个bug,不知道后续有没有改回来,注意一下。
2.2 main.c
  1. /*
  2. * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. *     http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */

  15. #include "los_tick.h"
  16. #include "los_task.h"
  17. #include "los_config.h"
  18. #include "los_interrupt.h"
  19. #include "los_debug.h"
  20. #include "los_compiler.h"
  21. #include "lz_hardware.h"
  22. #include "config_network.h"

  23. #define  MAIN_TAG              "MAIN"
  24. int DeviceManagerStart();
  25. void IotInit(void);
  26. // void NFC_example2();
  27. // void uart_example();
  28. void iot_mqtt_example();
  29. /*****************************************************************************
  30. Function    : main
  31. Description : Main function entry
  32. Input       : None
  33. Output      : None
  34. Return      : None
  35. *****************************************************************************/
  36. LITE_OS_SEC_TEXT_INIT int Main(void)
  37. {
  38.     int ret;
  39.     LZ_HARDWARE_LOGD(MAIN_TAG, "%s: enter ...", __func__);
  40.    
  41.     HalInit();

  42.     ret = LOS_KernelInit();
  43.     if (ret == LOS_OK) {
  44.         IotInit();

  45.         printf("n");
  46.         
  47.         printf("----------- test start -----------n");
  48.         // task_example2();
  49.         // nfc_example2();
  50.         iot_mqtt_example();
  51.         // uart_example();
  52.         printf("----------- test end -----------n");

  53.         printf("n");

  54.         OHOS_SystemInit();
  55.         ClkDevInit();
  56.         /* 开启驱动管理服务 */
  57.         //DeviceManagerStart();
  58.         // ExternalTaskConfigNetwork();
  59.         LZ_HARDWARE_LOGD(MAIN_TAG, "%s: LOS_Start ...", __func__);
  60.         LOS_Start();
  61.     }

  62.     while (1) {
  63.         __asm volatile("wfi");
  64.     }
  65. }
PS:ExternalTaskConfigNetwork需要在main中先注释掉。

2.3 更改wifi账号和密码
lockzhiner-rk2206-openhARMony3.0ltsdevicerockchiprk2206sdk_liteosboardsrcconfig_network.c
2.png

2.4 MQTT账号与密码设置
初始化
  1. typedef struct
  2. {
  3.         /** The eyecatcher for this structure.  must be MQTC. */
  4.         char struct_id[4];
  5.         /** The version number of this structure.  Must be 0 */
  6.         int struct_version;
  7.         /** Version of MQTT to be used.  3 = 3.1 4 = 3.1.1
  8.           */
  9.         unsigned char MQTTVersion;
  10.         MQTTString clientID;
  11.         unsigned short keepAliveInterval;
  12.         unsigned char cleansession;
  13.         unsigned char willFlag;
  14.         MQTTPacket_willOptions will;
  15.         MQTTString username;
  16.         MQTTString password;
  17. } MQTTPacket_connectData;


  18. #define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0,
  19.                 MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
仿照着添加mqtt账号和密码

这里使用的是示例的代码。增加账号和密码即可
  1. begin:
  2.     printf("NetworkConnect...n");
  3.     rc = NetworkConnect(&network, MQTT_HOST, MQTT_PORT);

  4.     printf("MQTTClientInit...n");
  5.     MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));

  6.     clientId.cstring = "RK2206-youkai";

  7.     data.clientID          = clientId;
  8.     data.willFlag          = 0;
  9.     data.MQTTVersion       = 3;
  10.     data.keepAliveInterval = 0;
  11.     data.cleansession      = 1;

  12.     // youkai add
  13.     MQTTString username_t = MQTTString_initializer;
  14.     username_t.cstring = "mqtt_id";
  15.     MQTTString password_t = MQTTString_initializer;
  16.     password_t.cstring = "mqtt_pw";
  17.     data.password = password_t;
  18.     data.username = username_t;

2.5 发送接口
接口比较简单,MQTTPublish用于发送消息,其中IOT_MQTT也就是消息本身,可以注意服务器的接收内容。
  1.     while (1)
  2.     {
  3.         sprintf(payload, "publish test");
  4.         message.qos = 2;
  5.         message.retained = 0;
  6.         message.payload = payload;
  7.         message.payloadlen = strlen(payload);

  8.         if ((rc = MQTTPublish(&client, "IOT_MQTT", &message)) != 0){
  9.             printf("Return code from MQTT publish is %dn", rc);
  10.             NetworkDisconnect(&network);
  11.             MQTTDisconnect(&client);
  12.             goto begin;
  13.         }

  14.         LOS_Msleep(5000);
  15.     }

3 结果
小凌派log
3.png
服务器log
4.png

4 小结
本章测试了官方的mqtt实例,后续需要结合多台设备实现mqtt的收发功能。
腾讯云的mqtt服务器搭建可以参见:https://blog.csdn.net/qq_38091632/article/details/124990369

更多回帖

发帖
×
20
完善资料,
赚取积分