47 lines
1.4 KiB
Markdown
47 lines
1.4 KiB
Markdown
|
# MQTT controllable switches
|
||
|
This usermod allows controlling switches (e.g. relays) via MQTT.
|
||
|
|
||
|
## Usermod installation
|
||
|
|
||
|
1. Copy the file `usermod_mqtt_switch.h` to the `wled00` directory.
|
||
|
2. Register the usermod by adding `#include "usermod_mqtt_switch.h"` in the top and `registerUsermod(new UsermodMqttSwitch());` in the bottom of `usermods_list.cpp`.
|
||
|
|
||
|
|
||
|
Example `usermods_list.cpp`:
|
||
|
|
||
|
```
|
||
|
#include "wled.h"
|
||
|
#include "usermod_mqtt_switch.h"
|
||
|
|
||
|
void registerUsermods()
|
||
|
{
|
||
|
usermods.add(new UsermodMqttSwitch());
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## Define pins
|
||
|
Add a define for MQTTSWITCHPINS to platformio_override.ini.
|
||
|
The following example defines 3 switches connected to the GPIO pins 13, 0 and 2:
|
||
|
|
||
|
```
|
||
|
[env:livingroom]
|
||
|
board = esp12e
|
||
|
platform = ${common.platform_wled_default}
|
||
|
board_build.ldscript = ${common.ldscript_4m1m}
|
||
|
build_flags = ${common.build_flags_esp8266}
|
||
|
-D LEDPIN=3
|
||
|
-D BTNPIN=4
|
||
|
-D RLYPIN=12
|
||
|
-D RLYMDE=1
|
||
|
-D STATUSPIN=15
|
||
|
-D MQTTSWITCHPINS="13, 0, 2"
|
||
|
```
|
||
|
|
||
|
## MQTT topics
|
||
|
This usermod listens on `[mqttDeviceTopic]/switch/0/set` (where 0 is replaced with the index of the switch) for commands. Anything starting with `ON` turns on the switch, everything else turns it off.
|
||
|
Feedback about the current state is provided at `[mqttDeviceTopic]/switch/0/state`.
|
||
|
|
||
|
### Home Assistant auto-discovery
|
||
|
Auto-discovery information is automatically published and you shoudn't have to do anything to register the switches in Home Assistant.
|
||
|
|