Skip to content

Commit

Permalink
Add options for inverted switches and default values.
Browse files Browse the repository at this point in the history
  • Loading branch information
herm committed Nov 1, 2020
1 parent fe3fb62 commit b725d66
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
8 changes: 6 additions & 2 deletions usermods/mqtt_switch_v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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.
Expand Down
25 changes: 21 additions & 4 deletions usermods/mqtt_switch_v2/usermod_mqtt_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

// Default behavior: All outputs off
#ifndef MQTTSWITCHDEFAULTS
#define MQTTSWITCHDEFAULTS
#endif

static const uint8_t switchPins[] = {MQTTSWITCHPINS};
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
{
Expand All @@ -30,8 +47,8 @@ class UsermodMqttSwitch: public Usermod
void setup()
{
for (int pinNr = 0; pinNr < NUM_SWITCH_PINS; pinNr++) {
setState(pinNr, switchDefaults[pinNr]);
pinMode(switchPins[pinNr], OUTPUT);
setState(pinNr, false);
}
}

Expand Down Expand Up @@ -64,7 +81,7 @@ class UsermodMqttSwitch: public Usermod
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);
}
};
Expand Down

0 comments on commit b725d66

Please sign in to comment.