Browse Source

Skip retained messages

fastled
Xose Pérez 8 years ago
parent
commit
1fc8fd0dfa
2 changed files with 19 additions and 1 deletions
  1. +2
    -0
      code/src/config/general.h
  2. +17
    -1
      code/src/mqtt.ino

+ 2
- 0
code/src/config/general.h View File

@ -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"


+ 17
- 1
code/src/mqtt.ino View File

@ -15,6 +15,9 @@ AsyncMqttClient mqtt;
String mqttTopic;
std::vector<void (*)(unsigned int, const char *, const char *)> _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


Loading…
Cancel
Save