Browse Source

Subscribe and publish to same topic, avoid recursion

fastled
Xose Pérez 8 years ago
parent
commit
8c67d7210f
2 changed files with 32 additions and 26 deletions
  1. +10
    -10
      code/data/index.html
  2. +22
    -16
      code/src/code.ino

+ 10
- 10
code/data/index.html View File

@ -111,37 +111,37 @@
</div> </div>
<div> <div>
<label class="desc" for="mqtt_server">MQTT Server</label>
<label class="desc" for="mqttServer">MQTT Server</label>
<div> <div>
<input id="mqtt_server" name="mqtt_server" type="text" class="field text fn" value="{mqtt_server}" size="8" tabindex="7">
<input id="mqttServer" name="mqttServer" type="text" class="field text fn" value="{mqttServer}" size="8" tabindex="7">
</div> </div>
</div> </div>
<div> <div>
<label class="desc" for="mqtt_port">MQTT Port</label>
<label class="desc" for="mqttPort">MQTT Port</label>
<div> <div>
<input id="mqtt_port" name="mqtt_port" type="text" spellcheck="false" value="{mqtt_port}" maxlength="255" tabindex="8">
<input id="mqttPort" name="mqttPort" type="text" spellcheck="false" value="{mqttPort}" maxlength="255" tabindex="8">
</div> </div>
</div> </div>
<div> <div>
<label class="desc" for="mqtt_topic">MQTT Topic</label>
<label class="desc" for="mqttTopic">MQTT Topic</label>
<div> <div>
<input id="mqtt_topic" name="mqtt_topic" type="text" class="field text fn" value="{mqtt_topic}" size="8" tabindex="9">
<input id="mqttTopic" name="mqttTopic" type="text" class="field text fn" value="{mqttTopic}" size="8" tabindex="9">
</div> </div>
</div> </div>
<div> <div>
<label class="desc" for="mqtt_port">RF Channel</label>
<label class="desc" for="rfChannel">RF Channel</label>
<div> <div>
<input id="rf_channel" name="rf_channel" type="text" spellcheck="false" value="{rf_channel}" maxlength="255" tabindex="8">
<input id="rfChannel" name="rfChannel" type="text" spellcheck="false" value="{rfChannel}" maxlength="255" tabindex="10">
</div> </div>
</div> </div>
<div> <div>
<label class="desc" for="mqtt_topic">RF Device</label>
<label class="desc" for="rfDevice">RF Device</label>
<div> <div>
<input id="rf_device" name="rf_device" type="text" class="field text fn" value="{rf_device}" size="8" tabindex="9">
<input id="rfDevice" name="rfDevice" type="text" class="field text fn" value="{rfDevice}" size="8" tabindex="11">
</div> </div>
</div> </div>


+ 22
- 16
code/src/code.ino View File

@ -83,11 +83,11 @@ DebounceEvent button1 = false;
WiFiClient client; WiFiClient client;
PubSubClient mqtt(client); PubSubClient mqtt(client);
String mqttServer = "192.168.1.100"; String mqttServer = "192.168.1.100";
String mqttBaseTopic = "/test/switch/{identifier}";
String mqttTopic = "/test/switch/{identifier}";
String mqttPort = "1883"; String mqttPort = "1883";
char mqttStatusSetTopic[30];
char mqttStatusTopic[30]; char mqttStatusTopic[30];
char mqttIPTopic[30]; char mqttIPTopic[30];
bool isMQTTMessage = false;
#endif #endif
#if ENABLE_RF #if ENABLE_RF
@ -108,7 +108,7 @@ void switchRelayOn() {
Serial.println("Turning the relay ON"); Serial.println("Turning the relay ON");
#endif #endif
#if ENABLE_MQTT #if ENABLE_MQTT
if (mqtt.connected()) {
if (!isMQTTMessage && mqtt.connected()) {
mqtt.publish(mqttStatusTopic, "1", MQTT_RETAIN); mqtt.publish(mqttStatusTopic, "1", MQTT_RETAIN);
} }
#endif #endif
@ -122,7 +122,7 @@ void switchRelayOff() {
Serial.println("Turning the relay OFF"); Serial.println("Turning the relay OFF");
#endif #endif
#if ENABLE_MQTT #if ENABLE_MQTT
if (mqtt.connected()) {
if (!isMQTTMessage && mqtt.connected()) {
mqtt.publish(mqttStatusTopic, "0", MQTT_RETAIN); mqtt.publish(mqttStatusTopic, "0", MQTT_RETAIN);
} }
#endif #endif
@ -332,7 +332,7 @@ void wifiLoop() {
#if ENABLE_MQTT #if ENABLE_MQTT
content.replace("{mqttServer}", mqttServer); content.replace("{mqttServer}", mqttServer);
content.replace("{mqttPort}", mqttPort); content.replace("{mqttPort}", mqttPort);
content.replace("{mqttBaseTopic}", mqttBaseTopic);
content.replace("{mqttTopic}", mqttTopic);
#endif #endif
#if ENABLE_RF #if ENABLE_RF
content.replace("{rfChannel}", rfChannel); content.replace("{rfChannel}", rfChannel);
@ -360,7 +360,7 @@ void wifiLoop() {
#if ENABLE_MQTT #if ENABLE_MQTT
mqttServer = server.arg("mqttServer"); mqttServer = server.arg("mqttServer");
mqttPort = server.arg("mqttPort"); mqttPort = server.arg("mqttPort");
mqttBaseTopic = server.arg("mqttBaseTopic");
mqttTopic = server.arg("mqttTopic");
#endif #endif
#if ENABLE_RF #if ENABLE_RF
rfChannel = server.arg("rfChannel"); rfChannel = server.arg("rfChannel");
@ -457,7 +457,7 @@ void wifiLoop() {
String tmp; String tmp;
// Replace identifier // Replace identifier
String base = mqttBaseTopic;
String base = mqttTopic;
base.replace("{identifier}", getIdentifier()); base.replace("{identifier}", getIdentifier());
// Get publish status topic // Get publish status topic
@ -469,11 +469,6 @@ void wifiLoop() {
tmp.toCharArray(mqttIPTopic, tmp.length()+1); tmp.toCharArray(mqttIPTopic, tmp.length()+1);
mqttIPTopic[tmp.length()+1] = 0; mqttIPTopic[tmp.length()+1] = 0;
// Get subscribe status topic
tmp = base + "/set";
tmp.toCharArray(mqttStatusSetTopic, tmp.length()+1);
mqttStatusSetTopic[tmp.length()+1] = 0;
} }
void mqttCallback(char* topic, byte* payload, unsigned int length) { void mqttCallback(char* topic, byte* payload, unsigned int length) {
@ -488,12 +483,17 @@ void wifiLoop() {
Serial.println(); Serial.println();
#endif #endif
isMQTTMessage = true;
if ((char)payload[0] == '1') { if ((char)payload[0] == '1') {
switchRelayOn(); switchRelayOn();
} else { } else {
switchRelayOff(); switchRelayOff();
} }
isMQTTMessage = false;
} }
void mqttConnect() { void mqttConnect() {
@ -515,14 +515,20 @@ void wifiLoop() {
#ifdef DEBUG #ifdef DEBUG
Serial.println("connected!"); Serial.println("connected!");
Serial.print("Subscribing to "); Serial.print("Subscribing to ");
Serial.println(mqttStatusSetTopic);
Serial.println(mqttStatusTopic);
#endif #endif
// Say hello and report our IP
String ipString = WiFi.localIP().toString(); String ipString = WiFi.localIP().toString();
char ip[ipString.length()+1]; char ip[ipString.length()+1];
ipString.toCharArray(ip, ipString.length()+1); ipString.toCharArray(ip, ipString.length()+1);
mqtt.publish(mqttIPTopic, ip, MQTT_RETAIN); mqtt.publish(mqttIPTopic, ip, MQTT_RETAIN);
mqtt.subscribe(mqttStatusSetTopic);
// Publish current relay status
mqtt.publish(mqttStatusTopic, digitalRead(RELAY_PIN) ? "1" : "0", MQTT_RETAIN);
// Subscribe to topic
mqtt.subscribe(mqttStatusTopic);
} else { } else {
@ -634,7 +640,7 @@ bool saveConfig() {
#if ENABLE_MQTT #if ENABLE_MQTT
file.println("mqttServer=" + mqttServer); file.println("mqttServer=" + mqttServer);
file.println("mqttPort=" + mqttPort); file.println("mqttPort=" + mqttPort);
file.println("mqttBaseTopic=" + mqttBaseTopic);
file.println("mqttTopic=" + mqttTopic);
#endif #endif
#if ENABLE_RF #if ENABLE_RF
file.println("rfChannel=" + rfChannel); file.println("rfChannel=" + rfChannel);
@ -679,7 +685,7 @@ bool loadConfig() {
#if ENABLE_MQTT #if ENABLE_MQTT
else if (line.startsWith("mqttServer=")) mqttServer = line.substring(11); else if (line.startsWith("mqttServer=")) mqttServer = line.substring(11);
else if (line.startsWith("mqttPort=")) mqttPort = line.substring(9); else if (line.startsWith("mqttPort=")) mqttPort = line.substring(9);
else if (line.startsWith("mqttBaseTopic=")) mqttBaseTopic = line.substring(14);
else if (line.startsWith("mqttTopic=")) mqttTopic = line.substring(14);
#endif #endif
#if ENABLE_RF #if ENABLE_RF
else if (line.startsWith("rfChannel=")) rfChannel = line.substring(10); else if (line.startsWith("rfChannel=")) rfChannel = line.substring(10);


Loading…
Cancel
Save