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>
<label class="desc" for="mqtt_server">MQTT Server</label>
<label class="desc" for="mqttServer">MQTT Server</label>
<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>
<label class="desc" for="mqtt_port">MQTT Port</label>
<label class="desc" for="mqttPort">MQTT Port</label>
<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>
<label class="desc" for="mqtt_topic">MQTT Topic</label>
<label class="desc" for="mqttTopic">MQTT Topic</label>
<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>
<label class="desc" for="mqtt_port">RF Channel</label>
<label class="desc" for="rfChannel">RF Channel</label>
<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>
<label class="desc" for="mqtt_topic">RF Device</label>
<label class="desc" for="rfDevice">RF Device</label>
<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>


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

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


Loading…
Cancel
Save