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] 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