Skip to content

Commit

Permalink
[INDICATOR] Dimmable RGB (#1801)
Browse files Browse the repository at this point in the history
  • Loading branch information
1technophile authored Nov 14, 2023
1 parent cf21aed commit cf285fb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
6 changes: 6 additions & 0 deletions docs/use/gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 12 additions & 1 deletion main/User_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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); \
Expand All @@ -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); \
Expand Down Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions main/ZactuatorONOFF.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
}

Expand Down
11 changes: 11 additions & 0 deletions main/ZmqttDiscovery.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 35 additions & 5 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSON_MSG_BUFFER> jsonBuffer;
preferences.begin(Gateway_Short_Name, true);
Expand Down Expand Up @@ -1004,14 +1010,19 @@ 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();
SetupIndicatorInfo();
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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2817,6 +2828,22 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
stateMeasures();
}
}
# ifdef RGB_INDICATORS
if (SYSdata.containsKey("rgbbrightness") && SYSdata["rgbbrightness"].is<float>()) {
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<bool>()) {
SYSConfig.ohdiscovery = SYSdata["ohdiscovery"];
Expand Down Expand Up @@ -2990,6 +3017,9 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
JsonObject jo = jsonBuffer.to<JsonObject>();
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);
Expand Down

0 comments on commit cf285fb

Please sign in to comment.