From 1fc8fd0dfaf5c2dee4a21f7669db598970d81284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 27 Dec 2016 05:00:11 +0100 Subject: [PATCH] Skip retained messages --- code/src/config/general.h | 2 ++ code/src/mqtt.ino | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/code/src/config/general.h b/code/src/config/general.h index c40a00c4..42671b26 100644 --- a/code/src/config/general.h +++ b/code/src/config/general.h @@ -67,6 +67,8 @@ #define MQTT_QOS 0 #define MQTT_KEEPALIVE 30 #define MQTT_RECONNECT_DELAY 10000 +#define MQTT_SKIP_RETAINED 1 +#define MQTT_SKIP_TIME 1000 #define MQTT_RELAY_TOPIC "/relay" #define MQTT_LED_TOPIC "/led" #define MQTT_IP_TOPIC "/ip" diff --git a/code/src/mqtt.ino b/code/src/mqtt.ino index d69b5d82..38274737 100644 --- a/code/src/mqtt.ino +++ b/code/src/mqtt.ino @@ -15,6 +15,9 @@ AsyncMqttClient mqtt; String mqttTopic; std::vector _mqtt_callbacks; +#if MQTT_SKIP_RETAINED + unsigned long mqttConnectedAt = 0; +#endif // ----------------------------------------------------------------------------- // MQTT @@ -55,6 +58,10 @@ void _mqttOnConnect(bool sessionPresent) { DEBUG_MSG("[MQTT] Connected!\n"); + #if MQTT_SKIP_RETAINED + mqttConnectedAt = millis(); + #endif + // Build MQTT topics buildTopics(); mqtt.setWill((mqttTopic + MQTT_HEARTBEAT_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, (char *) "0"); @@ -86,7 +93,16 @@ void _mqttOnDisconnect(AsyncMqttClientDisconnectReason reason) { void _mqttOnMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { - DEBUG_MSG("[MQTT] Received %s %c\n", topic, payload[0]); + DEBUG_MSG("[MQTT] Received %s %c", topic, payload[0]); + + #if MQTT_SKIP_RETAINED + if (millis() - mqttConnectedAt < MQTT_SKIP_TIME) { + DEBUG_MSG(" - SKIPPED\n"); + return; + } + #endif + + DEBUG_MSG("\n"); // Send message event to subscribers // Topic is set to the specific part each one might be checking