Browse Source

Map buttons to relays, send MQTT messages on button events

fastled
Xose Pérez 7 years ago
parent
commit
32bb6c45c7
3 changed files with 63 additions and 9 deletions
  1. +46
    -9
      code/espurna/button.ino
  2. +1
    -0
      code/espurna/config/general.h
  3. +16
    -0
      code/espurna/config/hardware.h

+ 46
- 9
code/espurna/button.ino View File

@ -12,6 +12,16 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#ifdef SONOFF_DUAL #ifdef SONOFF_DUAL
#ifdef MQTT_BUTTON_TOPIC
void buttonMQTT(unsigned char id) {
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, 1);
mqttSend(buffer, 0);
}
#endif
void buttonSetup() {} void buttonSetup() {}
void buttonLoop() { void buttonLoop() {
@ -29,7 +39,12 @@ void buttonLoop() {
// Since we are not passing back RELAY2 value // Since we are not passing back RELAY2 value
// (in the relayStatus method) it will only be present // (in the relayStatus method) it will only be present
// here if it has actually been pressed // here if it has actually been pressed
if ((value & 4) == 4) value = value ^ 1;
if ((value & 4) == 4) {
value = value ^ 1;
#ifdef MQTT_BUTTON_TOPIC
buttonMQTT(0);
#endif
}
// Otherwise check if any of the other two BUTTONs // Otherwise check if any of the other two BUTTONs
// (in the header) has been pressent, but we should // (in the header) has been pressent, but we should
@ -59,21 +74,36 @@ void buttonLoop() {
#include <DebounceEvent.h> #include <DebounceEvent.h>
#include <vector> #include <vector>
std::vector<DebounceEvent *> _buttons;
typedef struct {
DebounceEvent * button;
unsigned int relayID;
} button_t;
std::vector<button_t> _buttons;
#ifdef MQTT_BUTTON_TOPIC
void buttonMQTT(unsigned char id) {
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");
}
#endif
void buttonSetup() { void buttonSetup() {
#ifdef BUTTON1_PIN #ifdef BUTTON1_PIN
_buttons.push_back(new DebounceEvent(BUTTON1_PIN));
_buttons.push_back({new DebounceEvent(BUTTON1_PIN), BUTTON1_RELAY});
#endif #endif
#ifdef BUTTON2_PIN #ifdef BUTTON2_PIN
_buttons.push_back(new DebounceEvent(BUTTON2_PIN));
_buttons.push_back({new DebounceEvent(BUTTON2_PIN), BUTTON2_RELAY});
#endif #endif
#ifdef BUTTON3_PIN #ifdef BUTTON3_PIN
_buttons.push_back(new DebounceEvent(BUTTON3_PIN));
_buttons.push_back({new DebounceEvent(BUTTON3_PIN), BUTTON3_RELAY});
#endif #endif
#ifdef BUTTON4_PIN #ifdef BUTTON4_PIN
_buttons.push_back(new DebounceEvent(BUTTON4_PIN));
_buttons.push_back({new DebounceEvent(BUTTON4_PIN), BUTTON4_RELAY});
#endif #endif
DEBUG_MSG("[BUTTON] Number of buttons: %d\n", _buttons.size()); DEBUG_MSG("[BUTTON] Number of buttons: %d\n", _buttons.size());
@ -83,14 +113,21 @@ void buttonSetup() {
void buttonLoop() { void buttonLoop() {
for (unsigned int i=0; i < _buttons.size(); i++) { for (unsigned int i=0; i < _buttons.size(); i++) {
if (_buttons[i]->loop()) {
uint8_t event = _buttons[i]->getEvent();
if (_buttons[i].button->loop()) {
uint8_t event = _buttons[i].button->getEvent();
DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", i, event); DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", i, event);
#ifdef MQTT_BUTTON_TOPIC
buttonMQTT(i);
#endif
if (i == 0) { if (i == 0) {
if (event == EVENT_DOUBLE_CLICK) createAP(); if (event == EVENT_DOUBLE_CLICK) createAP();
if (event == EVENT_LONG_CLICK) ESP.reset(); if (event == EVENT_LONG_CLICK) ESP.reset();
} }
if (event == EVENT_SINGLE_CLICK) relayToggle(i);
if (event == EVENT_SINGLE_CLICK) {
if (_buttons[i].relayID > 0) {
relayToggle(_buttons[i].relayID - 1);
}
}
} }
} }


+ 1
- 0
code/espurna/config/general.h View File

@ -70,6 +70,7 @@
#define MQTT_SKIP_TIME 1000 #define MQTT_SKIP_TIME 1000
#define MQTT_RELAY_TOPIC "/relay" #define MQTT_RELAY_TOPIC "/relay"
#define MQTT_LED_TOPIC "/led" #define MQTT_LED_TOPIC "/led"
#define MQTT_BUTTON_TOPIC "/button"
#define MQTT_IP_TOPIC "/ip" #define MQTT_IP_TOPIC "/ip"
#define MQTT_VERSION_TOPIC "/version" #define MQTT_VERSION_TOPIC "/version"
#define MQTT_FSVERSION_TOPIC "/fsversion" #define MQTT_FSVERSION_TOPIC "/fsversion"


+ 16
- 0
code/espurna/config/hardware.h View File

@ -7,6 +7,7 @@
#define MANUFACTURER "NODEMCU" #define MANUFACTURER "NODEMCU"
#define DEVICE "LOLIN" #define DEVICE "LOLIN"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 2 #define LED1_PIN 2
@ -30,6 +31,7 @@
#define MANUFACTURER "ITEAD" #define MANUFACTURER "ITEAD"
#define DEVICE "SONOFF" #define DEVICE "SONOFF"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -40,6 +42,7 @@
#define MANUFACTURER "ITEAD" #define MANUFACTURER "ITEAD"
#define DEVICE "SONOFF_TH" #define DEVICE "SONOFF_TH"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -50,6 +53,7 @@
#define MANUFACTURER "ITEAD" #define MANUFACTURER "ITEAD"
#define DEVICE "SONOFF_SV" #define DEVICE "SONOFF_SV"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -60,6 +64,7 @@
#define MANUFACTURER "ITEAD" #define MANUFACTURER "ITEAD"
#define DEVICE "SLAMPHER" #define DEVICE "SLAMPHER"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -70,6 +75,7 @@
#define MANUFACTURER "ITEAD" #define MANUFACTURER "ITEAD"
#define DEVICE "S20" #define DEVICE "S20"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -80,6 +86,7 @@
#define MANUFACTURER "ITEAD" #define MANUFACTURER "ITEAD"
#define DEVICE "SONOFF_TOUCH" #define DEVICE "SONOFF_TOUCH"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13
@ -90,6 +97,7 @@
#define MANUFACTURER "ITEAD" #define MANUFACTURER "ITEAD"
#define DEVICE "SONOFF_POW" #define DEVICE "SONOFF_POW"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 15 #define LED1_PIN 15
@ -111,9 +119,13 @@
#define MANUFACTURER "ITEAD" #define MANUFACTURER "ITEAD"
#define DEVICE "SONOFF_4CH" #define DEVICE "SONOFF_4CH"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define BUTTON2_PIN 9 #define BUTTON2_PIN 9
#define BUTTON2_RELAY 2
#define BUTTON3_PIN 10 #define BUTTON3_PIN 10
#define BUTTON3_RELAY 3
#define BUTTON4_PIN 14 #define BUTTON4_PIN 14
#define BUTTON4_RELAY 4
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define RELAY2_PIN 5 #define RELAY2_PIN 5
@ -134,7 +146,9 @@
#define MANUFACTURER "ELECTRODRAGON" #define MANUFACTURER "ELECTRODRAGON"
#define DEVICE "ESP_RELAY_BOARD" #define DEVICE "ESP_RELAY_BOARD"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define BUTTON2_PIN 2 #define BUTTON2_PIN 2
#define BUTTON2_RELAY 2
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define RELAY2_PIN 13 #define RELAY2_PIN 13
@ -151,6 +165,7 @@
#define MANUFACTURER "WORKCHOICE" #define MANUFACTURER "WORKCHOICE"
#define DEVICE "ECOPLUG" #define DEVICE "ECOPLUG"
#define BUTTON1_PIN 13 #define BUTTON1_PIN 13
#define BUTTON1_RELAY 1
#define RELAY1_PIN 15 #define RELAY1_PIN 15
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 2 #define LED1_PIN 2
@ -165,6 +180,7 @@
#define MANUFACTURER "TINKERMAN" #define MANUFACTURER "TINKERMAN"
#define DEVICE "ESPURNA" #define DEVICE "ESPURNA"
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define RELAY1_PIN 12 #define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0 #define RELAY1_PIN_INVERSE 0
#define LED1_PIN 13 #define LED1_PIN 13


Loading…
Cancel
Save