From 9f6d10a69c86852822855c264c08e1ce7ba2e808 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 27 May 2018 23:00:37 +0200 Subject: wip --- main/main.cpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index ffccc99..9f864cb 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -40,7 +40,8 @@ extern "C" void user_init(); enum events { - EVENTS_GOT_IP = (1 << 0) + EVENTS_GOT_IP = (1 << 0), + EVENTS_LOST_IP = (1 << 1), }; struct config { @@ -287,7 +288,9 @@ void wifi_event_handler_cb(System_Event_t *event) } else if (event->event_id == EVENT_STAMODE_CONNECTED) { os_printf("STA CONNECTED\n"); } else if (event->event_id == EVENT_STAMODE_DISCONNECTED) { - os_printf("STA DISCONNECTED\n"); + if (main_task_handle) { + xTaskNotify(main_task_handle, EVENTS_LOST_IP, eSetBits); + } wifi_station_connect(); } else { os_printf("Unknown event\n"); @@ -315,12 +318,14 @@ static MQTTClient mqtt_client; static Network mqtt_network; unsigned char mqtt_tx_buf[80], mqtt_rx_buf[80]; -static int connect_mqtt(struct config &config) { +static int mqtt_connect(struct config &config) { int ret; MQTTPacket_connectData cd = MQTTPacket_connectData_initializer; NetworkInit(&mqtt_network); - MQTTClientInit(&mqtt_client, &mqtt_network, 30000, mqtt_tx_buf, sizeof(mqtt_tx_buf), mqtt_rx_buf, sizeof(mqtt_rx_buf)); + + unsigned int command_timeout_ms = 30000; + MQTTClientInit(&mqtt_client, &mqtt_network, command_timeout_ms, mqtt_tx_buf, sizeof(mqtt_tx_buf), mqtt_rx_buf, sizeof(mqtt_rx_buf)); if ((ret = NetworkConnect(&mqtt_network, config.mqtt_host, config.mqtt_port)) != 0) { printf("%s: NetworkConnect:%d\n", __FUNCTION__, ret); @@ -351,6 +356,12 @@ fail: return ret; } +static int mqtt_disconnect(struct config &config) { + printf("%s\n", __FUNCTION__); + + return 0; +} + static void set_station_mode(struct config &config) { printf("STATION_MODE\n"); if (wifi_get_opmode_default() != NULL_MODE) { @@ -383,7 +394,11 @@ void main_task(void* ctx) { struct ip_info info; wifi_get_ip_info(STATION_IF, &info); printf("ip=" IPSTR ", nm=" IPSTR ", gw=" IPSTR "\n", IP2STR(&info.ip), IP2STR(&info.netmask), IP2STR(&info.gw)); - connect_mqtt(main_config); + mqtt_connect(main_config); + } + if (notification_value & EVENTS_LOST_IP) { + mqtt_disconnect(main_config); + // TODO: start timer to reconnect to mqtt } if (timeout) { -- cgit v1.2.3