Quick note: openHAB/ESPEasy/MQTT and easy fading



Connecting ESPEasy's GPIO outputs to switch elements in openHAB, using MQTT is quite simple:

Switch sD1MINI02MOS "Countertop light [%s]" <whites> {
  mqtt=">[motherqtt:/d1mini02/gpio/5:command:ON:1],>[motherqtt:/d1mini02/gpio/5:command:OFF:0]"
}

MQTT binding field explanations:

motherqtt
broker's name (defined in openhab.cfg)
/d1mini02/
ESPEasy %sysname%. Basically, a module name
gpio/5
GPIO number to control
command
openHAB will send a command
ON:1
what to send (“1”) when state changes to on
OFF:0
ditto when switching off

Switching ON/OFF gets boring quite fast. Luckily, ESPEasy exposes PWM (pulse width modulation) control of GPIO outputs. This gives possibility to vary brightness of light sources controlled by GPIOs. Let's modify the item definition to have light dimmed when set to OFF (not turned fully off):

Switch sD1MINI02MOS "Countertop light [%s]" <whites> {
  mqtt=">[motherqtt:/d1mini02/pwm/5:command:ON:1023],>[motherqtt:/d1mini02/pwm/5:command:OFF:128]"
}

Compared to direct gpio control: gpio became pwm, and 1/0 for ON/OFF became 1023 (fully filled impulse) and 128 (one eighth of full brightness).

Two things to remember: while ESPEasy's range of PWM is 0÷1023, openHAB's dimmer type only works support 0÷100 (like in percents). You cannot directly drive /pwm/ with openHAB's values, you need transforms. Second, when you start using /pwm/, standard /gpio/ stops working. You need to set /pwm/ to 0 to have /gpio/ control work again.

PWM fading is quite nice, but with binary ON/OFF state not very useful. But! Look closely at ESPEasy GPIO command set. There's a PWM command version with <duration>. This is useful for having lights turn on and off softly, instead of abruptly.

This requires slightly more complicated definition. We cannot use /pwm/ directly, instead control the /cmd endpoint:

Switch sD1MINI02MOS "Countertop light [%s]" <whites> {
  mqtt=">[motherqtt:/d1mini02/cmd:command:ON:PWM,5,1023,200],>[motherqtt:/d1mini02/cmd:command:OFF:PWM,5,0,600]"
}

Above definition will give pleasant, 200ms long fade in when turned ON, and 600ms long fade out when turned OFF. Compared to first definition, following has changed:

  • /gpio/5 (or /pwm/5) became /cmd
  • 1 (or 1023) for ON became PWM,5,1023,200 – control PWM, on GPIO number 5, set fill to 1023 during 200ms
  • symetrically, 0 (or 128) for OFF became PWM,5,0,600 – fade GPIO 5 to 0 during 600 ms

You provide final PWM value, fade starts from current value. Happy fading! :)

Comments


Comments powered by Disqus