From 381b57f9f4c4b3522a0608f551630fd6c346e658 Mon Sep 17 00:00:00 2001 From: Aladdin-Wang <17630350805@163.com> Date: Mon, 13 Apr 2020 22:42:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BD=91=E7=BB=9C=E6=8E=89?= =?UTF-8?q?=E7=BA=BF=E9=87=8D=E8=BF=9E=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../applications/ali_device.c | 40 ++++++++++++++++++- .../applications/main.c | 7 +++- .../stm32f767-fire-challenger/project.uvoptx | 6 +-- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/demo/3_STM32F767_RTThread_TouchGFX/4_rtthread_TouchGFX_iot/stm32f767-fire-challenger/applications/ali_device.c b/demo/3_STM32F767_RTThread_TouchGFX/4_rtthread_TouchGFX_iot/stm32f767-fire-challenger/applications/ali_device.c index 979d095..17b98a5 100644 --- a/demo/3_STM32F767_RTThread_TouchGFX/4_rtthread_TouchGFX_iot/stm32f767-fire-challenger/applications/ali_device.c +++ b/demo/3_STM32F767_RTThread_TouchGFX/4_rtthread_TouchGFX_iot/stm32f767-fire-challenger/applications/ali_device.c @@ -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模块需要进行断电和上电操作 */ + 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); diff --git a/demo/3_STM32F767_RTThread_TouchGFX/4_rtthread_TouchGFX_iot/stm32f767-fire-challenger/applications/main.c b/demo/3_STM32F767_RTThread_TouchGFX/4_rtthread_TouchGFX_iot/stm32f767-fire-challenger/applications/main.c index 89f4d2a..35d53bd 100644 --- a/demo/3_STM32F767_RTThread_TouchGFX/4_rtthread_TouchGFX_iot/stm32f767-fire-challenger/applications/main.c +++ b/demo/3_STM32F767_RTThread_TouchGFX/4_rtthread_TouchGFX_iot/stm32f767-fire-challenger/applications/main.c @@ -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); //获取网卡对象 struct netdev* net = netdev_get_by_name("esp0"); - + netdev_set_status_callback(net,netdev_callback); //阻塞判断当前网络是否正常连接 while(netdev_is_internet_up(net) != 1) { rt_thread_mdelay(200); } + //提示当前网络已就绪 rt_kprintf("network is ok!\n"); diff --git a/demo/3_STM32F767_RTThread_TouchGFX/4_rtthread_TouchGFX_iot/stm32f767-fire-challenger/project.uvoptx b/demo/3_STM32F767_RTThread_TouchGFX/4_rtthread_TouchGFX_iot/stm32f767-fire-challenger/project.uvoptx index 5de14db..263e503 100644 --- a/demo/3_STM32F767_RTThread_TouchGFX/4_rtthread_TouchGFX_iot/stm32f767-fire-challenger/project.uvoptx +++ b/demo/3_STM32F767_RTThread_TouchGFX/4_rtthread_TouchGFX_iot/stm32f767-fire-challenger/project.uvoptx @@ -420,7 +420,7 @@ Drivers - 1 + 0 0 0 0 @@ -560,7 +560,7 @@ TouchGFX_app - 1 + 0 0 0 0 @@ -912,7 +912,7 @@ TouchGFX_gui - 1 + 0 0 0 0