sys_mqtt.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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("-----------------------------------------------------------------------------------");
  21. MQTT_LOG_I("%s:%d %s()...\ntopic: %s\nmessage:%s", __FILE__, __LINE__, __FUNCTION__, msg->topic_name, (char*)msg->message->payload);
  22. MQTT_LOG_I("-----------------------------------------------------------------------------------");
  23. }
  24. /*
  25. *初始化mqtt连接
  26. */
  27. int8_t mqtt_init(mqtt_client_t *client,char *clientId,char *user_name,char *password,char *ip,char *port,uint16_t keepAlive)
  28. {
  29. mqtt_set_client_id(client,clientId);
  30. mqtt_set_port(client, port);
  31. mqtt_set_host(client, ip);
  32. mqtt_set_user_name(client, user_name);
  33. mqtt_set_password(client, password);
  34. mqtt_set_clean_session(client, 1);
  35. mqtt_set_keep_alive_interval(client,keepAlive);
  36. return mqtt_connect(client);
  37. }
  38. //发送数据队列句柄
  39. QueueHandle_t xQueue1;
  40. void mqtt_task_creat()
  41. {
  42. mqtt_client_t *client = NULL;//创建一个客户端
  43. mqtt_log_init();
  44. client = mqtt_lease();
  45. printf("\nwelcome to mqttclient test...\n");
  46. xQueue1=xQueueCreate(10,sizeof( struct Pub_Queue * )); //创建一个mqtt上传的队列
  47. xTaskCreate(mqtt_publish_task, "mqtt_publish_task",1024, client, 2, NULL);
  48. }
  49. static void mqtt_publish_task(void *arg)
  50. {
  51. mqtt_client_t *client = (mqtt_client_t *)arg;
  52. while(1)
  53. {
  54. int rc=mqtt_init(client,"client_id_001",NULL,NULL,"36.134.23.11","1883",1000);
  55. vTaskDelay(100);
  56. }
  57. mqtt_subscribe(client,"sub_topic_task", QOS0, topic1_handler);
  58. mqtt_message_t msg;
  59. memset(&msg, 0, sizeof(msg));
  60. mqtt_list_subscribe_topic(client);
  61. struct Pub_Queue *pxMessage;
  62. while(1) {
  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. }