From 215da43a44412cac202b3fc8d9098690b664ec81 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Thu, 10 Sep 2015 22:49:48 -0700 Subject: [PATCH] ensure reconnect rexmits pending messages --- esp-link/mqtt_client.c | 3 +-- esp-link/status.c | 4 +++- mqtt/mqtt.c | 8 +++++--- mqtt/mqtt.h | 2 +- user/user_main.c | 28 ++-------------------------- 5 files changed, 12 insertions(+), 33 deletions(-) diff --git a/esp-link/mqtt_client.c b/esp-link/mqtt_client.c index 8e8c936..229d996 100644 --- a/esp-link/mqtt_client.c +++ b/esp-link/mqtt_client.c @@ -11,8 +11,7 @@ static int once = 0; static void ICACHE_FLASH_ATTR mqttTimerCb(void *arg) { if (once++ > 0) return; MQTT_Init(&mqttClient, flashConfig.mqtt_hostname, flashConfig.mqtt_port, 0, 2, - flashConfig.mqtt_client, flashConfig.mqtt_username, flashConfig.mqtt_password, - 60, 1); + flashConfig.mqtt_client, flashConfig.mqtt_username, flashConfig.mqtt_password, 60); MQTT_Connect(&mqttClient); MQTT_Subscribe(&mqttClient, "system/time", 0); } diff --git a/esp-link/status.c b/esp-link/status.c index 5d9562f..2b08b20 100644 --- a/esp-link/status.c +++ b/esp-link/status.c @@ -95,7 +95,9 @@ static void ICACHE_FLASH_ATTR mqttStatusCb(void *v) { char buf[128]; mqttStatusMsg(buf); - MQTT_Publish(&mqttClient, flashConfig.mqtt_status_topic, buf, 0, 0); + MQTT_Publish(&mqttClient, flashConfig.mqtt_status_topic, buf, 1, 0); + + //espconn_disconnect(mqttClient.pCon); } //===== Init status stuff diff --git a/mqtt/mqtt.c b/mqtt/mqtt.c index f70cda9..f4ca18c 100644 --- a/mqtt/mqtt.c +++ b/mqtt/mqtt.c @@ -325,6 +325,7 @@ mqtt_timer(void* arg) { client->msgQueue = PktBuf_Unshift(client->msgQueue, client->pending_buffer); client->pending_buffer = NULL; } + client->connect_info.clean_session = 0; // ask server to keep state MQTT_Connect(client); } } @@ -607,7 +608,7 @@ MQTT_Subscribe(MQTT_Client* client, char* topic, uint8_t qos) { void ICACHE_FLASH_ATTR MQTT_Init(MQTT_Client* mqttClient, char* host, uint32 port, uint8_t security, uint8_t sendTimeout, char* client_id, char* client_user, char* client_pass, - uint8_t keepAliveTime, uint8_t cleanSession) { + uint8_t keepAliveTime) { os_printf("MQTT_Init\n"); os_memset(mqttClient, 0, sizeof(MQTT_Client)); @@ -633,7 +634,7 @@ MQTT_Init(MQTT_Client* mqttClient, char* host, uint32 port, uint8_t security, ui os_strcpy(mqttClient->connect_info.password, client_pass); mqttClient->connect_info.keepalive = keepAliveTime; - mqttClient->connect_info.clean_session = cleanSession; + mqttClient->connect_info.clean_session = 1; mqttClient->in_buffer = (uint8_t *)os_zalloc(MQTT_MAX_RCV_MESSAGE); mqttClient->in_buffer_size = MQTT_MAX_RCV_MESSAGE; @@ -708,7 +709,6 @@ MQTT_Connect(MQTT_Client* mqttClient) { mqttClient->connState = TCP_CONNECTING; mqttClient->timeoutTick = 20; // generous timeout to allow for DNS, etc mqttClient->sending = FALSE; - mqttClient->msgQueue = NULL; } static void ICACHE_FLASH_ATTR @@ -741,6 +741,8 @@ MQTT_Disconnect(MQTT_Client* mqttClient) { return; } mqtt_doAbort(mqttClient); + //void *out_buffer = mqttClient->mqtt_connection.buffer; + //if (out_buffer != NULL) os_free(out_buffer); mqttClient->connState = MQTT_DISCONNECTED; // ensure we don't automatically reconnect } diff --git a/mqtt/mqtt.h b/mqtt/mqtt.h index 6ca7091..3155dc8 100644 --- a/mqtt/mqtt.h +++ b/mqtt/mqtt.h @@ -96,7 +96,7 @@ typedef struct { void MQTT_Init(MQTT_Client* mqttClient, char* host, uint32 port, uint8_t security, uint8_t sendTimeout, char* client_id, char* client_user, char* client_pass, - uint8_t keepAliveTime, uint8_t cleanSession); + uint8_t keepAliveTime); // Set Last Will Topic on client, must be called before MQTT_InitConnection void MQTT_InitLWT(MQTT_Client* mqttClient, char* will_topic, char* will_msg, diff --git a/user/user_main.c b/user/user_main.c index e5dd622..72608c6 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -2,36 +2,12 @@ #include "cgiwifi.h" #include "mqtt.h" -#if 0 - -MQTT_Client mqttClient; - -static ETSTimer mqttTimer; - -static int once = 0; -static void ICACHE_FLASH_ATTR mqttTimerCb(void *arg) { - if (once++ > 0) return; - MQTT_Init(&mqttClient, "h.voneicken.com", 1883, 0, 2, "test1", "", "", 10, 1); - MQTT_Connect(&mqttClient); - MQTT_Subscribe(&mqttClient, "system/time", 0); -} - -void ICACHE_FLASH_ATTR -wifiStateChangeCb(uint8_t status) -{ - if (status == wifiGotIP) { - os_timer_disarm(&mqttTimer); - os_timer_setfn(&mqttTimer, mqttTimerCb, NULL); - os_timer_arm(&mqttTimer, 15000, 0); - } -} - - // initialize the custom stuff that goes beyond esp-link void app_init() { - wifiAddStateChangeCb(wifiStateChangeCb); + } +#if 0 MQTT_Client mqttClient;