sys_mqtt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-11 21:53:07
  5. * @LastEditTime : 2022-06-15 23:03:30
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "mqtt_config.h"
  11. #include "mqtt_log.h"
  12. #include "sys_mqtt.h"
  13. static void mqtt_publish_task(void *arg);
  14. /*
  15. *接收并处理mqtt订阅消息
  16. */
  17. static void topic1_handler(void *client, message_data_t *msg)
  18. {
  19. (void)client;
  20. MQTT_LOG_I("topic: %s\nmessage:%s", msg->topic_name, (char *)msg->message->payload);
  21. }
  22. /*
  23. *初始化mqtt连接
  24. */
  25. int8_t mqtt_init(mqtt_client_t *client, char *clientId, char *user_name, char *password, char *ip, char *port, uint16_t keepAlive)
  26. {
  27. mqtt_set_client_id(client, clientId);
  28. mqtt_set_port(client, port);
  29. mqtt_set_host(client, ip);
  30. mqtt_set_user_name(client, user_name);
  31. mqtt_set_password(client, password);
  32. mqtt_set_clean_session(client, 1);
  33. mqtt_set_keep_alive_interval(client, keepAlive);
  34. return mqtt_connect(client);
  35. }
  36. // 发送数据队列句柄
  37. QueueHandle_t xQueue1;
  38. void mqtt_task_creat()
  39. {
  40. mqtt_client_t *client = NULL; // 创建一个客户端
  41. mqtt_log_init();
  42. client = mqtt_lease();
  43. printf("\nwelcome to mqttclient test...\n");
  44. xQueue1 = xQueueCreate(10, sizeof(struct Pub_Queue *)); // 创建一个mqtt上传的队列
  45. xTaskCreate(mqtt_publish_task, "mqtt_publish_task", 1024, client, 4, NULL);
  46. }
  47. static void mqtt_publish_task(void *arg)
  48. {
  49. mqtt_client_t *client = (mqtt_client_t *)arg;
  50. while (1)
  51. {
  52. LOG_PRINT(LOG_INFO,"mqtt_start");
  53. // int rc = mqtt_init(client, "client_id_001", NULL, NULL, "36.134.23.11", "1883", 1000);
  54. vTaskDelay(100);
  55. }
  56. mqtt_subscribe(client, "sub_topic_task", QOS0, topic1_handler);
  57. mqtt_message_t msg;
  58. memset(&msg, 0, sizeof(msg));
  59. mqtt_list_subscribe_topic(client);
  60. struct Pub_Queue *pxMessage;
  61. while (1)
  62. {
  63. if (xQueue1 != 0)
  64. {
  65. if (xQueueReceive(xQueue1, &(pxMessage), (TickType_t)10))
  66. {
  67. // 处理队列内部数据并上传
  68. msg.payloadlen = pxMessage->pubLength;
  69. msg.qos = pxMessage->qos;
  70. msg.payload = (void *)pxMessage->message;
  71. mqtt_publish(client, pxMessage->pub_topic, &msg);
  72. vPortFree(pxMessage->message);
  73. vPortFree(pxMessage->pub_topic);
  74. vPortFree(pxMessage);
  75. }
  76. }
  77. vTaskDelay(100);
  78. }
  79. }
  80. /*
  81. * 函数名:void mqtt_publish_data(uint8_t *payload,mqtt_qos_t qos,uint16_t pub_length,char *topic)
  82. * 输入参数:payload上传的数据包,qos以什么等级去发布数据,pub_length上传的数据包长度,topic上传的topic
  83. * 输出参数:无
  84. * 返回值:无
  85. * 函数作用:向队列中写入mqtt上传的消息
  86. * TODO:队列满无法写入的情况处理
  87. */
  88. void mqtt_publish_data(uint8_t *payload, mqtt_qos_t qos, uint16_t pub_length, char *topic)
  89. {
  90. struct Pub_Queue *pxMessage = pvPortMalloc(sizeof(struct Pub_Queue));
  91. pxMessage->message = pvPortMalloc(pub_length + 1);
  92. memset(pxMessage->message, 0, (pub_length + 1));
  93. memcpy(pxMessage->message, payload, pub_length);
  94. pxMessage->pub_topic = pvPortMalloc(strlen(topic) + 1);
  95. memset(pxMessage->pub_topic, 0, (strlen(topic) + 1));
  96. strcpy(pxMessage->pub_topic, topic);
  97. xQueueSend(xQueue1, (void *)&pxMessage, (TickType_t)0);
  98. }