ESP8266 controlled staircase LEDs

I have always been interested in LEDs of all kinds. I have also heard about ESP8266 Wi-Fi microchip but haven’t used it before. This time I did combine these two for lighting up our staircase and this is what it came out of it.

I had chosen to use a COB type of LEDs and had already purchased Meanwell NPF-60D-24 power supply for it. This (and many other) Meanwell’s power supply has a “3 in 1 dimming” capability. First, I tried it out with a potentiometer and thus a “fixed” dimming level. Obviously, it worked. But hey, what the fun in that one?!

Led strip installed under the hand rails

Another way of dimming is PWM based. So, I started thinking it would be nice to have a dynamic dimming for the lights. As I have already described in the “data from home” – blog series, I do have a Prometheus database having all sorts of time series data about my home. One of those time series happens to be the outside illuminance level in lux. Wouldn’t it be nice to dim those staircase LEDs dynamically based on that information?

I started gathering the information on the topic and came across DIY Guide list of example applications for arduino. That explains the very same scenario of dimming a Meanwell led driver with microcontroller generated PWM signal. Then I made a couple of observations: 1) Arduiono boards tend to be quite pricy if you want to have WLAN capability. 2) If I understood it right, those tend be quite slow to boot up.

I then asked a friend of mine for ideas how to solve this. He reminded me about the ESP8266 microcontroller. It is cheap, has WLAN and boots up with almost no latency and it even supports micro python! So I grabbed a couple of Wemos D1 mini’s from our local store. Then I found a nice blog of “Random Nerd Tutorials – Getting Started with Thonny MicroPython (Python) IDE for ESP32 and ESP8266” that helped me to get started.

This is the idea of my code. When the lights are turned on, also the Wemos gets powered (by a DC-DC stepdown), it then connects to my WLAN and then gets the lux value from the Prometheus http API. This is then used to set the dimming level. The process repeats every 10 minutes. When the lights are turned on, those will be at full brightness for a couple of seconds and then the dimming will kick in.

import network
import urequests
from machine import Pin, PWM
from time import sleep

# connect to the WLAN
sta_if = network.WLAN(network.STA_IF);
sta_if.connect("SSID", "PASSWORD")

# define convert function limiting min and max values
def convert(x, in_min, in_max, out_min, out_max):
    v1 = (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
    if(v1 > out_min):
        return out_min
    elif(v1 < out_max):
        return out_max
        return v1

# define PWM pin
pwm = machine.PWM(machine.Pin(4, Pin.OUT))

# loop requesting the last lux value every 10 minutes and setting 
# pwm duty accordingly, PWM range is from 0 to 1023, this time limiting 
# it to 400 to 825
while True:
        response = urequests.get("http://PROMETHEUSIP:9090/api/v1/query?query=EXPRESSIONSTRING")
        parsed = response.json()
        value = int(float(parsed["data"]["result"][0]["value"][1]))
        d0 = (convert(value, 0, 1500, 825, 400))
        # error during process, try again in 1 minute

I’m sure there will be some iterations to make it even better. Right now I’m happy with the result and delighted about how easy it was.

Stairs illuminated by LEDs

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.