From 323910268106604c4d89bdd3e66a581c72600c33 Mon Sep 17 00:00:00 2001 From: Henrik Ekblad Date: Tue, 10 Nov 2015 00:20:21 +0100 Subject: [PATCH 1/2] Split subscripe/publish MQTT topic Fix fix avoids loopback of messages from controller back to gateway. --- libraries/MySensors/MySensor.h | 7 +++++-- libraries/MySensors/core/MyGatewayTransportMQTTClient.cpp | 8 ++++---- .../GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino | 5 +++-- libraries/MySensors/keywords.txt | 3 ++- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/libraries/MySensors/MySensor.h b/libraries/MySensors/MySensor.h index ac37f38..10d4add 100644 --- a/libraries/MySensors/MySensor.h +++ b/libraries/MySensors/MySensor.h @@ -94,8 +94,11 @@ #if !defined(MY_CONTROLLER_IP_ADDRESS) #error You must specify MY_CONTROLLER_IP_ADDRESS (MQTT broker address) #endif - #if !defined(MY_MQTT_TOPIC_PREFIX) - #error You must specify a topic prefix MY_MQTT_TOPIC_PREFIX for this MQTT client + #if !defined(MY_MQTT_PUBLISH_TOPIC_PREFIX) + #error You must specify a topic publish prefix MY_MQTT_PUBLISH_TOPIC_PREFIX for this MQTT client + #endif + #if !defined(MY_MQTT_SUBSCRIBE_TOPIC_PREFIX) + #error You must specify a topic subscribe prefix MY_MQTT_SUBSCRIBE_TOPIC_PREFIX for this MQTT client #endif #if !defined(MY_MQTT_CLIENT_ID) #error You must define a unique MY_MQTT_CLIENT_ID for this MQTT client diff --git a/libraries/MySensors/core/MyGatewayTransportMQTTClient.cpp b/libraries/MySensors/core/MyGatewayTransportMQTTClient.cpp index 4a4ddc9..6575533 100644 --- a/libraries/MySensors/core/MyGatewayTransportMQTTClient.cpp +++ b/libraries/MySensors/core/MyGatewayTransportMQTTClient.cpp @@ -23,7 +23,7 @@ #include "MyGatewayTransport.h" #include "MyMessage.h" -// Topic structure: MY_MQTT_TOPIC_PREFIX/NODE-ID/SENSOR-ID/CMD-TYPE/ACK-FLAG/SUB-TYPE +// Topic structure: MY_MQTT_PUBLISH_TOPIC_PREFIX/NODE-ID/SENSOR-ID/CMD-TYPE/ACK-FLAG/SUB-TYPE @@ -53,7 +53,7 @@ MyMessage _mqttMsg; bool gatewayTransportSend(MyMessage &message) { if (!_client.connected()) return false; - snprintf_P(_fmtBuffer, MY_GATEWAY_MAX_SEND_LENGTH, PSTR(MY_MQTT_TOPIC_PREFIX "/%d/%d/%d/%d/%d"), message.sender, message.sensor, mGetCommand(message), mGetAck(message), message.type); + snprintf_P(_fmtBuffer, MY_GATEWAY_MAX_SEND_LENGTH, PSTR(MY_MQTT_PUBLISH_TOPIC_PREFIX "/%d/%d/%d/%d/%d"), message.sender, message.sensor, mGetCommand(message), mGetAck(message), message.type); debug(PSTR("Sending message on topic: %s\n"), _fmtBuffer); return _client.publish(_fmtBuffer, message.getString(_convBuffer)); } @@ -71,7 +71,7 @@ void incomingMQTT(char* topic, byte* payload, switch (i) { case 0: { // Topic prefix - if (strcmp_P(str, MY_MQTT_TOPIC_PREFIX) != 0) { + if (strcmp_P(str, MY_MQTT_SUBSCRIBE_TOPIC_PREFIX) != 0) { // Message not for us or malformed! return; } @@ -126,7 +126,7 @@ bool reconnectMQTT() { // Once connected, publish an announcement... //_client.publish("outTopic","hello world"); // ... and resubscribe - _client.subscribe(MY_MQTT_TOPIC_PREFIX "/+/+/+/+/+"); + _client.subscribe(MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "/+/+/+/+/+"); return true; } return false; diff --git a/libraries/MySensors/examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino b/libraries/MySensors/examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino index 4c99eb3..4aeb228 100644 --- a/libraries/MySensors/examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino +++ b/libraries/MySensors/examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino @@ -72,8 +72,9 @@ #define MY_GATEWAY_MQTT_CLIENT -// Set this nodes topic prefix -#define MY_MQTT_TOPIC_PREFIX "mygateway" +// Set this nodes subscripe and publish topic prefix +#define MY_MQTT_PUBLISH_TOPIC_PREFIX "mygateway1-out" +#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "mygateway1-in" // Set MQTT client id #define MY_MQTT_CLIENT_ID "mysensors-1" diff --git a/libraries/MySensors/keywords.txt b/libraries/MySensors/keywords.txt index ce8fc80..6fef79b 100644 --- a/libraries/MySensors/keywords.txt +++ b/libraries/MySensors/keywords.txt @@ -101,6 +101,7 @@ MY_RS485_BAUD_RATE LITERAL1 MY_RS485_MAX_MESSAGE_LENGTH LITERAL1 MY_INCLUSION_BUTTON_EXTERNAL_PULLUP LITERAL1 MY_W5100_SPI_EN LITERAL1 -MY_MQTT_TOPIC_PREFIX LITERAL1 +MY_MQTT_SUBSCRIBE_TOPIC_PREFIX LITERAL1 +MY_MQTT_PUBLISH_TOPIC_PREFIX LITERAL1 MY_MQTT_CLIENT_ID LITERAL1 MY_GATEWAY_MQTT_CLIENT LITERAL1 \ No newline at end of file From ec5eb3a6052d24710702f9154e8fe7b4a56946bf Mon Sep 17 00:00:00 2001 From: Henrik Ekblad Date: Tue, 10 Nov 2015 00:36:05 +0100 Subject: [PATCH 2/2] Update split topic chnages for the W5100 gateway sketch --- .../GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libraries/MySensors/examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino b/libraries/MySensors/examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino index bc90abb..770ed1f 100644 --- a/libraries/MySensors/examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino +++ b/libraries/MySensors/examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino @@ -69,8 +69,9 @@ #define MY_GATEWAY_MQTT_CLIENT -// Set this nodes topic prefix -#define MY_MQTT_TOPIC_PREFIX "mygateway" +// Set this nodes subscripe and publish topic prefix +#define MY_MQTT_PUBLISH_TOPIC_PREFIX "mygateway1-out" +#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "mygateway1-in" // Set MQTT client id #define MY_MQTT_CLIENT_ID "mysensors-1"