From b725d66ee3bf6656537b2216c37a0d5c5e36c5f0 Mon Sep 17 00:00:00 2001 From: Hermann Kraus Date: Sun, 1 Nov 2020 18:08:30 +0100 Subject: [PATCH] Add options for inverted switches and default values. --- usermods/mqtt_switch_v2/README.md | 8 ++++-- usermods/mqtt_switch_v2/usermod_mqtt_switch.h | 25 ++++++++++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/usermods/mqtt_switch_v2/README.md b/usermods/mqtt_switch_v2/README.md index ec64898d..dc0e259f 100644 --- a/usermods/mqtt_switch_v2/README.md +++ b/usermods/mqtt_switch_v2/README.md @@ -21,7 +21,7 @@ void registerUsermods() ## 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: +The following example defines 3 switches connected to the GPIO pins 13, 5 and 2: ``` [env:livingroom] @@ -34,8 +34,12 @@ build_flags = ${common.build_flags_esp8266} -D RLYPIN=12 -D RLYMDE=1 -D STATUSPIN=15 - -D MQTTSWITCHPINS="13, 0, 2" + -D MQTTSWITCHPINS="13, 5, 2" ``` + +Pins can be inverted by setting `MQTTSWITCHINVERT`. For example `-D MQTTSWITCHINVERT="false, false, true"` would invert the switch on pin 2 in the previous example. + +The default state after booting before any MQTT message can be set by `MQTTSWITCHDEFAULTS`. For example `-D MQTTSWITCHDEFAULTS="ON, OFF, OFF"` would power on the switch on pin 13 and power off switches on pins 5 and 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. diff --git a/usermods/mqtt_switch_v2/usermod_mqtt_switch.h b/usermods/mqtt_switch_v2/usermod_mqtt_switch.h index 32b3c1be..40241206 100644 --- a/usermods/mqtt_switch_v2/usermod_mqtt_switch.h +++ b/usermods/mqtt_switch_v2/usermod_mqtt_switch.h @@ -6,14 +6,31 @@ #endif #ifndef MQTTSWITCHPINS +#error "Please define MQTTSWITCHPINS in platformio_override.ini. e.g. -D MQTTSWITCHPINS="12, 0, 2" " +// The following define helps Eclipse's C++ parser but is never used in production due to the #error statement on the line before #define MQTTSWITCHPINS 12, 0, 2 -//#error "Please define MQTTSWITCHPINS in platformio_override.ini. e.g. -D MQTTSWITCHPINS="12, 0, 2" " #endif +// Default behavior: All outputs active high +#ifndef MQTTSWITCHINVERT +#define MQTTSWITCHINVERT +#endif -static const uint8_t switchPins[] = {MQTTSWITCHPINS}; +// Default behavior: All outputs off +#ifndef MQTTSWITCHDEFAULTS +#define MQTTSWITCHDEFAULTS +#endif + +static const uint8_t switchPins[] = { MQTTSWITCHPINS }; //This is a hack to get the number of pins defined by the user #define NUM_SWITCH_PINS (sizeof(switchPins)) +static const bool switchInvert[NUM_SWITCH_PINS] = { MQTTSWITCHINVERT}; +//Make settings in config file more readable +#define ON 1 +#define OFF 0 +static const bool switchDefaults[NUM_SWITCH_PINS] = { MQTTSWITCHDEFAULTS}; +#undef ON +#undef OFF class UsermodMqttSwitch: public Usermod { @@ -30,8 +47,8 @@ public: void setup() { for (int pinNr = 0; pinNr < NUM_SWITCH_PINS; pinNr++) { + setState(pinNr, switchDefaults[pinNr]); pinMode(switchPins[pinNr], OUTPUT); - setState(pinNr, false); } } @@ -64,7 +81,7 @@ public: if (pinNr > NUM_SWITCH_PINS) return; switchState[pinNr] = active; - digitalWrite((char) switchPins[pinNr], (char) active); + digitalWrite((char) switchPins[pinNr], (char) (switchInvert[pinNr] ? !active : active)); updateState(pinNr); } };