From 356be730e28cb7f7fb0202e31bcb177ad180abd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Thu, 10 Jan 2019 09:39:01 +0100 Subject: [PATCH 1/2] Added message type to broker --- code/espurna/alexa.ino | 5 ++++- code/espurna/broker.ino | 12 ++++++------ code/espurna/config/prototypes.h | 2 +- code/espurna/config/types.h | 9 +++++++++ code/espurna/domoticz.ino | 7 ++++++- code/espurna/influxdb.ino | 7 +++++-- code/espurna/led.ino | 7 ++++++- code/espurna/light.ino | 2 +- code/espurna/ntp.ino | 2 +- code/espurna/relay.ino | 2 +- code/espurna/sensor.ino | 10 +--------- code/espurna/thinkspeak.ino | 13 +++++++++++-- 12 files changed, 52 insertions(+), 26 deletions(-) diff --git a/code/espurna/alexa.ino b/code/espurna/alexa.ino index 0388c3eb..c4449f22 100644 --- a/code/espurna/alexa.ino +++ b/code/espurna/alexa.ino @@ -46,8 +46,11 @@ bool _alexaRequestCallback(AsyncWebServerRequest *request) { } #if BROKER_SUPPORT -void _alexaBrokerCallback(const char * topic, unsigned char id, const char * payload) { +void _alexaBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) { + // Only process status messages + if (BROKER_MSG_TYPE_STATUS != type) return; + unsigned char value = atoi(payload); if (strcmp(MQTT_TOPIC_CHANNEL, topic) == 0) { diff --git a/code/espurna/broker.ino b/code/espurna/broker.ino index 6db86e20..d154c4ab 100644 --- a/code/espurna/broker.ino +++ b/code/espurna/broker.ino @@ -10,23 +10,23 @@ Copyright (C) 2017-2018 by Xose PĂ©rez #include -std::vector _broker_callbacks; +std::vector _broker_callbacks; // ----------------------------------------------------------------------------- -void brokerRegister(void (*callback)(const char *, unsigned char, const char *)) { +void brokerRegister(void (*callback)(const unsigned char, const char *, unsigned char, const char *)) { _broker_callbacks.push_back(callback); } -void brokerPublish(const char * topic, unsigned char id, const char * message) { +void brokerPublish(const unsigned char type, const char * topic, unsigned char id, const char * message) { //DEBUG_MSG_P(PSTR("[BROKER] Message %s[%u] => %s\n"), topic, id, message); for (unsigned char i=0; i<_broker_callbacks.size(); i++) { - (_broker_callbacks[i])(topic, id, message); + (_broker_callbacks[i])(type, topic, id, message); } } -void brokerPublish(const char * topic, const char * message) { - brokerPublish(topic, 0, message); +void brokerPublish(const unsigned char type, const char * topic, const char * message) { + brokerPublish(type, topic, 0, message); } #endif // BROKER_SUPPORT diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 6ceb1cf8..daeeb78a 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -24,7 +24,7 @@ extern "C" { // Broker // ----------------------------------------------------------------------------- #if BROKER_SUPPORT - void brokerRegister(void (*)(const char *, unsigned char, const char *)); + void brokerRegister(void (*)(const unsigned char, const char *, unsigned char, const char *)); #endif // ----------------------------------------------------------------------------- diff --git a/code/espurna/config/types.h b/code/espurna/config/types.h index 258e127c..83923293 100644 --- a/code/espurna/config/types.h +++ b/code/espurna/config/types.h @@ -3,6 +3,15 @@ // Do not touch this definitions //------------------------------------------------------------------------------ +// ----------------------------------------------------------------------------- +// BROKER +// ----------------------------------------------------------------------------- + +#define BROKER_MSG_TYPE_SYSTEM 0 +#define BROKER_MSG_TYPE_DATETIME 1 +#define BROKER_MSG_TYPE_STATUS 2 +#define BROKER_MSG_TYPE_SENSOR 3 + // ----------------------------------------------------------------------------- // WIFI // ----------------------------------------------------------------------------- diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index 06bd602c..98f89671 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -149,11 +149,16 @@ 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) { +void _domoticzBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) { + + // Only process status messages + if (BROKER_MSG_TYPE_STATUS != type) return; + if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) { unsigned char value = atoi(payload); domoticzSendRelay(id, value == 1); } + } #endif // BROKER_SUPPORT diff --git a/code/espurna/influxdb.ino b/code/espurna/influxdb.ino index d2b3b330..06319e39 100644 --- a/code/espurna/influxdb.ino +++ b/code/espurna/influxdb.ino @@ -39,10 +39,13 @@ void _idbConfigure() { } #if BROKER_SUPPORT -void _idbBrokerCallback(const char * topic, unsigned char id, const char * payload) { - if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) { +void _idbBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) { + + // Only process status & senssor messages + if ((BROKER_MSG_TYPE_STATUS == type) || (BROKER_MSG_TYPE_SENSOR == type)) { idbSend(topic, id, (char *) payload); } + } #endif // BROKER_SUPPORT diff --git a/code/espurna/led.ino b/code/espurna/led.ino index eda1f339..01999aad 100644 --- a/code/espurna/led.ino +++ b/code/espurna/led.ino @@ -74,10 +74,15 @@ void _ledWebSocketOnSend(JsonObject& root) { #endif #if BROKER_SUPPORT -void _ledBrokerCallback(const char * topic, unsigned char id, const char * payload) { +void _ledBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) { + + // Only process status messages + if (BROKER_MSG_TYPE_STATUS != type) return; + if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) { ledUpdate(true); } + } #endif // BROKER_SUPPORT diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 3269ed8d..866c9a22 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -640,7 +640,7 @@ void lightBroker() { char buffer[10]; for (unsigned int i=0; i < _light_channel.size(); i++) { itoa(_light_channel[i].inputValue, buffer, 10); - brokerPublish(MQTT_TOPIC_CHANNEL, i, buffer); + brokerPublish(BROKER_MSG_TYPE_STATUS, MQTT_TOPIC_CHANNEL, i, buffer); } } diff --git a/code/espurna/ntp.ino b/code/espurna/ntp.ino index 8eadb77c..555017c5 100644 --- a/code/espurna/ntp.ino +++ b/code/espurna/ntp.ino @@ -108,7 +108,7 @@ void _ntpLoop() { static unsigned char last_minute = 60; if (ntpSynced() && (minute() != last_minute)) { last_minute = minute(); - brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str()); + brokerPublish(BROKER_MSG_TYPE_DATETIME, MQTT_TOPIC_DATETIME, ntpDateTime().c_str()); } #endif diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 897b19d6..b5460aaf 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -177,7 +177,7 @@ void _relayProcess(bool mode) { // Send to Broker #if BROKER_SUPPORT - brokerPublish(MQTT_TOPIC_RELAY, id, target ? "1" : "0"); + brokerPublish(BROKER_MSG_TYPE_STATUS, MQTT_TOPIC_RELAY, id, target ? "1" : "0"); #endif // Send MQTT diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 8e9e99a7..aa97ba02 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -1172,7 +1172,7 @@ void _sensorReport(unsigned char index, double value) { dtostrf(value, 1-sizeof(buffer), decimals, buffer); #if BROKER_SUPPORT - brokerPublish(magnitudeTopic(magnitude.type).c_str(), magnitude.local, buffer); + brokerPublish(BROKER_MSG_TYPE_SENSOR ,magnitudeTopic(magnitude.type).c_str(), magnitude.local, buffer); #endif #if MQTT_SUPPORT @@ -1191,14 +1191,6 @@ void _sensorReport(unsigned char index, double value) { #endif // MQTT_SUPPORT - #if INFLUXDB_SUPPORT - if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 1)) { - idbSend(magnitudeTopic(magnitude.type).c_str(), magnitude.global, buffer); - } else { - idbSend(magnitudeTopic(magnitude.type).c_str(), buffer); - } - #endif // INFLUXDB_SUPPORT - #if THINGSPEAK_SUPPORT tspkEnqueueMeasurement(index, buffer); #endif diff --git a/code/espurna/thinkspeak.ino b/code/espurna/thinkspeak.ino index 8db7973e..306d7666 100644 --- a/code/espurna/thinkspeak.ino +++ b/code/espurna/thinkspeak.ino @@ -36,11 +36,20 @@ 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) { +void _tspkBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) { + + // Process status messages + if (BROKER_MSG_TYPE_STATUS == type) { tspkEnqueueRelay(id, (char *) payload); tspkFlush(); } + + // Porcess sensor messages + if (BROKER_MSG_TYPE_SENSOR == type) { + //tspkEnqueueMeasurement(id, (char *) payload); + //tspkFlush(); + } + } #endif // BROKER_SUPPORT From cc4ba52b4d16201fbf7c0e1ae5d52a1524df263e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Thu, 10 Jan 2019 10:48:10 +0100 Subject: [PATCH 2/2] Delay light comms (mqtt, ws, broker) to avooid jamming --- code/espurna/config/general.h | 4 ++++ code/espurna/config/hardware.h | 2 +- code/espurna/light.ino | 41 +++++++++++++++++++++------------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index b79c6523..7df60847 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -881,6 +881,10 @@ #define LIGHT_SAVE_ENABLED 1 // Light channel values saved by default after each change #endif +#ifndef LIGHT_COMMS_DELAY +#define LIGHT_COMMS_DELAY 100 // Delay communication after light update (in ms) +#endif + #ifndef LIGHT_SAVE_DELAY #define LIGHT_SAVE_DELAY 5 // Persist color after 5 seconds to avoid wearing out #endif diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index 007cb99f..790acf0b 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -112,7 +112,7 @@ #define MANUFACTURER "WEMOS" #define DEVICE "D1_MINI_RELAYSHIELD" - // Buttons + // Buttons // No buttons on the D1 MINI alone, but defining it without adding a button doen't create problems #define BUTTON1_PIN 0 // Connect a pushbutton between D3 and GND, // it's the same as using a Wemos one button shield diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 866c9a22..6ae8aedb 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -25,6 +25,7 @@ extern "C" { // ----------------------------------------------------------------------------- +Ticker _light_comms_ticker; Ticker _light_save_ticker; Ticker _light_transition_ticker; @@ -658,6 +659,26 @@ bool lightHasColor() { return _light_has_color; } +void _lightComms(unsigned char mask) { + + // Report color & brightness to MQTT broker + #if MQTT_SUPPORT + if (mask & 0x01) lightMQTT(); + if (mask & 0x02) lightMQTTGroup(); + #endif + + // Report color to WS clients (using current brightness setting) + #if WEB_SUPPORT + wsSend(_lightWebSocketOnSend); + #endif + + // Report channels to local broker + #if BROKER_SUPPORT + lightBroker(); + #endif + +} + void lightUpdate(bool save, bool forward, bool group_forward) { _generateBrightness(); @@ -672,21 +693,11 @@ void lightUpdate(bool save, bool forward, bool group_forward) { _light_steps_left = _light_use_transitions ? _light_transition_time / LIGHT_TRANSITION_STEP : 1; _light_transition_ticker.attach_ms(LIGHT_TRANSITION_STEP, _lightProviderUpdate); - // Report channels to local broker - #if BROKER_SUPPORT - lightBroker(); - #endif - - // Report color & brightness to MQTT broker - #if MQTT_SUPPORT - if (forward) lightMQTT(); - if (group_forward) lightMQTTGroup(); - #endif - - // Report color to WS clients (using current brightness setting) - #if WEB_SUPPORT - wsSend(_lightWebSocketOnSend); - #endif + // Delay every communication 100ms to avoid jamming + unsigned char mask = 0; + if (forward) mask += 1; + if (group_forward) mask += 2; + _light_comms_ticker.once_ms(LIGHT_COMMS_DELAY, _lightComms, mask); #if LIGHT_SAVE_ENABLED // Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily