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 binding field explanations:

broker's name (defined in openhab.cfg)
ESPEasy %sysname%. Basically, a module name
GPIO number to control
openHAB will send a command
what to send (“1”) when state changes to on
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> {

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

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 powered by Disqus