增加网络掉线重连功能

This commit is contained in:
Aladdin-Wang 2020-04-13 22:42:30 +08:00
parent e68c490d1f
commit 381b57f9f4
3 changed files with 47 additions and 6 deletions

View File

@ -31,6 +31,7 @@ static void *mqtt_client_hd = RT_NULL;
static char *topic_buff = RT_NULL;
static uint32_t mqtt_period_cnt = 0;
static uint32_t mqtt_packet_id = 1;
static uint8_t is_mqtt_disconnect = 1;
static void ali_mqtt_rgb_ctrl_msg_arrive(void *pcontext, void *handle, iotx_mqtt_event_msg_pt msg);
static void ali_mqtt_property_post_msg_arrive(void *pcontext, void *handle, iotx_mqtt_event_msg_pt msg);
extern sht3x_device_t aliget_sht30_singleshot(void);
@ -184,6 +185,7 @@ static void ali_mqtt_event_handle(void *pcontext, void *pclient, iotx_mqtt_event
LOG_D("undefined event occur.");
break;
case IOTX_MQTT_EVENT_DISCONNECT:
is_mqtt_disconnect = 1;
LOG_I("MQTT disconnect.");
break;
case IOTX_MQTT_EVENT_RECONNECT:
@ -363,7 +365,39 @@ static void mqtt_thread_main_thread(void *arg)
}
}
static void mqtt_connect_check_thread(void *arg)
{
struct netdev *netdev_link;
while (1)
{
if ((is_mqtt_disconnect) && (mqtt_client_hd != RT_NULL))
{
if (IOT_MQTT_CheckStateNormal(mqtt_client_hd) <= 0)
{
if (is_mqtt_disconnect >= 20)
{
netdev_link = netdev_get_first_by_flags(NETDEV_FLAG_LINK_UP);
if (!rt_strcmp(netdev_link->name, "esp0"))
{
/* GPRSģ<53><C4A3><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>жϵ<D0B6><CFB5><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD> */
netdev_link->ops->set_down(netdev_link);
rt_thread_mdelay(rt_tick_from_millisecond(RT_TICK_PER_SECOND));
netdev_link->ops->set_up(netdev_link);
}
/* next period to check */
is_mqtt_disconnect = 1;
}
else
is_mqtt_disconnect++;
}
else
{
is_mqtt_disconnect = 0;
}
}
rt_thread_mdelay(rt_tick_from_millisecond(RT_TICK_PER_SECOND * 3));
}
}
static int ali_mqtt_init(void)
{
rt_thread_t tid;
@ -371,7 +405,9 @@ static int ali_mqtt_init(void)
tid = rt_thread_create("ali.main", mqtt_thread_main_thread, RT_NULL, 6 * 1024, RT_THREAD_PRIORITY_MAX / 2, 10);
if (tid != RT_NULL)
rt_thread_startup(tid);
tid = rt_thread_create("mqtt.chk", mqtt_connect_check_thread, RT_NULL, 512, RT_THREAD_PRIORITY_MAX / 2 + 1, 10);
if (tid != RT_NULL)
rt_thread_startup(tid);
return 0;
}
//INIT_APP_EXPORT(ali_mqtt_init);

View File

@ -18,6 +18,10 @@
/* defined the LED0 pin: PH10 */
#define LED0_PIN GET_PIN(H, 10)
void netdev_callback(struct netdev *netdev, enum netdev_cb_type type)
{
rt_kprintf("IOT_MQTT_netdev_cb_type=%d\r\n", type);
}
int main(void)
{
int count = 1;
@ -27,12 +31,13 @@ int main(void)
rt_pin_write(LED0_PIN, PIN_HIGH);
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct netdev* net = netdev_get_by_name("esp0");
netdev_set_status_callback(net,netdev_callback);
//<2F><><EFBFBD><EFBFBD><EFBFBD>жϵ<D0B6>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(netdev_is_internet_up(net) != 1)
{
rt_thread_mdelay(200);
}
//<2F><>ʾ<EFBFBD><CABE>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD>
rt_kprintf("network is ok!\n");

View File

@ -420,7 +420,7 @@
<Group>
<GroupName>Drivers</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
@ -560,7 +560,7 @@
<Group>
<GroupName>TouchGFX_app</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
@ -912,7 +912,7 @@
<Group>
<GroupName>TouchGFX_gui</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>