diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 48953e94..3faf1945 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -204,8 +204,8 @@ String mqttTopic(const char * magnitude, unsigned int index, bool is_set); String mqttMagnitude(char * topic); -void mqttSendRaw(const char * topic, const char * message, bool retain); -void mqttSendRaw(const char * topic, const char * message); +bool mqttSendRaw(const char * topic, const char * message, bool retain); +bool mqttSendRaw(const char * topic, const char * message); void mqttSend(const char * topic, const char * message, bool force, bool retain); void mqttSend(const char * topic, const char * message, bool force); diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index d5777e0b..54b01a1d 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -640,25 +640,34 @@ String mqttTopic(const char * magnitude, unsigned int index, bool is_set) { // ----------------------------------------------------------------------------- -void mqttSendRaw(const char * topic, const char * message, bool retain) { +bool mqttSendRaw(const char * topic, const char * message, bool retain) { - if (_mqtt.connected()) { + if (!_mqtt.connected()) return false; + + const unsigned int packetId( #if MQTT_LIBRARY == MQTT_LIBRARY_ASYNCMQTTCLIENT - unsigned int packetId = _mqtt.publish(topic, _mqtt_qos, retain, message); - DEBUG_MSG_P(PSTR("[MQTT] Sending %s => %s (PID %d)\n"), topic, message, packetId); + _mqtt.publish(topic, _mqtt_qos, retain, message) #elif MQTT_LIBRARY == MQTT_LIBRARY_ARDUINOMQTT - _mqtt.publish(topic, message, retain, _mqtt_qos); - DEBUG_MSG_P(PSTR("[MQTT] Sending %s => %s\n"), topic, message); + _mqtt.publish(topic, message, retain, _mqtt_qos) #elif MQTT_LIBRARY == MQTT_LIBRARY_PUBSUBCLIENT - _mqtt.publish(topic, message, retain); - DEBUG_MSG_P(PSTR("[MQTT] Sending %s => %s\n"), topic, message); + _mqtt.publish(topic, message, retain) #endif + ); + + const size_t message_len = strlen(message); + if (message_len > 128) { + DEBUG_MSG_P(PSTR("[MQTT] Sending %s => (%u bytes) (PID %u)\n"), topic, message_len, packetId); + } else { + DEBUG_MSG_P(PSTR("[MQTT] Sending %s => %s (PID %u)\n"), topic, message, packetId); } + + return (packetId > 0); + } -void mqttSendRaw(const char * topic, const char * message) { - mqttSendRaw (topic, message, _mqtt_retain); +bool mqttSendRaw(const char * topic, const char * message) { + return mqttSendRaw (topic, message, _mqtt_retain); } void mqttSend(const char * topic, const char * message, bool force, bool retain) {