Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT report setting for buttons: send action events, all events or state #1772

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions code/espurna/button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ typedef struct {

std::vector<button_t> _buttons;

#define BUTTONS_MAX 8u

#if MQTT_SUPPORT

void buttonMQTT(unsigned char id, uint8_t event) {
uint8_t _button_mqtt_mask_events {BUTTON_MQTT_MASK_EVENTS};
uint8_t _button_mqtt_mask_pressed {BUTTON_MQTT_MASK_PRESSED};

void buttonMQTT(unsigned char id, uint8_t event, bool optional_retain = false) {
if (id >= _buttons.size()) return;
char payload[2];
itoa(event, payload, 10);
mqttSend(MQTT_TOPIC_BUTTON, id, payload, false, false); // 1st bool = force, 2nd = retain
mqttSend(MQTT_TOPIC_BUTTON, id, payload, false, optional_retain ? mqttRetain() : false); // 1st bool = force, 2nd = retain
}

#endif
Expand Down Expand Up @@ -100,9 +105,18 @@ void buttonEvent(unsigned int id, unsigned char event) {
unsigned char action = buttonAction(id, event);

#if MQTT_SUPPORT
if (action != BUTTON_MODE_NONE || BUTTON_MQTT_SEND_ALL_EVENTS) {
buttonMQTT(id, event);
}
{
uint8_t id_bit = (1 << ((BUTTONS_MAX - 1) - id));

if (_button_mqtt_mask_pressed & id_bit) {
buttonMQTT(id, buttonState(id), true);
return;
}

if ((BUTTON_MODE_NONE != action) || (_button_mqtt_mask_events & id_bit)) {
buttonMQTT(id, event);
}
}
#endif

if (BUTTON_MODE_TOGGLE == action) {
Expand Down Expand Up @@ -246,6 +260,22 @@ void buttonSetup() {
wsOnReceiveRegister(_buttonWebSocketOnReceive);
#endif

#if MQTT_SUPPORT
_button_mqtt_mask_events = getSetting("btnMaskEvents", BUTTON_MQTT_MASK_EVENTS).toInt();
if (_button_mqtt_mask_events > 0) {
char buffer[16];
itoa(_button_mqtt_mask_events, buffer, 2);
DEBUG_MSG_P(PSTR("[BUTTON] MQTT Events mask: %s\n"), buffer);
}

_button_mqtt_mask_pressed = getSetting("btnMaskPressed", BUTTON_MQTT_MASK_PRESSED).toInt();
if (_button_mqtt_mask_pressed > 0) {
char buffer[16];
itoa(_button_mqtt_mask_pressed, buffer, 2);
DEBUG_MSG_P(PSTR("[BUTTON] MQTT pressed mask: %s\n"), buffer);
}
#endif

// Register loop
espurnaRegisterLoop(buttonLoop);

Expand Down
13 changes: 10 additions & 3 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,16 @@
#define BUTTON_LNGLNGCLICK_DELAY 10000 // Time in ms holding the button down to get a long-long click
#endif

#ifndef BUTTON_MQTT_SEND_ALL_EVENTS
#define BUTTON_MQTT_SEND_ALL_EVENTS 0 // 0 - to send only events the are bound to actions
// 1 - to send all button events to MQTT
// Button event filter for MQTT represented as bitset (unsigned)
// 0 - to send only events the are bound to actions
// 1 - to send all button events to MQTT
#ifndef BUTTON_MQTT_MASK_EVENTS
#define BUTTON_MQTT_MASK_EVENTS 0b00000000u
#endif

// Bitset for PUSHBUTTONs that can directly send pressed() status, instead of event
#ifndef BUTTON_MQTT_MASK_PRESSED
#define BUTTON_MQTT_MASK_PRESSED 0b00000000u
#endif

//------------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion code/espurna/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
#define BUTTON_MODE_DIM_UP 10
#define BUTTON_MODE_DIM_DOWN 11


// Needed for ESP8285 boards under Windows using PlatformIO (?)
#ifndef BUTTON_PUSHBUTTON
#define BUTTON_PUSHBUTTON 0
Expand Down
4 changes: 4 additions & 0 deletions code/espurna/mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,10 @@ bool mqttConnected() {
return _mqtt.connected();
}

bool mqttRetain() {
return _mqtt_retain;
}

void mqttDisconnect() {
if (_mqtt.connected()) {
DEBUG_MSG_P(PSTR("[MQTT] Disconnecting\n"));
Expand Down