Skip to content

Commit

Permalink
MQTT Client Retain Feature (#793)
Browse files Browse the repository at this point in the history
Optionally allow MQTT client to publish specific messages with
the retain flag set.

default/current behavior should not be impacted.
  • Loading branch information
mannkind authored and henrikekblad committed Mar 21, 2017
1 parent 61a415c commit ef42b3b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions MyConfig.h
Expand Up @@ -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

Expand Down Expand Up @@ -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
8 changes: 7 additions & 1 deletion core/MyGatewayTransportMQTTClient.cpp
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Expand Up @@ -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
Expand Down

0 comments on commit ef42b3b

Please sign in to comment.