|
@ -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); |
|
|