Subscruibe to set
This commit is contained in:
@@ -5,6 +5,13 @@
|
||||
#include <ESP_EEPROM.h>
|
||||
#include <Bounce2.h>
|
||||
|
||||
//#define DEBUG_DISABLED true
|
||||
#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug
|
||||
#ifndef DEBUG_DISABLED // Only if debug is not disabled (for production/release)
|
||||
RemoteDebug Debug;
|
||||
|
||||
#endif
|
||||
|
||||
const char* ssid = "wf-home";
|
||||
const char* password = "0ndthnrf";
|
||||
const char* mqtt_server = "192.168.1.250";
|
||||
@@ -103,6 +110,12 @@ void setup() {
|
||||
connectToWifi();
|
||||
|
||||
cRun = millis();
|
||||
Debug.begin("SW-BigRoom"); // Initialize the WiFi server
|
||||
|
||||
Debug.setResetCmdEnabled(true); // Enable the reset command
|
||||
|
||||
Debug.showProfiler(true); // Profiler (Good to measure times, to optimize codes)
|
||||
Debug.showColors(true); // Colors
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -110,39 +123,22 @@ void loop() {
|
||||
l1.update();
|
||||
l2.update();
|
||||
if(l1.fell()){
|
||||
switchLight(R_LED1, !lStat1, true);
|
||||
lStat1 = !lStat1;
|
||||
switchLight(R_LED1, lStat1, true);
|
||||
}
|
||||
if(l2.fell()){
|
||||
switchLight(R_LED2, !lStat2, true);
|
||||
lStat2 = !lStat2;
|
||||
switchLight(R_LED2, lStat2, true);
|
||||
}
|
||||
// if(lStat1 != oldLStat1){
|
||||
// digitalWrite(R_LED1, lStat1);
|
||||
// oldLStat1 = lStat1;
|
||||
// if(!rcv)
|
||||
// mqttClient.publish("/home/bigroom/lamp1", 0, false, lStat1 ? "1" : "0");
|
||||
// else
|
||||
// rcv = false;
|
||||
// EEPROM.put(0, lStat1);
|
||||
// EEPROM.commit();
|
||||
// }
|
||||
// if(lStat2 != oldLStat2){
|
||||
// digitalWrite(R_LED2, lStat2);
|
||||
// oldLStat2 = lStat2;
|
||||
// if(!rcv)
|
||||
// mqttClient.publish("/home/bigroom/lamp2", 0, false, lStat2 ? "1" : "0");
|
||||
// else
|
||||
// rcv = false;
|
||||
// EEPROM.put(1, lStat2);
|
||||
// EEPROM.commit();
|
||||
// }
|
||||
if (cRun + 9999 < millis()){
|
||||
cRun = millis();
|
||||
char v[11];
|
||||
ultoa(cRun, v, 10);
|
||||
mqttClient.publish("/home/bigroom/millislamp", 0, false, v);
|
||||
//debugI("*Publish Millis: %u");
|
||||
}
|
||||
Debug.handle();
|
||||
yield();
|
||||
}
|
||||
|
||||
bool switchLight(uint8_t nLamp, bool state, bool pub)
|
||||
@@ -152,7 +148,10 @@ bool switchLight(uint8_t nLamp, bool state, bool pub)
|
||||
EEPROM.commit();
|
||||
String topic = "/home/bigroom/lamp";
|
||||
char n = nLamp == R_LED1 ? '1' : '2';
|
||||
if (pub) mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "true" : "false");
|
||||
//if (pub){
|
||||
mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "true" : "false");
|
||||
debugI("*Publish State %d-%s, lst1:%d, lst2%d", n, state ? "true" : "false", lStat1, lStat2);
|
||||
//}
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -174,10 +173,12 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
mqttClient.subscribe("/home/bigroom/lamp1", 1);
|
||||
mqttClient.subscribe("/home/bigroom/lamp2", 1);
|
||||
mqttClient.publish("/home/bigroom/lamp1", 1, false, lStat1 ? "true" : "false");
|
||||
mqttClient.publish("/home/bigroom/lamp2", 1, false, lStat2 ? "true" : "false");
|
||||
mqttClient.publish("/home/bigroom/lamp1_set", 1, false, lStat1 ? "true" : "false");
|
||||
mqttClient.publish("/home/bigroom/lamp2_set", 1, false, lStat2 ? "true" : "false");
|
||||
mqttClient.subscribe("/home/bigroom/lamp1_set", 1);
|
||||
mqttClient.subscribe("/home/bigroom/lamp2_set", 1);
|
||||
digitalWrite(B_LED, LOW);
|
||||
}
|
||||
|
||||
@@ -189,19 +190,31 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if(String(topic) == "/home/bigroom/lamp1"){
|
||||
|
||||
char s[10];
|
||||
strncpy(s, payload, len);
|
||||
if(String(topic) == "/home/bigroom/lamp1_set"){
|
||||
// if (atoi(payload) == 1) switchLight(R_LED1, 1, false);
|
||||
if (strcmp("true", payload) == 0) switchLight(R_LED1, true, false);
|
||||
//lStat1 = true;
|
||||
else switchLight(R_LED1, false, false);
|
||||
//lStat1 = false;
|
||||
//rcv = true;
|
||||
}
|
||||
if(String(topic) == "/home/bigroom/lamp2"){
|
||||
if (strcmp("true", payload) == 0) switchLight(R_LED2, true, false);
|
||||
if (strncmp("true", payload, 4) == 0){
|
||||
switchLight(R_LED1, true, false);
|
||||
debugI("*Switch from MQTT T1: %s", s);
|
||||
}
|
||||
//if (atoi(payload) == 1) switchLight(R_LED2, 1, false);
|
||||
//lStat1 = true;
|
||||
else switchLight(R_LED2, false, false);
|
||||
else{
|
||||
switchLight(R_LED1, false, false);
|
||||
debugI("*Switch from MQTT F1: %s", s);
|
||||
}
|
||||
}
|
||||
if(String(topic) == "/home/bigroom/lamp2_set"){
|
||||
if (strncmp("true", payload, 4) == 0){
|
||||
switchLight(R_LED2, true, false);
|
||||
debugI("*Switch from MQTT T2: %s", s);
|
||||
}
|
||||
//if (atoi(payload) == 1) switchLight(R_LED2, 1, false);
|
||||
//lStat1 = true;
|
||||
else{
|
||||
switchLight(R_LED2, false, false);
|
||||
debugI("*Switch from MQTT F2: %s", s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user