diff --git a/include/mqtt_client.h b/include/mqtt_client.h index 8ca3293..f6eb034 100644 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -166,6 +166,7 @@ typedef struct { int refresh_connection_after_ms; /*!< Refresh connection after this value (in milliseconds) */ const struct psk_key_hint* psk_hint_key; /*!< Pointer to PSK struct defined in esp_tls.h to enable PSK authentication (as alternative to certificate verification). If not NULL and server/client certificates are NULL, PSK is enabled */ bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which this bool is set. */ + int reconnect_timeout_ms; /*!< Reconnect to the broker after this value in miliseconds if auto reconnect is not disabled */ } esp_mqtt_client_config_t; /** diff --git a/include/mqtt_config.h b/include/mqtt_config.h index 033fc0a..2a5d0a9 100644 --- a/include/mqtt_config.h +++ b/include/mqtt_config.h @@ -9,7 +9,7 @@ #include "sdkconfig.h" #define MQTT_PROTOCOL_311 CONFIG_MQTT_PROTOCOL_311 -#define MQTT_RECONNECT_TIMEOUT_MS (10*1000) +#define MQTT_RECON_DEFAULT_MS (10*1000) #define MQTT_POLL_READ_TIMEOUT_MS (1000) #if CONFIG_MQTT_BUFFER_SIZE diff --git a/mqtt_client.c b/mqtt_client.c index 19458f7..5ccd1ef 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -77,6 +77,7 @@ typedef struct { void *user_context; int network_timeout_ms; int refresh_connection_after_ms; + int reconnect_timeout_ms; } mqtt_config_storage_t; typedef enum { @@ -245,6 +246,13 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl if (config->disable_auto_reconnect == cfg->auto_reconnect) { cfg->auto_reconnect = !config->disable_auto_reconnect; } + + if (config->reconnect_timeout_ms) { + cfg->reconnect_timeout_ms = config->reconnect_timeout_ms; + } else { + cfg->reconnect_timeout_ms = MQTT_RECON_DEFAULT_MS; + } + MQTT_API_UNLOCK_FROM_OTHER_TASK(client); return ESP_OK; _mqtt_set_config_failed: @@ -353,7 +361,7 @@ static esp_err_t esp_mqtt_connect(esp_mqtt_client_handle_t client, int timeout_m static esp_err_t esp_mqtt_abort_connection(esp_mqtt_client_handle_t client) { esp_transport_close(client->transport); - client->wait_timeout_ms = MQTT_RECONNECT_TIMEOUT_MS; + client->wait_timeout_ms = client->config->reconnect_timeout_ms; client->reconnect_tick = platform_tick_get_ms(); client->state = MQTT_STATE_WAIT_TIMEOUT; ESP_LOGD(TAG, "Reconnect after %d ms", client->wait_timeout_ms); @@ -1379,7 +1387,7 @@ int esp_mqtt_client_publish(esp_mqtt_client_handle_t client, const char *topic, } } - if (qos > 0) { + if (qos > 0) { //Tick is set after transmit to avoid retransmitting too early due slow network speed / big messages outbox_set_tick(client->outbox, pending_msg_id, platform_tick_get_ms()); outbox_set_pending(client->outbox, pending_msg_id, TRANSMITTED);