diff --git a/MyConfig.h b/MyConfig.h index cfba68f..512d193 100644 --- a/MyConfig.h +++ b/MyConfig.h @@ -805,6 +805,12 @@ #endif #endif +/** + * @def MY_MQTT_CLIENT_PUBLISH_RETAIN + * @brief Enables MQTT client to set the retain flag when publishing specific messages. + */ +//#define MY_MQTT_CLIENT_PUBLISH_RETAIN + // Static ip address of gateway (if this is disabled, DHCP will be used) //#define MY_IP_ADDRESS 192,168,178,66 @@ -982,4 +988,5 @@ #define MY_RFM95_ATC_MODE_DISABLED #define MY_RFM95_RST_PIN #define MY_INDICATION_HANDLER +#define MY_MQTT_CLIENT_PUBLISH_RETAIN #endif diff --git a/core/MyGatewayTransportMQTTClient.cpp b/core/MyGatewayTransportMQTTClient.cpp index 0795e47..23ffc0e 100644 --- a/core/MyGatewayTransportMQTTClient.cpp +++ b/core/MyGatewayTransportMQTTClient.cpp @@ -56,7 +56,13 @@ bool gatewayTransportSend(MyMessage &message) setIndication(INDICATION_GW_TX); char *topic = protocolFormatMQTTTopic(MY_MQTT_PUBLISH_TOPIC_PREFIX, message); debug(PSTR("Sending message on topic: %s\n"), topic); - return _MQTT_client.publish(topic, message.getString(_convBuffer)); + #ifdef MY_MQTT_CLIENT_PUBLISH_RETAIN + bool retain = mGetCommand(message) == C_SET || + (mGetCommand(message) == C_INTERNAL && message.type == I_BATTERY_LEVEL); + #else + bool retain = false; + #endif + return _MQTT_client.publish(topic, message.getString(_convBuffer), retain); } void incomingMQTT(char* topic, uint8_t* payload, unsigned int length) diff --git a/keywords.txt b/keywords.txt index b39e424..bb195e9 100644 --- a/keywords.txt +++ b/keywords.txt @@ -130,6 +130,7 @@ MY_W5100_SPI_EN LITERAL1 MY_MQTT_SUBSCRIBE_TOPIC_PREFIX LITERAL1 MY_MQTT_PUBLISH_TOPIC_PREFIX LITERAL1 MY_MQTT_CLIENT_ID LITERAL1 +MY_MQTT_CLIENT_PUBLISH_RETAIN LITERAL1 MY_GATEWAY_MQTT_CLIENT LITERAL1 MY_DISABLE_REMOTE_RESET LITERAL1 MY_RS485_DE_PIN LITERAL1