From 65de183a078ca8a7f33e1925af4bcad15b7e0e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 11 Aug 2017 23:10:08 +0200 Subject: [PATCH] Added onPublish and onSubscribe callback and onDisconnect reasons to MQTT debug log --- code/espurna/mqtt.ino | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 07795041..21c6c9f1 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -81,11 +81,12 @@ String mqttSubtopic(char * topic) { void mqttSendRaw(const char * topic, const char * message) { if (mqtt.connected()) { - DEBUG_MSG_P(PSTR("[MQTT] Sending %s => %s\n"), topic, message); #if MQTT_USE_ASYNC - mqtt.publish(topic, MQTT_QOS, MQTT_RETAIN, message); + unsigned int packetId = mqtt.publish(topic, MQTT_QOS, MQTT_RETAIN, message); + DEBUG_MSG_P(PSTR("[MQTT] Sending %s => %s (PID %d)\n"), topic, message, packetId); #else mqtt.publish(topic, message, MQTT_RETAIN); + DEBUG_MSG_P(PSTR("[MQTT] Sending %s => %s\n"), topic, message); #endif } } @@ -149,8 +150,13 @@ void mqttSend(const char * topic, unsigned int index, const char * message) { void mqttSubscribeRaw(const char * topic) { if (mqtt.connected() && (strlen(topic) > 0)) { - DEBUG_MSG_P(PSTR("[MQTT] Subscribing to %s\n"), topic); - mqtt.subscribe(topic, MQTT_QOS); + #if MQTT_USE_ASYNC + unsigned int packetId = mqtt.subscribe(topic, MQTT_QOS); + DEBUG_MSG_P(PSTR("[MQTT] Subscribing to %s (PID %d)\n"), topic, packetId); + #else + mqtt.subscribe(topic, MQTT_QOS); + DEBUG_MSG_P(PSTR("[MQTT] Subscribing to %s\n"), topic); + #endif } } @@ -319,11 +325,35 @@ void mqttSetup() { _mqttOnConnect(); }); mqtt.onDisconnect([](AsyncMqttClientDisconnectReason reason) { + if (reason == AsyncMqttClientDisconnectReason::TCP_DISCONNECTED) { + DEBUG_MSG_P(PSTR("[MQTT] TCP Disconnected\n")); + } + if (reason == AsyncMqttClientDisconnectReason::MQTT_IDENTIFIER_REJECTED) { + DEBUG_MSG_P(PSTR("[MQTT] Identifier Rejected\n")); + } + if (reason == AsyncMqttClientDisconnectReason::MQTT_SERVER_UNAVAILABLE) { + DEBUG_MSG_P(PSTR("[MQTT] Server unavailable\n")); + } + if (reason == AsyncMqttClientDisconnectReason::MQTT_MALFORMED_CREDENTIALS) { + DEBUG_MSG_P(PSTR("[MQTT] Malformed credentials\n")); + } + if (reason == AsyncMqttClientDisconnectReason::MQTT_NOT_AUTHORIZED) { + DEBUG_MSG_P(PSTR("[MQTT] Not authorized\n")); + } + if (reason == AsyncMqttClientDisconnectReason::TLS_BAD_FINGERPRINT) { + DEBUG_MSG_P(PSTR("[MQTT] Bad fingerprint\n")); + } _mqttOnDisconnect(); }); mqtt.onMessage([](char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { _mqttOnMessage(topic, payload, len); }); + mqtt.onSubscribe([](uint16_t packetId, uint8_t qos) { + DEBUG_MSG_P(PSTR("[MQTT] Subscribe ACK for PID %d\n"), packetId); + }); + mqtt.onPublish([](uint16_t packetId) { + DEBUG_MSG_P(PSTR("[MQTT] Publish ACK for PID %d\n"), packetId); + }); #else mqtt.setCallback([](char* topic, byte* payload, unsigned int length) { _mqttOnMessage(topic, (char *) payload, length);