From 01f4f207306624d824ddbfbd29f078532be760be Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Mon, 13 Nov 2023 20:38:50 -0600 Subject: [PATCH] [INDICATOR] Dimmable RGB --- docs/use/gateway.md | 6 ++++++ main/User_config.h | 13 ++++++++++++- main/ZactuatorONOFF.ino | 14 +++++++++----- main/ZmqttDiscovery.ino | 11 +++++++++++ main/main.ino | 40 +++++++++++++++++++++++++++++++++++----- 5 files changed, 73 insertions(+), 11 deletions(-) diff --git a/docs/use/gateway.md b/docs/use/gateway.md index 385526fc03..d1fed95129 100644 --- a/docs/use/gateway.md +++ b/docs/use/gateway.md @@ -201,6 +201,12 @@ The `server_cert` parameter is optional. If the update server has changed or cer The pre-built binaries for **rfbridge** and **avatto-bakeey-ir** have the above WiFi and MQTT broker credentials and the Firmware update via MQTT options disabled. This is due to the restricted available flash, so as to still be able to use OTA firmware updates for these boards. ::: +## Change the LED indicator brightness + +Minimum: 0, Maximum: 255, Default defined by DEAULT_ADJ_BRIGHTNESS + +`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m '{"brightness":200}'` + # State LED usage The gateway can support up to 3 LED to display its operating state: diff --git a/main/User_config.h b/main/User_config.h index 1b42e0513b..79aae05882 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -513,7 +513,10 @@ Adafruit_NeoPixel leds2(ANEOPIX_IND_NUM_LEDS, ANEOPIX_IND_DATA_GPIO2, ANEOPIX_IN # define RGB_LED_POWER -1 // If the RGB Led is linked to GPIO pin for power define it here # endif # ifndef ANEOPIX_BRIGHTNESS -# define ANEOPIX_BRIGHTNESS 20 // Set Default RGB brightness to approx 10% (0-255 scale) +# define ANEOPIX_BRIGHTNESS 20 // Set Default maximum RGB brightness to approx 10% (0-255 scale) +# endif +# ifndef DEAULT_ADJ_BRIGHTNESS +# define DEAULT_ADJ_BRIGHTNESS 255 // Set Default RGB adjustable brightness # endif # ifndef ANEOPIX_COLOR_SCHEME // allow for different color combinations # define ANEOPIX_COLOR_SCHEME 0 @@ -590,18 +593,21 @@ Adafruit_NeoPixel leds2(ANEOPIX_IND_NUM_LEDS, ANEOPIX_IND_DATA_GPIO2, ANEOPIX_IN # endif # define ErrorIndicatorON() \ leds.setPixelColor(ANEOPIX_ERROR_LED, ANEOPIX_ERROR); \ + leds.setBrightness(SYSConfig.rgbbrightness); \ leds.show(); # define ErrorIndicatorOFF() \ leds.setPixelColor(ANEOPIX_ERROR_LED, ANEOPIX_OFF); \ leds.show(); # define SendReceiveIndicatorON() \ leds.setPixelColor(ANEOPIX_SEND_RECEIVE_LED, ANEOPIX_SENDRECEIVE); \ + leds.setBrightness(SYSConfig.rgbbrightness); \ leds.show(); # define SendReceiveIndicatorOFF() \ leds.setPixelColor(ANEOPIX_SEND_RECEIVE_LED, ANEOPIX_OFF); \ leds.show(); # define InfoIndicatorON() \ leds.setPixelColor(ANEOPIX_INFO_LED, ANEOPIX_INFO); \ + leds.setBrightness(SYSConfig.rgbbrightness); \ leds.show(); # define InfoIndicatorOFF() \ leds.setPixelColor(ANEOPIX_INFO_LED, ANEOPIX_OFF); \ @@ -611,9 +617,11 @@ Adafruit_NeoPixel leds2(ANEOPIX_IND_NUM_LEDS, ANEOPIX_IND_DATA_GPIO2, ANEOPIX_IN // This enable to have persistence of the indicator to inform the user # define CriticalIndicatorON() \ leds2.setPixelColor(ANEOPIX_INFO_LED, ANEOPIX_CRITICAL); \ + leds2.setBrightness(255); \ leds2.show(); # define PowerIndicatorON() \ leds2.setPixelColor(ANEOPIX_INFO_LED, ANEOPIX_INFO); \ + leds2.setBrightness(SYSConfig.rgbbrightness); \ leds2.show(); # define PowerIndicatorOFF() \ leds2.setPixelColor(ANEOPIX_INFO_LED, ANEOPIX_OFF); \ @@ -762,6 +770,9 @@ unsigned long lastDiscovery = 0; // Time of the last discovery to trigger automa struct SYSConfig_s { bool discovery; // HA discovery convention bool ohdiscovery; // OH discovery specificities +# ifdef RGB_INDICATORS + int rgbbrightness; // brightness of the RGB LED +# endif }; #endif diff --git a/main/ZactuatorONOFF.ino b/main/ZactuatorONOFF.ino index 8cee244f30..70067ade1f 100644 --- a/main/ZactuatorONOFF.ino +++ b/main/ZactuatorONOFF.ino @@ -99,6 +99,14 @@ void ONOFFConfig_fromJson(JsonObject& ONOFFdata){}; void ONOFFConfig_load(){}; # endif +void updatePowerIndicator() { + if (digitalRead(ACTUATOR_ONOFF_GPIO) == ACTUATOR_ON) { + PowerIndicatorON(); + } else { + PowerIndicatorOFF(); + } +} + void setupONOFF() { # ifdef MAX_TEMP_ACTUATOR xTaskCreate(overLimitTemp, "overLimitTemp", 4000, NULL, 10, NULL); @@ -116,11 +124,7 @@ void setupONOFF() { if (ONOFFConfig.useLastStateOnStart) digitalWrite(ACTUATOR_ONOFF_GPIO, ONOFFConfig.ONOFFState); # endif - if (digitalRead(ACTUATOR_ONOFF_GPIO) == ACTUATOR_ON) { - PowerIndicatorON(); - } else { - PowerIndicatorOFF(); - } + updatePowerIndicator(); Log.trace(F("ZactuatorONOFF setup done" CR)); } diff --git a/main/ZmqttDiscovery.ino b/main/ZmqttDiscovery.ino index b2b5012ff1..d2984e3154 100644 --- a/main/ZmqttDiscovery.ino +++ b/main/ZmqttDiscovery.ino @@ -516,6 +516,17 @@ void pubMqttDiscovery() { stateClassNone, //State Class "false", "true" //state_off, state_on ); +# ifdef RGB_INDICATORS + createDiscovery("number", //set Type + subjectSYStoMQTT, "SYS: LED Brightness", (char*)getUniqueId("rgbbrightness", "").c_str(), //set state_topic,name,uniqueId + will_Topic, "", "{{ value_json.rgbbrightness/2.55}}", //set availability_topic,device_class,value_template, + "{\"rgbbrightness\":{{value*2.55}},\"save\":true}", "", "s", //set,payload_on,payload_off,unit_of_meas, + 0, //set off_delay + Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoSYSset, //set,payload_available,payload_not available ,is a gateway entity, command topic + "", "", "", "", false, // device name, device manufacturer, device model, device ID, retain, + stateClassNone //State Class + ); +# endif # ifdef ZdisplaySSD1306 createDiscovery("switch", //set Type diff --git a/main/main.ino b/main/main.ino index 0509c9b8b1..f03d56c05e 100644 --- a/main/main.ino +++ b/main/main.ino @@ -772,18 +772,24 @@ void delayWithOTA(long waitMillis) { void SYSConfig_init() { SYSConfig.discovery = true; SYSConfig.ohdiscovery = OpenHABDiscovery; +# ifdef RGB_INDICATORS + SYSConfig.rgbbrightness = DEAULT_ADJ_BRIGHTNESS; +# endif } void SYSConfig_fromJson(JsonObject& SYSdata) { Config_update(SYSdata, "discovery", SYSConfig.discovery); Config_update(SYSdata, "ohdiscovery", SYSConfig.ohdiscovery); +# ifdef RGB_INDICATORS + Config_update(SYSdata, "rgbbrightness", SYSConfig.rgbbrightness); +# endif } #else void SYSConfig_init() {} void SYSConfig_fromJson(JsonObject& SYSdata) {} #endif -#if defined(ESP32) && defined(ZmqttDiscovery) +#if defined(ESP32) void SYSConfig_load() { StaticJsonDocument jsonBuffer; preferences.begin(Gateway_Short_Name, true); @@ -1004,6 +1010,12 @@ void setup() { pinMode(TRIGGER_GPIO, INPUT_PULLUP); checkButton(); #endif + + delay(100); //give time to start the flash and avoid issue when reading the preferences + + SYSConfig_init(); + SYSConfig_load(); + //setup LED status SetupIndicatorError(); SetupIndicatorSendReceive(); @@ -1011,7 +1023,6 @@ void setup() { SetupIndicators(); // For RGB Leds #if defined(ESP8266) || defined(ESP32) - delay(100); //give time to start the flash and avoid issue when reading the preferences # ifdef ESP8266 # ifndef ZgatewaySRFB // if we are not in sonoff rf bridge case we apply the ESP8266 GPIO optimization Serial.end(); @@ -1117,9 +1128,6 @@ void setup() { modules.add(ZwebUI); #endif - SYSConfig_init(); - SYSConfig_load(); - #if defined(ESP8266) || defined(ESP32) if (mqtt_secure) { eClient = new WiFiClientSecure; @@ -2317,6 +2325,9 @@ String stateMeasures() { SYSdata["uptime"] = uptime(); SYSdata["version"] = OMG_VERSION; +# ifdef RGB_INDICATORS + SYSdata["rgbbrightness"] = SYSConfig.rgbbrightness; +# endif # ifdef ZmqttDiscovery SYSdata["discovery"] = SYSConfig.discovery; SYSdata["ohdiscovery"] = SYSConfig.ohdiscovery; @@ -2817,6 +2828,22 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding stateMeasures(); } } +# ifdef RGB_INDICATORS + if (SYSdata.containsKey("rgbbrightness") && SYSdata["rgbbrightness"].is()) { + if (SYSdata["rgbbrightness"] >= 0 && SYSdata["rgbbrightness"] <= 255) { + SYSConfig.rgbbrightness = round2(SYSdata["rgbbrightness"]); + leds.setBrightness(SYSConfig.rgbbrightness); + leds.show(); +# ifdef ZactuatorONOFF + updatePowerIndicator(); +# endif + Log.notice(F("RGB brightness: %d" CR), SYSConfig.rgbbrightness); + stateMeasures(); + } else { + Log.error(F("RGB brightness value invalid - ignoring command" CR)); + } + } +# endif # ifdef ZmqttDiscovery if (SYSdata.containsKey("ohdiscovery") && SYSdata["ohdiscovery"].is()) { SYSConfig.ohdiscovery = SYSdata["ohdiscovery"]; @@ -2990,6 +3017,9 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding JsonObject jo = jsonBuffer.to(); jo["discovery"] = SYSConfig.discovery; jo["ohdiscovery"] = SYSConfig.ohdiscovery; +# ifdef RGB_INDICATORS + jo["rgbbrightness"] = SYSConfig.rgbbrightness; +# endif // Save config into NVS (non-volatile storage) String conf = ""; serializeJson(jsonBuffer, conf);