2021-05-07 12:41:39 +02:00
# Multi Relay
2022-12-10 16:12:55 +01:00
This usermod-v2 modification allows the connection of multiple relays, each with individual delay and on/off mode.
2023-05-24 23:40:23 +02:00
Usermod supports PCF8574 I2C port expander to reduce GPIO use.
PCF8574 supports 8 outputs and each output corresponds to a relay in WLED (relay 0 = port 0, etc). I you are using more than 8 relays with multiple PCF8574 make sure their addresses are set conscutively (e.g. 0x20 and 0x21). You can set address of first expander in settings.
(**NOTE:** Will require Wire library and global I2C pins defined.)
2021-05-07 12:41:39 +02:00
2021-05-17 12:52:27 +02:00
## HTTP API
2022-12-10 16:12:55 +01:00
All responses are returned in JSON format.
2021-05-17 12:52:27 +02:00
2022-02-01 09:33:57 +01:00
* Status Request: `http://[device-ip]/relays`
* Switch Command: `http://[device-ip]/relays?switch=1,0,1,1`
2022-12-10 16:12:55 +01:00
The number of values behind the switch parameter must correspond to the number of relays. The value 1 switches the relay on, 0 switches it off.
2021-05-17 12:52:27 +02:00
2022-02-01 09:33:57 +01:00
* Toggle Command: `http://[device-ip]/relays?toggle=1,0,1,1`
2022-12-10 16:12:55 +01:00
The number of values behind the parameter switch must correspond to the number of relays. The value 1 causes the relay to toggle, 0 leaves its state unchanged.
2021-05-17 12:52:27 +02:00
2022-12-10 16:12:55 +01:00
Examples:
2022-02-01 09:33:57 +01:00
1. total of 4 relays, relay 2 will be toggled: `http://[device-ip]/relays?toggle=0,1,0,0`
2. total of 3 relays, relay 1& 3 will be switched on: `http://[device-ip]/relays?switch=1,0,1`
2021-05-17 12:52:27 +02:00
2021-10-31 11:57:03 +01:00
## JSON API
2022-12-10 16:12:55 +01:00
You can toggle the relay state by sending the following JSON object to: `http://[device-ip]/json`
2022-02-01 09:33:57 +01:00
2021-10-31 11:57:03 +01:00
Switch relay 0 on: `{"MultiRelay":{"relay":0,"on":true}}`
2022-02-01 09:33:57 +01:00
2022-12-10 16:12:55 +01:00
Switch relay 3 and 4 off: `{"MultiRelay":[{"relay":2,"on":false},{"relay":3,"on":false}]}`
2021-10-31 11:57:03 +01:00
2021-05-17 12:52:27 +02:00
## MQTT API
2022-02-01 09:33:57 +01:00
* `wled` /_deviceMAC_/`relay`/`0`/`command` `on` |`off`|`toggle`
* `wled` /_deviceMAC_/`relay`/`1`/`command` `on` |`off`|`toggle`
2021-05-17 12:52:27 +02:00
2022-12-10 16:12:55 +01:00
When a relay is switched, a message is published:
2021-05-17 12:52:27 +02:00
2022-02-01 09:33:57 +01:00
* `wled` /_deviceMAC_/`relay`/`0` `on` |`off`
2021-05-17 12:52:27 +02:00
2021-05-07 12:41:39 +02:00
## Usermod installation
1. Register the usermod by adding `#include "../usermods/multi_relay/usermod_multi_relay.h"` at the top and `usermods.add(new MultiRelay());` at the bottom of `usermods_list.cpp` .
or
2022-01-20 00:26:07 +01:00
2. Use `#define USERMOD_MULTI_RELAY` in wled.h or `-D USERMOD_MULTI_RELAY` in your platformio.ini
2021-05-07 12:41:39 +02:00
2022-12-10 16:12:55 +01:00
You can override the default maximum number of relays (which is 4) by defining MULTI_RELAY_MAX_RELAYS.
2021-05-07 12:41:39 +02:00
Example **usermods_list.cpp** :
```cpp
#include "wled.h"
/*
* Register your v2 usermods here!
* (for v1 usermods using just usermod.cpp, you can ignore this file)
*/
/*
* Add/uncomment your usermod filename here (and once more below)
* || || ||
* \/ \/ \/
*/
//#include "usermod_v2_example.h"
//#include "usermod_temperature.h"
#include "../usermods/usermod_multi_relay.h"
void registerUsermods()
{
/*
* Add your usermod class name here
* || || ||
* \/ \/ \/
*/
//usermods.add(new MyExampleUsermod());
//usermods.add(new UsermodTemperature());
usermods.add(new MultiRelay());
}
```
## Configuration
2022-12-10 16:12:55 +01:00
Usermod can be configured via the Usermods settings page.
2021-05-07 12:41:39 +02:00
2022-02-01 09:33:57 +01:00
* `enabled` - enable/disable usermod
2023-05-24 23:40:23 +02:00
* `use-PCF8574` - use PCF8574 port expander instead of GPIO pins
* `first-PCF8574` - I2C address of first expander (WARNING: enter *decimal* value)
* `broadcast` - time in seconds between MQTT relay-state broadcasts
* `HA-discovery` - enable Home Assistant auto discovery
2022-12-10 16:12:55 +01:00
* `pin` - ESP GPIO pin the relay is connected to (can be configured at compile time `-D MULTI_RELAY_PINS=xx,xx,...` )
2022-02-01 09:33:57 +01:00
* `delay-s` - delay in seconds after on/off command is received
2022-12-10 16:12:55 +01:00
* `active-high` - assign high/low activation of relay (can be used to reverse relay states)
* `external` - if enabled, WLED does not control relay, it can only be triggered by an external command (MQTT, HTTP, JSON or button)
2022-02-01 09:33:57 +01:00
* `button` - button (from LED Settings) that controls this relay
2021-05-07 12:41:39 +02:00
If there is no MultiRelay section, just save current configuration and re-open Usermods settings page.
Have fun - @blazoncek
## Change log
2021-04
2022-02-01 09:33:57 +01:00
* First implementation.
2021-11
* Added information about dynamic configuration options
2022-12-10 16:12:55 +01:00
* Added button support.
2023-05-24 23:40:23 +02:00
2023-05
* Added support for PCF8574 I2C port expander (multiple)