Browse Source

Manage relay changes in third party modules via broker

refactor-terminal
Xose Pérez 5 years ago
parent
commit
cb8d9c344d
6 changed files with 74 additions and 39 deletions
  1. +17
    -0
      code/espurna/config/dependencies.h
  2. +16
    -3
      code/espurna/domoticz.ino
  3. +12
    -0
      code/espurna/influxdb.ino
  4. +13
    -0
      code/espurna/led.ino
  5. +1
    -33
      code/espurna/relay.ino
  6. +15
    -3
      code/espurna/thinkspeak.ino

+ 17
- 0
code/espurna/config/dependencies.h View File

@ -33,9 +33,26 @@
#define DEBUG_SERIAL_SUPPORT 0 #define DEBUG_SERIAL_SUPPORT 0
#endif #endif
#if ALEXA_SUPPORT
#undef BROKER_SUPPORT
#define BROKER_SUPPORT 1 // If Alexa enabled enable BROKER
#endif
#if INFLUXDB_SUPPORT
#undef BROKER_SUPPORT
#define BROKER_SUPPORT 1 // If InfluxDB enabled enable BROKER
#endif
#if THINKSPEAK_SUPPORT
#undef BROKER_SUPPORT
#define BROKER_SUPPORT 1 // If Thingspeak enabled enable BROKER
#endif
#if DOMOTICZ_SUPPORT #if DOMOTICZ_SUPPORT
#undef MQTT_SUPPORT #undef MQTT_SUPPORT
#define MQTT_SUPPORT 1 // If Domoticz enabled enable MQTT #define MQTT_SUPPORT 1 // If Domoticz enabled enable MQTT
#undef BROKER_SUPPORT
#define BROKER_SUPPORT 1 // If Domoticz enabled enable BROKER
#endif #endif
#if HOMEASSISTANT_SUPPORT #if HOMEASSISTANT_SUPPORT


+ 16
- 3
code/espurna/domoticz.ino View File

@ -80,6 +80,15 @@ void _domoticzMqtt(unsigned int type, const char * topic, const char * payload)
}; };
#if BROKER_SUPPORT
void _domoticzBrokerCallback(const char * topic, unsigned char id, const char * payload) {
if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
unsigned char value = atoi(payload);
domoticzSendRelay(id, value == 1);
}
}
#endif // BROKER_SUPPORT
#if WEB_SUPPORT #if WEB_SUPPORT
bool _domoticzWebSocketOnReceive(const char * key, JsonVariant& value) { bool _domoticzWebSocketOnReceive(const char * key, JsonVariant& value) {
@ -137,16 +146,16 @@ template<typename T> void domoticzSend(const char * key, T nvalue) {
domoticzSend(key, nvalue, ""); domoticzSend(key, nvalue, "");
} }
void domoticzSendRelay(unsigned char relayID) {
void domoticzSendRelay(unsigned char relayID, bool status) {
if (!_dcz_enabled) return; if (!_dcz_enabled) return;
char buffer[15]; char buffer[15];
snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID); snprintf_P(buffer, sizeof(buffer), PSTR("dczRelayIdx%u"), relayID);
domoticzSend(buffer, relayStatus(relayID) ? "1" : "0");
domoticzSend(buffer, status ? "1" : "0");
} }
void domoticzSendRelays() { void domoticzSendRelays() {
for (uint8_t relayID=0; relayID < relayCount(); relayID++) { for (uint8_t relayID=0; relayID < relayCount(); relayID++) {
domoticzSendRelay(relayID);
domoticzSendRelay(relayID, relayStatus(relayID));
} }
} }
@ -165,6 +174,10 @@ void domoticzSetup() {
wsOnReceiveRegister(_domoticzWebSocketOnReceive); wsOnReceiveRegister(_domoticzWebSocketOnReceive);
#endif #endif
#if BROKER_SUPPORT
brokerRegister(_domoticzBrokerCallback);
#endif
// Callbacks // Callbacks
mqttRegister(_domoticzMqtt); mqttRegister(_domoticzMqtt);
espurnaRegisterReload(_domoticzConfigure); espurnaRegisterReload(_domoticzConfigure);


+ 12
- 0
code/espurna/influxdb.ino View File

@ -38,6 +38,14 @@ void _idbConfigure() {
} }
} }
#if BROKER_SUPPORT
void _idbBrokerCallback(const char * topic, unsigned char id, const char * payload) {
if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
idbSend(topic, id, (char *) payload);
}
}
#endif // BROKER_SUPPORT
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
bool idbSend(const char * topic, const char * payload) { bool idbSend(const char * topic, const char * payload) {
@ -108,6 +116,10 @@ void idbSetup() {
wsOnReceiveRegister(_idbWebSocketOnReceive); wsOnReceiveRegister(_idbWebSocketOnReceive);
#endif #endif
#if BROKER_SUPPORT
brokerRegister(_idbBrokerCallback);
#endif
// Main callbacks // Main callbacks
espurnaRegisterReload(_idbConfigure); espurnaRegisterReload(_idbConfigure);


+ 13
- 0
code/espurna/led.ino View File

@ -73,6 +73,14 @@ void _ledWebSocketOnSend(JsonObject& root) {
#endif #endif
#if BROKER_SUPPORT
void _ledBrokerCallback(const char * topic, unsigned char id, const char * payload) {
if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
ledUpdate(true);
}
}
#endif // BROKER_SUPPORT
#if MQTT_SUPPORT #if MQTT_SUPPORT
void _ledMQTTCallback(unsigned int type, const char * topic, const char * payload) { void _ledMQTTCallback(unsigned int type, const char * topic, const char * payload) {
@ -173,6 +181,11 @@ void ledSetup() {
wsOnReceiveRegister(_ledWebSocketOnReceive); wsOnReceiveRegister(_ledWebSocketOnReceive);
#endif #endif
#if BROKER_SUPPORT
brokerRegister(_ledBrokerCallback);
#endif
DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size()); DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size());
// Main callbacks // Main callbacks


+ 1
- 33
code/espurna/relay.ino View File

@ -164,7 +164,7 @@ void _relayProcess(bool mode) {
// Only process the relays we have to change // Only process the relays we have to change
if (target == _relays[id].current_status) continue; if (target == _relays[id].current_status) continue;
// Only process the relays we have change to the requested mode
// Only process the relays we have to change to the requested mode
if (target != mode) continue; if (target != mode) continue;
// Only process if the change_time has arrived // Only process if the change_time has arrived
@ -201,24 +201,6 @@ void _relayProcess(bool mode) {
} }
#if DOMOTICZ_SUPPORT
domoticzSendRelay(id);
#endif
#if INFLUXDB_SUPPORT
relayInfluxDB(id);
#endif
#if THINGSPEAK_SUPPORT
tspkEnqueueRelay(id, target);
tspkFlush();
#endif
// Flag relay-based LEDs to update status
#if LED_SUPPORT
ledUpdate(true);
#endif
_relays[id].report = false; _relays[id].report = false;
_relays[id].group_report = false; _relays[id].group_report = false;
@ -993,20 +975,6 @@ void relaySetupMQTT() {
#endif #endif
//------------------------------------------------------------------------------
// InfluxDB
//------------------------------------------------------------------------------
#if INFLUXDB_SUPPORT
void relayInfluxDB(unsigned char id) {
if (id >= _relays.size()) return;
idbSend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0");
}
#endif
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Settings // Settings
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------


+ 15
- 3
code/espurna/thinkspeak.ino View File

@ -35,6 +35,16 @@ unsigned char _tspk_tries = 0;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if BROKER_SUPPORT
void _tspkBrokerCallback(const char * topic, unsigned char id, const char * payload) {
if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
tspkEnqueueRelay(id, (char *) payload);
tspkFlush();
}
}
#endif // BROKER_SUPPORT
#if WEB_SUPPORT #if WEB_SUPPORT
bool _tspkWebSocketOnReceive(const char * key, JsonVariant& value) { bool _tspkWebSocketOnReceive(const char * key, JsonVariant& value) {
@ -259,12 +269,10 @@ void _tspkFlush() {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
bool tspkEnqueueRelay(unsigned char index, unsigned char status) {
bool tspkEnqueueRelay(unsigned char index, char * payload) {
if (!_tspk_enabled) return true; if (!_tspk_enabled) return true;
unsigned char id = getSetting("tspkRelay", index, 0).toInt(); unsigned char id = getSetting("tspkRelay", index, 0).toInt();
if (id > 0) { if (id > 0) {
char payload[3] = {0};
itoa(status ? 1 : 0, payload, 10);
_tspkEnqueue(id, payload); _tspkEnqueue(id, payload);
return true; return true;
} }
@ -298,6 +306,10 @@ void tspkSetup() {
wsOnReceiveRegister(_tspkWebSocketOnReceive); wsOnReceiveRegister(_tspkWebSocketOnReceive);
#endif #endif
#if BROKER_SUPPORT
brokerRegister(_tspkBrokerCallback);
#endif
DEBUG_MSG_P(PSTR("[THINGSPEAK] Async %s, SSL %s\n"), DEBUG_MSG_P(PSTR("[THINGSPEAK] Async %s, SSL %s\n"),
THINGSPEAK_USE_ASYNC ? "ENABLED" : "DISABLED", THINGSPEAK_USE_ASYNC ? "ENABLED" : "DISABLED",
THINGSPEAK_USE_SSL ? "ENABLED" : "DISABLED" THINGSPEAK_USE_SSL ? "ENABLED" : "DISABLED"


Loading…
Cancel
Save