Skip to content

Buttons and switches

Maxim Prokhorov edited this page Jan 14, 2021 · 45 revisions

This page is about general options you can compile-in into your ESPurna. Predefined board settings can be found in header file espurna/config/hardware.h and will take precedence over command line flags (-DFLAG=VALUE) and any additional configuration in espurna/config/custom.h. It is recommended to define a custom unique board instead of modifying hardware.h directly, either by using custom.h or manually specifying command line flags. See PlatformIO documentation for more information and the comments on top of the custom.h.

Buttons and switches

Headers configuration and run-time settings

Various C preprocessor flags that are used to configure multiple entities are numbered starting from 1. However, internal logging, MQTT & HTTP API, Terminal commands, Settings, etc., reference each entity using zero-based numbering.

For example, when modifying boot mode for the first relay - in headers it is defined as #define RELAY1_BOOT_MODE <flag>, while the equivalent settings command is set relayBoot0 <number>

Buttons

NOTICE!: Since version 1.15.0 BUTTON[1-8]_CONFIG is used instead of BUTTON[1-8]_MODE

ESPurna supports up to 8 buttons connected to various GPIO pins. These buttons are defined using C preprocessor flag BUTTONx_PIN (x being a number from 1 to 8). Some buttons might be onboard, and you might have the option of connecting some additional, depending on the board you are using.

Each button can operate in number of different modes, configured using BUTTONx_CONFIG bitmask.

  • Button type results in a different events set:
    • BUTTON_PUSHBUTTON - connected button is of push-button type, possible events are: pressed, released, double clicked, long clicked and long-long clicked).
    • BUTTON_SWITCH - connected button is actually a flip-switch, reports eithr pressed or released, depending on the BUTTON_DEFAULT_... flag for the default pin state.
  • Default state of the button, used to determine whether it's pressed or released:
    • BUTTON_DEFAULT_LOW (default)
    • BUTTON_DEFAULT_HIGH
    • BUTTON_DEFAULT_BOOT (read at firmware startup)
  • Internal pull-up / pull-down resistors. When not specified, pin is left floating.
    • BUTTON_SET_PULLUP
    • BUTTON_SET_PULLDOWN (WARNING! ESP8266 only supports pull-down on GPIO16, which in turn does not support pull-up)

For example, using the following build flags:

#define BUTTON3_PIN 2
#define BUTTON3_CONFIG BUTTON_PUSHBUTTON | BUTTON_DEFAULT_LOW

Will set up a pushbutton on GPIO2 (externally pulled-down), which will be pressed when it is connected to the VCC.

Or, using the following flags we will set up a switch on GPIO5 (with internal pull-up), which will read the current pin status on boot. Doing that allows us to leave the external switch in any position. In case firmware reboots, we don't trigger the pressed / released event.

#define BUTTON1_PIN 5
#define BUTTON1_CONFIG BUTTON_SWITCH | BUTTON_SET_PULLUP | BUTTON_DEFAULT_BOOT
#define BUTTON1_PRESS BUTTON_ACTION_TOGGLE
#define BUTTON1_RELEASE BUTTON_ACTION_TOGGLE

Providers

Available since version 1.15.0

By default, buttons can only use ESP8266 GPIOs. However, we support additional 'providers' by setting BUTTONx_PROVIDER build flag or changing btnProvX, replacing x with the correct button index and setting it at runtime with the corresponding integer value of the provider:

Build flag Settings key Description
BUTTONx_PROVIDER btnProvX

Provider types

Build flag Settings key Description
BUTTON_PROVIDER_NONE No-op provider type
BUTTON_PROVIDER_GPIO Generic GPIO (value: 1)

| BUTTON_PROVIDER_ANALOG | | Resistor ladder support. For more info, see #2357 (value: 2). Specific analogRead() value that will trigger the button press can be configured via BUTTON#_ANALOG_LEVEL |

GPIO provider

Build flag Settings key Description
BUTTONx_PIN_TYPE btnGPIOType One of the GPIO_TYPE_x flags (default: GPIO_TYPE_HARDWARE)

Events

As seen in https://github.com/xoseperez/espurna/blob/b289c77021c4d3fcd70feb25ca14178929f31e6c/code/espurna/button.h#L19-L27:

enum class button_event_t {
    None,
    Pressed,
    Released,
    Click,
    DoubleClick,
    LongClick,
    LongLongClick,
    TripleClick
};

To every button event an action can be assigned by setting the corresponding value to the button event define. The following button events are available

#define BUTTONx_PRESS
#define BUTTONx_RELEASE   (specific to switch)
#define BUTTONx_CLICK
#define BUTTONx_DBLCLICK
#define BUTTONx_LNGCLICK
#define BUTTONx_LNGLNGCLICK

Actions

To every event, one of the following actions can be assigned:

Flag Description
BUTTON_ACTION_NONE
BUTTON_ACTION_TOGGLE Toggle relay
BUTTON_ACTION_ON Turn relay ON
BUTTON_ACTION_OFF Turn relay OFF
BUTTON_ACTION_AP Access point mode
BUTTON_ACTION_RESET Reboot the device
BUTTON_ACTION_FACTORY Erase settings and reboot
BUTTON_ACTION_WPS Save new WiFi connection settings via WPS
BUTTON_ACTION_SMART_CONFIG Save new WiFi connection settings via SmartConfig
BUTTON_ACTION_DIM_UP Lights: increase brightness
BUTTON_ACTION_DIM_DOWN Lights: decrease brightness
BUTTON_ACTION_DISPLAY_ON Thermostat: toggle display

Button 1 has the following defaults:

#define BUTTONx_PRESS           BUTTON_ACTION_NONE
#define BUTTONx_RELEASE         BUTTON_ACTION_NONE
#define BUTTONx_CLICK           BUTTON_ACTION_TOGGLE
#define BUTTONx_DBLCLICK        BUTTON_ACTION_AP
#define BUTTONx_LNGCLICK        BUTTON_ACTION_RESET
#define BUTTONx_LNGLNGCLICK     BUTTON_ACTION_FACTORY

NOTE: If you plan using button1, please make sure you override these settings.

NOTE: You can always test out configuration by setting btnClick0 (CLICK), btnDclk0 (DBLCLICK), btnLLclk0 (LNGLNGCLICK) etc. from runtime with the correct numeric representation of an action.

NOTE: Since version 1.14.2 BUTTON_ACTION_ is used instead of BUTTON_MODE_

Buttons 2 to 8 BUTTONx_CLICK event is set to BUTTON_ACTION_TOGGLE, while every other event is set to BUTTON_ACTION_NONE.

Settings

Settings key Possible values Description
btnProv# Provider ID, see Providers
btnGPIO# 0 ... 16 GPIO for the specified provider
btnMode# 0 for Pushbutton or 1 for Switch (Pushbutton by default)
btnDefVal# 0 for LOW, 1 for HIGH or 2 to set the value at boot from the current reading
btnPinMode# 0 for INPUT, 1 for INPUT_PULLUP or 2 INPUT_PULLDOWN
btnPress# Action ID, see Actions What happens after the 'Pressed' event
btnRlse# 'Released' event
btnClick# 'Click' event
btnDclk# 'Double-click' event
btnLclk# 'Long-click' event
btnLLclk# 'Looong-click' event
btnTclk# 'Triple-click' event
btnDebDel# Number of milliseconds Timeout between checking the GPIO value
btnRepDel# Number of milliseconds Timeout between repeated presses (used for Double-click and Triple-click)
btnLclkDel# Number of milliseconds Timeout between 'Pressed' and 'Released' to trigger 'Long-click' event
btnLLclkDel# Number of milliseconds Timeout between 'Pressed' and 'Released' to trigger 'Looong-click' event

Settings are loaded on the firmware boot.

MQTT

By default, MQTT client will report the button event only when it has an associated action. The following build flags control this behaviour:

Build flag Description
BUTTONx_MQTT_SEND_ALL_EVENTS
BUTTON_MQTT_SEND_ALL_EVENTS Global setting for every button (unless, overriden by the BUTTONx flag)
BUTTONx_MQTT_RETAIN
BUTTON_MQTT_RETAIN Global setting for every button (unless, overriden by the BUTTONx flag)

Or, to control this at runtime:

Settings key Possible values Description
btnMqttSendAll# 0 or 1 (default 0) Always send button events
btnMqttRetain# 0 or 1 (default 0) Set 'Retain' flag for the MQTT button event message

Relays

ESPurna supports up to 8 connected relays to various GPIO pins. These relays are defined using C preprocessor flag RELAYx_PIN (x being a number from 1 to 8).

For example, relay 1 is connected to GPIO4:

#define RELAY1_PIN        4

Each relay can operate in one of the following modes defined by RELAYx_TYPE:

  • RELAY_TYPE_NORMAL - High-level-trigger, normally open relay.
  • RELAY_TYPE_INVERSE - Either low-level-trigger, or normally closed relay.
  • RELAY_TYPE_LATCHED- Relay is controlled with two normally-low GPIOs, if set GPIO goes up the relay will turn on, if reset GPIO goes up the relay will turn off
  • RELAY_TYPE_LATCHED_INVERSE- Relay is controlled with two normally-high GPIOs, if set GPIO goes down the relay will turn on, if reset GPIO goes down the relay will turn off

#define RELAY1_TYPE RELAY_TYPE_NORMAL defines that relay 1 is RELAY_TYPE_NORMAL

ON and OFF actions can be delayed for a specified time in milliseconds:

  • RELAYx_DELAY_ON
  • RELAYx_DELAY_OFF

LEDs

ESPurna supports up to 8 connected LEDs to various GPIO pins. These LEDs are defined using C preprocessor flag LEDx_PIN (x being a number from 1 to 8). Some LEDs might be onboard, and you might have the option of connecting some additional, depending on the board you are using.

See LED Configuration & Modes.

Tasmota templates

As we cannot use templates directly, we need to translate names given by the Tasmota Device Templates Repository (see top right) into something ESPurna configuration can understand.

Template flag ESPurna Build flag Description
<pin> Relay# RELAY#_PIN <pin>, RELAY#_TYPE RELAY_TYPE_NORMAL
<pin> Relay#i RELAY#_PIN <pin>, RELAY#_TYPE RELAY_TYPE_INVERSE
<pin> Led# LED#_PIN <pin>, LED#_PIN_INVERSE 0, (for other configuration, see LED#_MODE)
<pin> Led#i LED#_PIN <pin>, LED#_PIN_INVERSE 1
<pin> Button# BUTTON#_PIN <pin>, BUTTON#_CONFIG with BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH
<pin> Button#i BUTTON#_PIN <pin>, BUTTON#_CONFIG with BUTTON_SET_PULLUP (implicit default LOW)
<pin> Button#in BUTTON#_PIN <pin>, BUTTON#_CONFIG without BUTTON_SET_PULLUP (i.e. floating pin) (implicit default LOW)
<pin> Switch# BUTTON#_PIN <pin>, BUTTON#_CONFIG with BUTTON_SWITCH | BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH
<pin> Switch#n BUTTON#_PIN <pin>, BUTTON#_CONFIG without BUTTON_SET_PULLUP

Note that not every configuration option is 1-to-1. For more information, see:

Binding buttons, relays and LEDs

ESPurna has ability to change relay state based on the event coming from a button and set a LED status accordingly.

To do that, one should define BUTTONx_RELAY with a relay number that's "bound" to a switch. So, for example -DBUTTON1_PIN=10 -DRELAY2_PIN=11 -DBUTTON1_RELAY=2 will configure Button1 on GPIO10, Relay2 on GPIO11, and will connect Button1 to Relay2.

To also reflect Relay2 state (as defined above) on a LED1 connected to GPIO02, one should configure: -DLED1_PIN=2 -DLED1_RELAY=2 in addition to the above line.

Home
Change log

Getting started

Supported hardware and options

Configuration

Integrations

Network

Developers

More around ESPurna

Clone this wiki locally