mqtt应用学习

一 网络连接状态:

如果是无线(如esp32的WIFI_AT)初始化网络成功连接后Connect_Status=1,然后在mqtt开始前进行超时判断while(Connect_Status==0){deley(2s)}

二 以umqtt为例:

2.0配置mqtt_client参数进行连接

 1 int umqtt_ex_start(void)
 2 {
 3     LOG_D(" umqtt example start!");
 4     struct umqtt_info umqtt_info = { 0 };
 5     umqtt_info.uri = MQTT_URI;
 6     umqtt_info.client_id="RTU00002";
 7     umqtt_info.reconnect_interval=2;
 8 
 9     m_umqtt_client = umqtt_create(&umqtt_info);/*用umqtt_info配置mqtt连接客户端参数*/
10     if (m_umqtt_client == RT_NULL)
11     {
12        LOG_E(" umqtt client create failed!");
13        return -1;
14     }
15     umqtt_control(m_umqtt_client, UMQTT_CMD_EVT_CB, user_callback);
16 
17     if (umqtt_start(m_umqtt_client) >= 0)
18     {
19        LOG_I(" umqtt start success!");
20        is_started = 1;
21        return 0;
22     }
23     else
24     {
25        m_umqtt_client = RT_NULL;
26        LOG_E(" umqtt start failed!");
27        is_started = 0;
28        return -1;
29     }       
30 }
View Code

 

2.1周期发布

 1  static void app_mqtt_thread_entry(void *parameter)
 2 {   
 3     while(connected==0)
 4     {
 5         rt_thread_mdelay(3000);
 6     }
 7     umqtt_ex_start();   
 8     while(1)
 9     {
10         umqtt_ex_publish();/*周期发布*/
11         rt_thread_mdelay(1000);
12     }
13 }
View Code

2.2

posted on 2022-09-16 18:03  杰瑞鼠  阅读(38)  评论(0编辑  收藏  举报