Browse Source

Unsibscribe to all MQTT topics before subscribing to new ones

fastled
Xose Pérez 6 years ago
parent
commit
66d209ab33
1 changed files with 15 additions and 1 deletions
  1. +15
    -1
      code/espurna/mqtt.ino

+ 15
- 1
code/espurna/mqtt.ino View File

@ -180,6 +180,18 @@ void mqttSubscribe(const char * topic) {
mqttSubscribeRaw(path.c_str());
}
void mqttUnsubscribeRaw(const char * topic) {
if (_mqtt.connected() && (strlen(topic) > 0)) {
#if MQTT_USE_ASYNC
unsigned int packetId = _mqtt.unsubscribe(topic);
DEBUG_MSG_P(PSTR("[MQTT] Unsubscribing to %s (PID %d)\n"), topic, packetId);
#else
_mqtt.unsubscribe(topic);
DEBUG_MSG_P(PSTR("[MQTT] Unsubscribing to %s\n"), topic);
#endif
}
}
void mqttRegister(void (*callback)(unsigned int, const char *, const char *)) {
_mqtt_callbacks.push_back(callback);
}
@ -190,7 +202,6 @@ void mqttRegister(void (*callback)(unsigned int, const char *, const char *)) {
void _mqttCallback(unsigned int type, const char * topic, const char * payload) {
if (type == MQTT_CONNECT_EVENT) {
mqttSubscribe(MQTT_TOPIC_ACTION);
@ -226,6 +237,9 @@ void _mqttOnConnect() {
// Send first Heartbeat
heartbeat();
// Clean subscriptions
mqttUnsubscribeRaw("#");
// Send connect event to subscribers
for (unsigned char i = 0; i < _mqtt_callbacks.size(); i++) {
(*_mqtt_callbacks[i])(MQTT_CONNECT_EVENT, NULL, NULL);


Loading…
Cancel
Save