Browse Source

Report brightness via MQTT

fastled
Xose Pérez 7 years ago
parent
commit
46d86eb6c9
1 changed files with 19 additions and 10 deletions
  1. +19
    -10
      code/espurna/light.ino

+ 19
- 10
code/espurna/light.ino View File

@ -237,21 +237,27 @@ void lightColor(bool save, bool forward) {
_lightProviderSet(_lightState, _lightColor[0], _lightColor[1], _lightColor[2], brightness); _lightProviderSet(_lightState, _lightColor[0], _lightColor[1], _lightColor[2], brightness);
char rgb[8];
_color_array2rgb(_lightColor, brightness, rgb);
// Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily // Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily
if (save) colorTicker.once(LIGHT_SAVE_DELAY, _lightColorSave); if (save) colorTicker.once(LIGHT_SAVE_DELAY, _lightColorSave);
// Report color to MQTT broker
// Report color & brightness to MQTT broker
if (forward) { if (forward) {
char rgb[8];
_color_array2rgb(_lightColor, 1.0, rgb);
mqttSend(MQTT_TOPIC_COLOR, rgb); mqttSend(MQTT_TOPIC_COLOR, rgb);
char buffer[5];
sprintf(buffer, "%d", (int) (brightness * LIGHT_MAX_BRIGHTNESS));
mqttSend(MQTT_TOPIC_BRIGHTNESS, buffer);
} }
// Report color to WS clients // Report color to WS clients
char message[64];
sprintf(message, "{\"color\": \"%s\", \"brightness\": %d}", rgb, (int) (brightness * LIGHT_MAX_BRIGHTNESS));
wsSend(message);
{
char rgb[8];
_color_array2rgb(_lightColor, brightness, rgb);
char message[64];
sprintf(message, "{\"color\": \"%s\"}", rgb);
wsSend(message);
}
} }
@ -358,14 +364,17 @@ void lightSetup() {
}, },
[](const char * payload) { [](const char * payload) {
parseColor(payload); parseColor(payload);
lightColor(true, mqttForward());
lightColor(true, true);
} }
); );
apiRegister(MQTT_TOPIC_BRIGHTNESS, MQTT_TOPIC_BRIGHTNESS, apiRegister(MQTT_TOPIC_BRIGHTNESS, MQTT_TOPIC_BRIGHTNESS,
NULL,
[](char * buffer, size_t len) {
snprintf(buffer, len, "%d", (int) (brightness * LIGHT_MAX_BRIGHTNESS));
},
[](const char * payload) { [](const char * payload) {
lightColor(true, mqttForward());
brightness = (float) atoi(payload) / LIGHT_MAX_BRIGHTNESS;
lightColor(true, true);
} }
); );


Loading…
Cancel
Save