just comment

This commit is contained in:
2021-12-12 19:11:03 +03:00
parent 620e954716
commit 149cb3b5b6
16 changed files with 432 additions and 64 deletions

View File

@@ -9,7 +9,8 @@
#include <Ticker.h>
#include <AsyncMqttClient.h>
#include <ESP_EEPROM.h>
#include <OneButton.h>
#include <Bounce2.h>
//#include <OneButton.h>
const char* ssid = "wf-home";
const char* password = "0ndthnrf";
@@ -31,7 +32,8 @@ unsigned long cRun;
#define BUTT (5)
//bool led = false;
OneButton button(BUTT);
//OneButton button(BUTT);
Bounce lSwitch = Bounce();
bool switchLight(uint8_t nLamp, int state, bool pub);
void connectToWifi();
void connectToMqtt();
@@ -54,18 +56,18 @@ void oneClick()
//lStat1 = !lStat1;
}
void longPress()
{
//lStat1 = false;
switchLight(R_LED, 0, true);
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");
}
// void longPress()
// {
// //lStat1 = false;
// switchLight(R_LED, 0, true);
// 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");
// }
void setup() {
//Serial.begin(115200);
@@ -126,9 +128,11 @@ void setup() {
mqttClient.setServer(mqtt_server, 1883);
mqttClient.setClientId("SW_Koridor");
button.attachClick(oneClick);
button.attachLongPressStart(longPress);
button.setPressTicks(2000);
//button.attachClick(oneClick);
lSwitch.attach(BUTT);
lSwitch.interval(5);
// button.attachLongPressStart(longPress);
//button.setPressTicks(2000);
//attachInterrupt(digitalPinToInterrupt(BUTT), sw_func, RISING);
@@ -140,7 +144,10 @@ void setup() {
void loop() {
ArduinoOTA.handle();
button.tick();
//button.tick();
lSwitch.update();
if(lSwitch.fell())
switchLight(R_LED, !digitalRead(R_LED), true);
// if(lStat1 != oldLStat1){
// digitalWrite(R_LED, lStat1);
// oldLStat1 = lStat1;
@@ -165,7 +172,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
EEPROM.put(0, state);
EEPROM.commit();
//if (pub)
mqttClient.publish("/home/kor/lamp1", 1, false, state ? "true" : "false");
mqttClient.publish("/home/kor/lamp1", 1, false, state ? "1" : "0");
return state;
}
@@ -193,8 +200,8 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
}
void onMqttConnect(bool sessionPresent) {
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.publish("/home/kor/lamp1", 0, false, digitalRead(R_LED) == 1 ? "1" : "0");
mqttClient.publish("/home/kor/lamp1_set", 0, false, digitalRead(R_LED) == 1 ? "1" : "0");
mqttClient.subscribe("/home/kor/lamp1_set", 1);
digitalWrite(B_LED, LOW);
}
@@ -224,7 +231,7 @@ 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_set") == 0){
if (strncmp("true", payload, 4) == 0) switchLight(R_LED, 1, false);//lStat1 = true;
if (strncmp("1", payload, 1) == 0) switchLight(R_LED, 1, false);//lStat1 = true;
else switchLight(R_LED, 0, false);//lStat1 = false;
//rcv = true;
}