Add support for PCF8574 I2C port expander
- for Multi Relay usermod.
This commit is contained in:
parent
56a3bff42e
commit
b2df7981a9
@ -1,6 +1,9 @@
|
|||||||
# Multi Relay
|
# Multi Relay
|
||||||
|
|
||||||
This usermod-v2 modification allows the connection of multiple relays, each with individual delay and on/off mode.
|
This usermod-v2 modification allows the connection of multiple relays, each with individual delay and on/off mode.
|
||||||
|
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.)
|
||||||
|
|
||||||
## HTTP API
|
## HTTP API
|
||||||
All responses are returned in JSON format.
|
All responses are returned in JSON format.
|
||||||
@ -81,13 +84,15 @@ void registerUsermods()
|
|||||||
Usermod can be configured via the Usermods settings page.
|
Usermod can be configured via the Usermods settings page.
|
||||||
|
|
||||||
* `enabled` - enable/disable usermod
|
* `enabled` - enable/disable usermod
|
||||||
|
* `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
|
||||||
* `pin` - ESP GPIO pin the relay is connected to (can be configured at compile time `-D MULTI_RELAY_PINS=xx,xx,...`)
|
* `pin` - ESP GPIO pin the relay is connected to (can be configured at compile time `-D MULTI_RELAY_PINS=xx,xx,...`)
|
||||||
* `delay-s` - delay in seconds after on/off command is received
|
* `delay-s` - delay in seconds after on/off command is received
|
||||||
* `active-high` - assign high/low activation of relay (can be used to reverse relay states)
|
* `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)
|
* `external` - if enabled, WLED does not control relay, it can only be triggered by an external command (MQTT, HTTP, JSON or button)
|
||||||
* `button` - button (from LED Settings) that controls this relay
|
* `button` - button (from LED Settings) that controls this relay
|
||||||
* `broadcast`- time in seconds between MQTT relay-state broadcasts
|
|
||||||
* `HA-discovery`- enable Home Assistant auto discovery
|
|
||||||
|
|
||||||
If there is no MultiRelay section, just save current configuration and re-open Usermods settings page.
|
If there is no MultiRelay section, just save current configuration and re-open Usermods settings page.
|
||||||
|
|
||||||
@ -100,3 +105,6 @@ Have fun - @blazoncek
|
|||||||
2021-11
|
2021-11
|
||||||
* Added information about dynamic configuration options
|
* Added information about dynamic configuration options
|
||||||
* Added button support.
|
* Added button support.
|
||||||
|
|
||||||
|
2023-05
|
||||||
|
* Added support for PCF8574 I2C port expander (multiple)
|
@ -4,6 +4,11 @@
|
|||||||
|
|
||||||
#ifndef MULTI_RELAY_MAX_RELAYS
|
#ifndef MULTI_RELAY_MAX_RELAYS
|
||||||
#define MULTI_RELAY_MAX_RELAYS 4
|
#define MULTI_RELAY_MAX_RELAYS 4
|
||||||
|
#else
|
||||||
|
#if MULTI_RELAY_MAX_RELAYS>16
|
||||||
|
#undef MULTI_RELAY_MAX_RELAYS
|
||||||
|
#define MULTI_RELAY_MAX_RELAYS 16
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MULTI_RELAY_PINS
|
#ifndef MULTI_RELAY_PINS
|
||||||
@ -15,21 +20,29 @@
|
|||||||
#define ON true
|
#define ON true
|
||||||
#define OFF false
|
#define OFF false
|
||||||
|
|
||||||
|
#ifndef PCF8574_ADDRESS
|
||||||
|
#define PCF8574_ADDRESS 0x20 // some may start at 0x38
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This usermod handles multiple relay outputs.
|
* This usermod handles multiple relay outputs.
|
||||||
* These outputs complement built-in relay output in a way that the activation can be delayed.
|
* These outputs complement built-in relay output in a way that the activation can be delayed.
|
||||||
* They can also activate/deactivate in reverse logic independently.
|
* They can also activate/deactivate in reverse logic independently.
|
||||||
|
*
|
||||||
|
* Written and maintained by @blazoncek
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
typedef struct relay_t {
|
typedef struct relay_t {
|
||||||
int8_t pin;
|
int8_t pin;
|
||||||
bool active;
|
struct { // reduces memory footprint
|
||||||
bool mode;
|
bool active : 1;
|
||||||
bool state;
|
bool mode : 1;
|
||||||
bool external;
|
bool state : 1;
|
||||||
|
bool external : 1;
|
||||||
|
int8_t button : 4;
|
||||||
|
};
|
||||||
uint16_t delay;
|
uint16_t delay;
|
||||||
int8_t button;
|
|
||||||
} Relay;
|
} Relay;
|
||||||
|
|
||||||
|
|
||||||
@ -48,7 +61,8 @@ class MultiRelay : public Usermod {
|
|||||||
bool enabled = false; // needs to be configured (no default config)
|
bool enabled = false; // needs to be configured (no default config)
|
||||||
// status of initialisation
|
// status of initialisation
|
||||||
bool initDone = false;
|
bool initDone = false;
|
||||||
|
bool usePcf8574 = false;
|
||||||
|
uint8_t addrPcf8574 = PCF8574_ADDRESS;
|
||||||
bool HAautodiscovery = false;
|
bool HAautodiscovery = false;
|
||||||
|
|
||||||
uint16_t periodicBroadcastSec = 60;
|
uint16_t periodicBroadcastSec = 60;
|
||||||
@ -64,8 +78,127 @@ class MultiRelay : public Usermod {
|
|||||||
static const char _button[];
|
static const char _button[];
|
||||||
static const char _broadcast[];
|
static const char _broadcast[];
|
||||||
static const char _HAautodiscovery[];
|
static const char _HAautodiscovery[];
|
||||||
|
static const char _pcf8574[];
|
||||||
|
static const char _pcfAddress[];
|
||||||
|
|
||||||
void publishMqtt(int relay) {
|
void handleOffTimer();
|
||||||
|
void InitHtmlAPIHandle();
|
||||||
|
int getValue(String data, char separator, int index);
|
||||||
|
uint8_t getActiveRelayCount();
|
||||||
|
|
||||||
|
byte IOexpanderWrite(byte address, byte _data);
|
||||||
|
byte IOexpanderRead(int address);
|
||||||
|
|
||||||
|
void publishMqtt(int relay);
|
||||||
|
#ifndef WLED_DISABLE_MQTT
|
||||||
|
void publishHomeAssistantAutodiscovery();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* constructor
|
||||||
|
*/
|
||||||
|
MultiRelay();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* desctructor
|
||||||
|
*/
|
||||||
|
~MultiRelay() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable/Disable the usermod
|
||||||
|
*/
|
||||||
|
inline void enable(bool enable) { enabled = enable; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get usermod enabled/disabled state
|
||||||
|
*/
|
||||||
|
inline bool isEnabled() { return enabled; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!).
|
||||||
|
* This could be used in the future for the system to determine whether your usermod is installed.
|
||||||
|
*/
|
||||||
|
inline uint16_t getId() { return USERMOD_ID_MULTI_RELAY; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* switch relay on/off
|
||||||
|
*/
|
||||||
|
void switchRelay(uint8_t relay, bool mode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* toggle relay
|
||||||
|
*/
|
||||||
|
inline void toggleRelay(uint8_t relay) {
|
||||||
|
switchRelay(relay, !_relay[relay].state);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setup() is called once at boot. WiFi is not yet connected at this point.
|
||||||
|
* You can use it to initialize variables, sensors or similar.
|
||||||
|
*/
|
||||||
|
void setup();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* connected() is called every time the WiFi is (re)connected
|
||||||
|
* Use it to initialize network interfaces
|
||||||
|
*/
|
||||||
|
inline void connected() { InitHtmlAPIHandle(); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* loop() is called continuously. Here you can check for events, read sensors, etc.
|
||||||
|
*/
|
||||||
|
void loop();
|
||||||
|
|
||||||
|
#ifndef WLED_DISABLE_MQTT
|
||||||
|
bool onMqttMessage(char* topic, char* payload);
|
||||||
|
void onMqttConnect(bool sessionPresent);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* handleButton() can be used to override default button behaviour. Returning true
|
||||||
|
* will prevent button working in a default way.
|
||||||
|
* Replicating button.cpp
|
||||||
|
*/
|
||||||
|
bool handleButton(uint8_t b);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
|
||||||
|
*/
|
||||||
|
void addToJsonInfo(JsonObject &root);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object).
|
||||||
|
* Values in the state object may be modified by connected clients
|
||||||
|
*/
|
||||||
|
void addToJsonState(JsonObject &root);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object).
|
||||||
|
* Values in the state object may be modified by connected clients
|
||||||
|
*/
|
||||||
|
void readFromJsonState(JsonObject &root);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* provide the changeable values
|
||||||
|
*/
|
||||||
|
void addToConfig(JsonObject &root);
|
||||||
|
|
||||||
|
void appendConfigData();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* restore the changeable values
|
||||||
|
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
|
||||||
|
*
|
||||||
|
* The function should return true if configuration was successfully loaded or false if there was no configuration.
|
||||||
|
*/
|
||||||
|
bool readFromConfig(JsonObject &root);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// class implementetion
|
||||||
|
|
||||||
|
void MultiRelay::publishMqtt(int relay) {
|
||||||
#ifndef WLED_DISABLE_MQTT
|
#ifndef WLED_DISABLE_MQTT
|
||||||
//Check if MQTT Connected, otherwise it will crash the 8266
|
//Check if MQTT Connected, otherwise it will crash the 8266
|
||||||
if (WLED_MQTT_CONNECTED){
|
if (WLED_MQTT_CONNECTED){
|
||||||
@ -79,7 +212,7 @@ class MultiRelay : public Usermod {
|
|||||||
/**
|
/**
|
||||||
* switch off the strip if the delay has elapsed
|
* switch off the strip if the delay has elapsed
|
||||||
*/
|
*/
|
||||||
void handleOffTimer() {
|
void MultiRelay::handleOffTimer() {
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
bool activeRelays = false;
|
bool activeRelays = false;
|
||||||
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
|
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
|
||||||
@ -101,7 +234,7 @@ class MultiRelay : public Usermod {
|
|||||||
* https://github.com/gsieben/WLED/blob/master/usermods/GeoGab-Relays/usermod_GeoGab.h
|
* https://github.com/gsieben/WLED/blob/master/usermods/GeoGab-Relays/usermod_GeoGab.h
|
||||||
*/
|
*/
|
||||||
#define GEOGABVERSION "0.1.3"
|
#define GEOGABVERSION "0.1.3"
|
||||||
void InitHtmlAPIHandle() { // https://github.com/me-no-dev/ESPAsyncWebServer
|
void MultiRelay::InitHtmlAPIHandle() { // https://github.com/me-no-dev/ESPAsyncWebServer
|
||||||
DEBUG_PRINTLN(F("Relays: Initialize HTML API"));
|
DEBUG_PRINTLN(F("Relays: Initialize HTML API"));
|
||||||
|
|
||||||
server.on("/relays", HTTP_GET, [this](AsyncWebServerRequest *request) {
|
server.on("/relays", HTTP_GET, [this](AsyncWebServerRequest *request) {
|
||||||
@ -163,7 +296,7 @@ class MultiRelay : public Usermod {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
int getValue(String data, char separator, int index) {
|
int MultiRelay::getValue(String data, char separator, int index) {
|
||||||
int found = 0;
|
int found = 0;
|
||||||
int strIndex[] = {0, -1};
|
int strIndex[] = {0, -1};
|
||||||
int maxIndex = data.length()-1;
|
int maxIndex = data.length()-1;
|
||||||
@ -178,11 +311,27 @@ class MultiRelay : public Usermod {
|
|||||||
return found>index ? data.substring(strIndex[0], strIndex[1]).toInt() : -1;
|
return found>index ? data.substring(strIndex[0], strIndex[1]).toInt() : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
//Write a byte to the IO expander
|
||||||
/**
|
byte MultiRelay::IOexpanderWrite(byte address, byte _data ) {
|
||||||
* constructor
|
Wire.beginTransmission(addrPcf8574 + address);
|
||||||
*/
|
Wire.write(_data);
|
||||||
MultiRelay() {
|
return Wire.endTransmission();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Read a byte from the IO expander
|
||||||
|
byte MultiRelay::IOexpanderRead(int address) {
|
||||||
|
byte _data = 0;
|
||||||
|
Wire.requestFrom(addrPcf8574 + address, 1);
|
||||||
|
if (Wire.available()) {
|
||||||
|
_data = Wire.read();
|
||||||
|
}
|
||||||
|
return _data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// public methods
|
||||||
|
|
||||||
|
MultiRelay::MultiRelay() {
|
||||||
const int8_t defPins[] = {MULTI_RELAY_PINS};
|
const int8_t defPins[] = {MULTI_RELAY_PINS};
|
||||||
for (size_t i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
|
for (size_t i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
|
||||||
_relay[i].pin = i<sizeof(defPins) ? defPins[i] : -1;
|
_relay[i].pin = i<sizeof(defPins) ? defPins[i] : -1;
|
||||||
@ -194,44 +343,35 @@ class MultiRelay : public Usermod {
|
|||||||
_relay[i].button = -1;
|
_relay[i].button = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* desctructor
|
|
||||||
*/
|
|
||||||
~MultiRelay() {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enable/Disable the usermod
|
|
||||||
*/
|
|
||||||
inline void enable(bool enable) { enabled = enable; }
|
|
||||||
/**
|
|
||||||
* Get usermod enabled/disabled state
|
|
||||||
*/
|
|
||||||
inline bool isEnabled() { return enabled; }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* switch relay on/off
|
* switch relay on/off
|
||||||
*/
|
*/
|
||||||
void switchRelay(uint8_t relay, bool mode) {
|
void MultiRelay::switchRelay(uint8_t relay, bool mode) {
|
||||||
if (relay>=MULTI_RELAY_MAX_RELAYS || _relay[relay].pin<0) return;
|
if (relay>=MULTI_RELAY_MAX_RELAYS || (_relay[relay].pin<0 && !usePcf8574)) return;
|
||||||
_relay[relay].state = mode;
|
_relay[relay].state = mode;
|
||||||
|
if (usePcf8574) {
|
||||||
|
byte expander = relay/8;
|
||||||
|
uint16_t state = 0;
|
||||||
|
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) state |= (uint16_t)_relay[i].state << i; // fill relay states for all pins
|
||||||
|
state = (mode ? !_relay[relay].mode : _relay[relay].mode) ? state | (1<<relay) : state & ~(1<<relay); // take into account invert mode
|
||||||
|
IOexpanderWrite(expander, state>>(8*expander));
|
||||||
|
DEBUG_PRINT(F("PCF8574 Writing to ")); DEBUG_PRINT(addrPcf8574 + expander); DEBUG_PRINT(F(" with data ")); DEBUG_PRINTLN(state>>(8*expander));
|
||||||
|
} else {
|
||||||
pinMode(_relay[relay].pin, OUTPUT);
|
pinMode(_relay[relay].pin, OUTPUT);
|
||||||
digitalWrite(_relay[relay].pin, mode ? !_relay[relay].mode : _relay[relay].mode);
|
digitalWrite(_relay[relay].pin, mode ? !_relay[relay].mode : _relay[relay].mode);
|
||||||
|
}
|
||||||
publishMqtt(relay);
|
publishMqtt(relay);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
uint8_t MultiRelay::getActiveRelayCount() {
|
||||||
* toggle relay
|
|
||||||
*/
|
|
||||||
inline void toggleRelay(uint8_t relay) {
|
|
||||||
switchRelay(relay, !_relay[relay].state);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t getActiveRelayCount() {
|
|
||||||
uint8_t count = 0;
|
uint8_t count = 0;
|
||||||
|
if (usePcf8574) return MULTI_RELAY_MAX_RELAYS; // we don't know how many there are
|
||||||
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) if (_relay[i].pin>=0) count++;
|
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) if (_relay[i].pin>=0) count++;
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Functions called by WLED
|
//Functions called by WLED
|
||||||
|
|
||||||
#ifndef WLED_DISABLE_MQTT
|
#ifndef WLED_DISABLE_MQTT
|
||||||
@ -240,7 +380,7 @@ class MultiRelay : public Usermod {
|
|||||||
* topic only contains stripped topic (part after /wled/MAC)
|
* topic only contains stripped topic (part after /wled/MAC)
|
||||||
* topic should look like: /relay/X/command; where X is relay number, 0 based
|
* topic should look like: /relay/X/command; where X is relay number, 0 based
|
||||||
*/
|
*/
|
||||||
bool onMqttMessage(char* topic, char* payload) {
|
bool MultiRelay::onMqttMessage(char* topic, char* payload) {
|
||||||
if (strlen(topic) > 8 && strncmp_P(topic, PSTR("/relay/"), 7) == 0 && strncmp_P(topic+8, PSTR("/command"), 8) == 0) {
|
if (strlen(topic) > 8 && strncmp_P(topic, PSTR("/relay/"), 7) == 0 && strncmp_P(topic+8, PSTR("/command"), 8) == 0) {
|
||||||
uint8_t relay = strtoul(topic+7, NULL, 10);
|
uint8_t relay = strtoul(topic+7, NULL, 10);
|
||||||
if (relay<MULTI_RELAY_MAX_RELAYS) {
|
if (relay<MULTI_RELAY_MAX_RELAYS) {
|
||||||
@ -263,7 +403,7 @@ class MultiRelay : public Usermod {
|
|||||||
/**
|
/**
|
||||||
* subscribe to MQTT topic for controlling relays
|
* subscribe to MQTT topic for controlling relays
|
||||||
*/
|
*/
|
||||||
void onMqttConnect(bool sessionPresent) {
|
void MultiRelay::onMqttConnect(bool sessionPresent) {
|
||||||
//(re)subscribe to required topics
|
//(re)subscribe to required topics
|
||||||
char subuf[64];
|
char subuf[64];
|
||||||
if (mqttDeviceTopic[0] != 0) {
|
if (mqttDeviceTopic[0] != 0) {
|
||||||
@ -278,7 +418,7 @@ class MultiRelay : public Usermod {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void publishHomeAssistantAutodiscovery() {
|
void MultiRelay::publishHomeAssistantAutodiscovery() {
|
||||||
for (int i = 0; i < MULTI_RELAY_MAX_RELAYS; i++) {
|
for (int i = 0; i < MULTI_RELAY_MAX_RELAYS; i++) {
|
||||||
char uid[24], json_str[1024], buf[128];
|
char uid[24], json_str[1024], buf[128];
|
||||||
size_t payload_size;
|
size_t payload_size;
|
||||||
@ -322,8 +462,21 @@ class MultiRelay : public Usermod {
|
|||||||
* setup() is called once at boot. WiFi is not yet connected at this point.
|
* setup() is called once at boot. WiFi is not yet connected at this point.
|
||||||
* You can use it to initialize variables, sensors or similar.
|
* You can use it to initialize variables, sensors or similar.
|
||||||
*/
|
*/
|
||||||
void setup() {
|
void MultiRelay::setup() {
|
||||||
// pins retrieved from cfg.json (readFromConfig()) prior to running setup()
|
// pins retrieved from cfg.json (readFromConfig()) prior to running setup()
|
||||||
|
// if we want PCF8574 expander I2C pins need to be valid
|
||||||
|
if (i2c_sda == i2c_scl && i2c_sda == -1) usePcf8574 = false;
|
||||||
|
|
||||||
|
if (usePcf8574) {
|
||||||
|
uint16_t state = 0;
|
||||||
|
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) state |= (uint16_t)(_relay[i].external ? (_relay[i].mode ? !_relay[i].state : _relay[i].state) : (_relay[i].mode ? !offMode : offMode)) << i; // fill relay states for all pins
|
||||||
|
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i += 8) {
|
||||||
|
byte expander = i/8;
|
||||||
|
IOexpanderWrite(expander, state>>(8*expander)); // init expander (set all outputs)
|
||||||
|
delay(1);
|
||||||
|
}
|
||||||
|
DEBUG_PRINTLN(F("PCF8574(s) inited."));
|
||||||
|
} else {
|
||||||
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
|
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
|
||||||
if (_relay[i].pin<0) continue;
|
if (_relay[i].pin<0) continue;
|
||||||
if (!pinManager.allocatePin(_relay[i].pin,true, PinOwner::UM_MultiRelay)) {
|
if (!pinManager.allocatePin(_relay[i].pin,true, PinOwner::UM_MultiRelay)) {
|
||||||
@ -334,22 +487,15 @@ class MultiRelay : public Usermod {
|
|||||||
_relay[i].active = false;
|
_relay[i].active = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
_oldMode = offMode;
|
_oldMode = offMode;
|
||||||
initDone = true;
|
initDone = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* connected() is called every time the WiFi is (re)connected
|
|
||||||
* Use it to initialize network interfaces
|
|
||||||
*/
|
|
||||||
void connected() {
|
|
||||||
InitHtmlAPIHandle();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* loop() is called continuously. Here you can check for events, read sensors, etc.
|
* loop() is called continuously. Here you can check for events, read sensors, etc.
|
||||||
*/
|
*/
|
||||||
void loop() {
|
void MultiRelay::loop() {
|
||||||
yield();
|
yield();
|
||||||
if (!enabled || strip.isUpdating()) return;
|
if (!enabled || strip.isUpdating()) return;
|
||||||
|
|
||||||
@ -362,7 +508,7 @@ class MultiRelay : public Usermod {
|
|||||||
_oldMode = offMode;
|
_oldMode = offMode;
|
||||||
_switchTimerStart = millis();
|
_switchTimerStart = millis();
|
||||||
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
|
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
|
||||||
if (_relay[i].pin>=0 && !_relay[i].external) _relay[i].active = true;
|
if ((_relay[i].pin>=0 || usePcf8574) && !_relay[i].external) _relay[i].active = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -374,7 +520,7 @@ class MultiRelay : public Usermod {
|
|||||||
* will prevent button working in a default way.
|
* will prevent button working in a default way.
|
||||||
* Replicating button.cpp
|
* Replicating button.cpp
|
||||||
*/
|
*/
|
||||||
bool handleButton(uint8_t b) {
|
bool MultiRelay::handleButton(uint8_t b) {
|
||||||
yield();
|
yield();
|
||||||
if (!enabled
|
if (!enabled
|
||||||
|| buttonType[b] == BTN_TYPE_NONE
|
|| buttonType[b] == BTN_TYPE_NONE
|
||||||
@ -466,7 +612,7 @@ class MultiRelay : public Usermod {
|
|||||||
/**
|
/**
|
||||||
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
|
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
|
||||||
*/
|
*/
|
||||||
void addToJsonInfo(JsonObject &root) {
|
void MultiRelay::addToJsonInfo(JsonObject &root) {
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
JsonObject user = root["u"];
|
JsonObject user = root["u"];
|
||||||
if (user.isNull())
|
if (user.isNull())
|
||||||
@ -478,7 +624,7 @@ class MultiRelay : public Usermod {
|
|||||||
|
|
||||||
String uiDomString;
|
String uiDomString;
|
||||||
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
|
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
|
||||||
if (_relay[i].pin<0 || !_relay[i].external) continue;
|
if ((_relay[i].pin<0 && !usePcf8574) || !_relay[i].external) continue;
|
||||||
uiDomString = F("Relay "); uiDomString += i;
|
uiDomString = F("Relay "); uiDomString += i;
|
||||||
JsonArray infoArr = user.createNestedArray(uiDomString); // timer value
|
JsonArray infoArr = user.createNestedArray(uiDomString); // timer value
|
||||||
|
|
||||||
@ -503,7 +649,7 @@ class MultiRelay : public Usermod {
|
|||||||
* addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object).
|
* addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object).
|
||||||
* Values in the state object may be modified by connected clients
|
* Values in the state object may be modified by connected clients
|
||||||
*/
|
*/
|
||||||
void addToJsonState(JsonObject &root) {
|
void MultiRelay::addToJsonState(JsonObject &root) {
|
||||||
if (!initDone || !enabled) return; // prevent crash on boot applyPreset()
|
if (!initDone || !enabled) return; // prevent crash on boot applyPreset()
|
||||||
JsonObject multiRelay = root[FPSTR(_name)];
|
JsonObject multiRelay = root[FPSTR(_name)];
|
||||||
if (multiRelay.isNull()) {
|
if (multiRelay.isNull()) {
|
||||||
@ -527,7 +673,7 @@ class MultiRelay : public Usermod {
|
|||||||
* readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object).
|
* readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object).
|
||||||
* Values in the state object may be modified by connected clients
|
* Values in the state object may be modified by connected clients
|
||||||
*/
|
*/
|
||||||
void readFromJsonState(JsonObject &root) {
|
void MultiRelay::readFromJsonState(JsonObject &root) {
|
||||||
if (!initDone || !enabled) return; // prevent crash on boot applyPreset()
|
if (!initDone || !enabled) return; // prevent crash on boot applyPreset()
|
||||||
JsonObject usermod = root[FPSTR(_name)];
|
JsonObject usermod = root[FPSTR(_name)];
|
||||||
if (!usermod.isNull()) {
|
if (!usermod.isNull()) {
|
||||||
@ -557,11 +703,14 @@ class MultiRelay : public Usermod {
|
|||||||
/**
|
/**
|
||||||
* provide the changeable values
|
* provide the changeable values
|
||||||
*/
|
*/
|
||||||
void addToConfig(JsonObject &root) {
|
void MultiRelay::addToConfig(JsonObject &root) {
|
||||||
JsonObject top = root.createNestedObject(FPSTR(_name));
|
JsonObject top = root.createNestedObject(FPSTR(_name));
|
||||||
|
|
||||||
top[FPSTR(_enabled)] = enabled;
|
top[FPSTR(_enabled)] = enabled;
|
||||||
|
top[FPSTR(_pcf8574)] = usePcf8574;
|
||||||
|
top[FPSTR(_pcfAddress)] = addrPcf8574;
|
||||||
top[FPSTR(_broadcast)] = periodicBroadcastSec;
|
top[FPSTR(_broadcast)] = periodicBroadcastSec;
|
||||||
|
top[FPSTR(_HAautodiscovery)] = HAautodiscovery;
|
||||||
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
|
for (int i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
|
||||||
String parName = FPSTR(_relay_str); parName += '-'; parName += i;
|
String parName = FPSTR(_relay_str); parName += '-'; parName += i;
|
||||||
JsonObject relay = top.createNestedObject(parName);
|
JsonObject relay = top.createNestedObject(parName);
|
||||||
@ -571,17 +720,22 @@ class MultiRelay : public Usermod {
|
|||||||
relay[FPSTR(_external)] = _relay[i].external;
|
relay[FPSTR(_external)] = _relay[i].external;
|
||||||
relay[FPSTR(_button)] = _relay[i].button;
|
relay[FPSTR(_button)] = _relay[i].button;
|
||||||
}
|
}
|
||||||
top[FPSTR(_HAautodiscovery)] = HAautodiscovery;
|
|
||||||
DEBUG_PRINTLN(F("MultiRelay config saved."));
|
DEBUG_PRINTLN(F("MultiRelay config saved."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MultiRelay::appendConfigData() {
|
||||||
|
oappend(SET_F("addInfo('MultiRelay:first-PCF8574',1,'<i>(not hex!)</i>','address');"));
|
||||||
|
oappend(SET_F("addInfo('MultiRelay:broadcast-sec',1,'(MQTT message)');"));
|
||||||
|
oappend(SET_F("addInfo('MultiRelay:relay-0:pin',1,'(use -1 for PCF8574)');"));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* restore the changeable values
|
* restore the changeable values
|
||||||
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
|
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
|
||||||
*
|
*
|
||||||
* The function should return true if configuration was successfully loaded or false if there was no configuration.
|
* The function should return true if configuration was successfully loaded or false if there was no configuration.
|
||||||
*/
|
*/
|
||||||
bool readFromConfig(JsonObject &root) {
|
bool MultiRelay::readFromConfig(JsonObject &root) {
|
||||||
int8_t oldPin[MULTI_RELAY_MAX_RELAYS];
|
int8_t oldPin[MULTI_RELAY_MAX_RELAYS];
|
||||||
|
|
||||||
JsonObject top = root[FPSTR(_name)];
|
JsonObject top = root[FPSTR(_name)];
|
||||||
@ -591,7 +745,13 @@ class MultiRelay : public Usermod {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//bool configComplete = !top.isNull();
|
||||||
|
//configComplete &= getJsonValue(top[FPSTR(_enabled)], enabled);
|
||||||
enabled = top[FPSTR(_enabled)] | enabled;
|
enabled = top[FPSTR(_enabled)] | enabled;
|
||||||
|
usePcf8574 = top[FPSTR(_pcf8574)] | usePcf8574;
|
||||||
|
addrPcf8574 = top[FPSTR(_pcfAddress)] | addrPcf8574;
|
||||||
|
// if I2C is not globally initialised just ignore
|
||||||
|
if (i2c_sda == i2c_scl && i2c_sda == -1) usePcf8574 = false;
|
||||||
periodicBroadcastSec = top[FPSTR(_broadcast)] | periodicBroadcastSec;
|
periodicBroadcastSec = top[FPSTR(_broadcast)] | periodicBroadcastSec;
|
||||||
periodicBroadcastSec = min(900,max(0,(int)periodicBroadcastSec));
|
periodicBroadcastSec = min(900,max(0,(int)periodicBroadcastSec));
|
||||||
HAautodiscovery = top[FPSTR(_HAautodiscovery)] | HAautodiscovery;
|
HAautodiscovery = top[FPSTR(_HAautodiscovery)] | HAautodiscovery;
|
||||||
@ -640,19 +800,9 @@ class MultiRelay : public Usermod {
|
|||||||
DEBUG_PRINTLN(F(" config (re)loaded."));
|
DEBUG_PRINTLN(F(" config (re)loaded."));
|
||||||
}
|
}
|
||||||
// use "return !top["newestParameter"].isNull();" when updating Usermod with new features
|
// use "return !top["newestParameter"].isNull();" when updating Usermod with new features
|
||||||
return !top[FPSTR(_HAautodiscovery)].isNull();
|
return !top[FPSTR(_pcf8574)].isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!).
|
|
||||||
* This could be used in the future for the system to determine whether your usermod is installed.
|
|
||||||
*/
|
|
||||||
uint16_t getId()
|
|
||||||
{
|
|
||||||
return USERMOD_ID_MULTI_RELAY;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// strings to reduce flash memory usage (used more than twice)
|
// strings to reduce flash memory usage (used more than twice)
|
||||||
const char MultiRelay::_name[] PROGMEM = "MultiRelay";
|
const char MultiRelay::_name[] PROGMEM = "MultiRelay";
|
||||||
const char MultiRelay::_enabled[] PROGMEM = "enabled";
|
const char MultiRelay::_enabled[] PROGMEM = "enabled";
|
||||||
@ -663,3 +813,5 @@ const char MultiRelay::_external[] PROGMEM = "external";
|
|||||||
const char MultiRelay::_button[] PROGMEM = "button";
|
const char MultiRelay::_button[] PROGMEM = "button";
|
||||||
const char MultiRelay::_broadcast[] PROGMEM = "broadcast-sec";
|
const char MultiRelay::_broadcast[] PROGMEM = "broadcast-sec";
|
||||||
const char MultiRelay::_HAautodiscovery[] PROGMEM = "HA-autodiscovery";
|
const char MultiRelay::_HAautodiscovery[] PROGMEM = "HA-autodiscovery";
|
||||||
|
const char MultiRelay::_pcf8574[] PROGMEM = "use-PCF8574";
|
||||||
|
const char MultiRelay::_pcfAddress[] PROGMEM = "first-PCF8574";
|
||||||
|
Loading…
Reference in New Issue
Block a user