diff --git a/code/espurna/button.ino b/code/espurna/button.ino index 972186a1..f4214c87 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -82,28 +82,28 @@ typedef struct { std::vector _buttons; #ifdef MQTT_BUTTON_TOPIC -void buttonMQTT(unsigned char id) { +void buttonMQTT(unsigned char id, const char * payload) { if (id >= _buttons.size()) return; String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); char buffer[strlen(MQTT_BUTTON_TOPIC) + mqttGetter.length() + 3]; sprintf(buffer, "%s/%d%s", MQTT_BUTTON_TOPIC, id, mqttGetter.c_str()); - mqttSend(buffer, _buttons[id].button->pressed() ? "1" : "0"); + mqttSend(buffer, payload); } #endif void buttonSetup() { #ifdef BUTTON1_PIN - _buttons.push_back({new DebounceEvent(BUTTON1_PIN), BUTTON1_RELAY}); + _buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE), BUTTON1_RELAY}); #endif #ifdef BUTTON2_PIN - _buttons.push_back({new DebounceEvent(BUTTON2_PIN), BUTTON2_RELAY}); + _buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE), BUTTON2_RELAY}); #endif #ifdef BUTTON3_PIN - _buttons.push_back({new DebounceEvent(BUTTON3_PIN), BUTTON3_RELAY}); + _buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE), BUTTON3_RELAY}); #endif #ifdef BUTTON4_PIN - _buttons.push_back({new DebounceEvent(BUTTON4_PIN), BUTTON4_RELAY}); + _buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE), BUTTON4_RELAY}); #endif #ifdef LED_PULSE @@ -125,7 +125,7 @@ void buttonLoop() { DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", i, event); #ifdef MQTT_BUTTON_TOPIC - buttonMQTT(i); + buttonMQTT(i, (event == EVENT_CHANGED || event == EVENT_PRESSED) ? "1" : "0"); #endif if (i == 0) { @@ -140,7 +140,9 @@ void buttonLoop() { } #endif - if (event == EVENT_SINGLE_CLICK) { + // Here we can have EVENT_CHANGED only when using BUTTON_SWITCH + // and EVENT_SINGLE_CLICK only when using BUTTON_PUSHBUTTON + if (event == EVENT_SINGLE_CLICK || event == EVENT_CHANGED) { if (_buttons[i].relayID > 0) { relayToggle(_buttons[i].relayID - 1); } diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index 2d0dd5f2..88fa41b2 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -8,6 +8,7 @@ #define DEVICE "LOLIN" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_SWITCH | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 2 @@ -32,6 +33,7 @@ #define DEVICE "SONOFF" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -43,6 +45,7 @@ #define DEVICE "SONOFF_TH" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -54,6 +57,7 @@ #define DEVICE "SONOFF_SV" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -65,6 +69,7 @@ #define DEVICE "SLAMPHER" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -76,6 +81,7 @@ #define DEVICE "S20" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -87,6 +93,7 @@ #define DEVICE "SONOFF_TOUCH" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -98,6 +105,7 @@ #define DEVICE "SONOFF_POW" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 15 @@ -120,12 +128,16 @@ #define DEVICE "SONOFF_4CH" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON2_PIN 9 #define BUTTON2_RELAY 2 + #define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON3_PIN 10 #define BUTTON3_RELAY 3 + #define BUTTON3_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON4_PIN 14 #define BUTTON4_RELAY 4 + #define BUTTON4_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define RELAY2_PIN 5 @@ -147,8 +159,10 @@ #define DEVICE "1CH_INCHING" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON2_PIN 15 #define BUTTON2_RELAY 0 + #define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 @@ -165,8 +179,10 @@ #define DEVICE "ESP_RELAY_BOARD" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON2_PIN 2 #define BUTTON2_RELAY 2 + #define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define RELAY2_PIN 13 @@ -184,6 +200,7 @@ #define DEVICE "ECOPLUG" #define BUTTON1_PIN 13 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 15 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 2 @@ -199,8 +216,10 @@ #define DEVICE "WIFI_RELAY_NC" #define BUTTON1_PIN 12 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON2_PIN 13 #define BUTTON2_RELAY 2 + #define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 2 #define RELAY1_PIN_INVERSE 1 #define RELAY2_PIN 14 @@ -212,8 +231,10 @@ #define DEVICE "WIFI_RELAY_NO" #define BUTTON1_PIN 12 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON2_PIN 13 #define BUTTON2_RELAY 2 + #define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 2 #define RELAY1_PIN_INVERSE 0 #define RELAY2_PIN 14 @@ -229,6 +250,7 @@ #define DEVICE "ESPURNA" #define BUTTON1_PIN 0 #define BUTTON1_RELAY 1 + #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define RELAY1_PIN 12 #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13