Subscruibe to set

This commit is contained in:
2020-11-28 20:07:10 +03:00
parent 50b11b44f5
commit ece7809304
5 changed files with 84 additions and 61 deletions

View File

@@ -14,4 +14,6 @@ board = nodemcuv2
framework = arduino
board_build.ldscript = eagle.flash.2m.ld
upload_protocol = espota
upload_port = 192.168.1.129
upload_port = 192.168.1.129
lib_deps =
joaolopesf/RemoteDebug @ ^3.0.5

View File

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

View File

@@ -57,12 +57,12 @@ void longPress()
{
//lStat1 = false;
switchLight(R_LED, 0, true);
mqttClient.publish("/home/bigroom/lamp1", 0, false, "false");
mqttClient.publish("/home/bigroom/lamp2", 0, false, "false");
mqttClient.publish("/home/midroom/lamp1", 0, false, "false");
mqttClient.publish("/home/midroom/lamp2", 0, false, "false");
mqttClient.publish("/home/smallroom/lamp1", 0, false, "false");
mqttClient.publish("/home/smallroom/lamp2", 0, false, "false");
mqttClient.publish("/home/bigroom/lamp1_set", 0, false, "false");
mqttClient.publish("/home/bigroom/lamp2_set", 0, false, "false");
mqttClient.publish("/home/midroom/lamp1_set", 0, false, "false");
mqttClient.publish("/home/midroom/lamp2_set", 0, false, "false");
mqttClient.publish("/home/smallroom/lamp1_set", 0, false, "false");
mqttClient.publish("/home/smallroom/lamp2_set", 0, false, "false");
mqttClient.publish("/home/kuh/lighttbl", 0, false, "false");
}
@@ -158,7 +158,8 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
digitalWrite(nLamp, state);
EEPROM.put(0, state);
EEPROM.commit();
if (pub) mqttClient.publish("/home/kor/lamp1", 1, false, state ? "true" : "false");
//if (pub)
mqttClient.publish("/home/kor/lamp1", 1, false, state ? "true" : "false");
return state;
}
@@ -186,8 +187,9 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
}
void onMqttConnect(bool sessionPresent) {
mqttClient.subscribe("/home/kor/lamp1", 1);
mqttClient.publish("/home/kor/lamp1", 0, false, digitalRead(R_LED) == 1 ? "true" : "false");
mqttClient.publish("/home/kor/lamp1_set", 0, false, digitalRead(R_LED) == 1 ? "true" : "false");
mqttClient.subscribe("/home/kor/lamp1_set", 1);
digitalWrite(B_LED, LOW);
}
@@ -215,8 +217,8 @@ void onMqttUnsubscribe(uint16_t packetId) {
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
if(strcmp(topic, "/home/kor/lamp1") == 0){
if (strcmp("true", payload) == 0) switchLight(R_LED, 1, false);//lStat1 = true;
if(strcmp(topic, "/home/kor/lamp1_set") == 0){
if (strncmp("true", payload, 4) == 0) switchLight(R_LED, 1, false);//lStat1 = true;
else switchLight(R_LED, 0, false);//lStat1 = false;
//rcv = true;
}

View File

@@ -117,7 +117,8 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
EEPROM.commit();
String topic = "/home/midroom/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");
return state;
}
@@ -139,10 +140,12 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
}
void onMqttConnect(bool sessionPresent) {
mqttClient.subscribe("/home/midroom/lamp1", 1);
mqttClient.subscribe("/home/midroom/lamp2", 1);
mqttClient.publish("/home/midroom/lamp1", 1, false, digitalRead(R_LED1) == 1 ? "true" : "false");
mqttClient.publish("/home/midroom/lamp2", 1, false, digitalRead(R_LED2) == 1 ? "true" : "false");
mqttClient.publish("/home/midroom/lamp1_set", 1, false, digitalRead(R_LED1) == 1 ? "true" : "false");
mqttClient.publish("/home/midroom/lamp2_set", 1, false, digitalRead(R_LED2) == 1 ? "true" : "false");
mqttClient.subscribe("/home/midroom/lamp1_set", 1);
mqttClient.subscribe("/home/midroom/lamp2_set", 1);
digitalWrite(B_LED, LOW);
}
@@ -154,12 +157,12 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
if(strcmp(topic, "/home/midroom/lamp1") == 0){
if (strcmp("true", payload) == 0) switchLight(R_LED1, 1, false);//lStat1 = true;
if(strcmp(topic, "/home/midroom/lamp1_set") == 0){
if (strncmp("true", payload, 4) == 0) switchLight(R_LED1, 1, false);//lStat1 = true;
else switchLight(R_LED1, 0, false);//lStat1 = false;
}
if(strcmp(topic, "/home/midroom/lamp2") == 0){
if (strcmp("true", payload) == 0) switchLight(R_LED2, 1, false);//lStat2 = true;
if(strcmp(topic, "/home/midroom/lamp2_set") == 0){
if (strncmp("true", payload, 4) == 0) switchLight(R_LED2, 1, false);//lStat2 = true;
else switchLight(R_LED2, 0, false);//lStat2 = false;
}
}

View File

@@ -125,7 +125,8 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
EEPROM.commit();
String topic = "/home/smallroom/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");
return state;
}
@@ -153,10 +154,12 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
}
void onMqttConnect(bool sessionPresent) {
mqttClient.subscribe("/home/smallroom/lamp1", 1);
mqttClient.subscribe("/home/smallroom/lamp2", 1);
mqttClient.publish("/home/smallroom/lamp1", 1, false, digitalRead(R_LED1) == 1 ? "true" : "false");
mqttClient.publish("/home/smallroom/lamp2", 1, false, digitalRead(R_LED2) == 1 ? "true" : "false");
mqttClient.publish("/home/smallroom/lamp1_set", 1, false, digitalRead(R_LED1) == 1 ? "true" : "false");
mqttClient.publish("/home/smallroom/lamp2_set", 1, false, digitalRead(R_LED2) == 1 ? "true" : "false");
mqttClient.subscribe("/home/smallroom/lamp1_set", 1);
mqttClient.subscribe("/home/smallroom/lamp2_set", 1);
digitalWrite(B_LED, LOW);
}
@@ -168,12 +171,12 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
if(strcmp(topic, "/home/smallroom/lamp1") == 0){
if (strcmp("true", payload) == 0) switchLight(R_LED1, 1, false);//lStat1 = true;
if(strcmp(topic, "/home/smallroom/lamp1_set") == 0){
if (strncmp("true", payload, 4) == 0) switchLight(R_LED1, 1, false);//lStat1 = true;
else switchLight(R_LED1, 0, false);//lStat1 = false;
}
if(strcmp(topic, "/home/smallroom/lamp2") == 0){
if (strcmp("true", payload) == 0) switchLight(R_LED2, 1, false);//lStat2 = true;
if(strcmp(topic, "/home/smallroom/lamp2_set") == 0){
if (strncmp("true", payload, 4) == 0) switchLight(R_LED2, 1, false);//lStat2 = true;
else switchLight(R_LED2, 0, false);//lStat2 = false;
}
}