Day 8 of Hanukkah at Anvil
Build a web app for each night of Hanukkah, with nothing but Python!
For the final night of Hanukkah we wanted to do something a little bit special. And that means Raspberry Pi! Inspired by Ian’s many remote controlled tree apps I decided to try playing with a Pi and LEDs myself.
Here’s the code that’s running on my Raspberry Pi Zero.
boot.sh
runs when the Pi starts up. It activates the Python virtual environment where we have our LED drivers installed, exports the UPLINK_KEY we’re using to connect to Anvil, and then runs the leds.py
main Python script.
cd /home/pi
. env/bin/activate
export UPLINK_KEY=<your uplink key goes here>
python leds.py
There are just two functions in leds.py
that we need: led_on
and led_off
- these let us individually control each LED in the strip of awesome APA 102c LEDs connected to the Pi. We decorate each of them with an anvil.server.callable
so we can call them from our web app:
@anvil.server.callable
def led_on(idx):
buffer[idx*4] = 0xe0 + MAX_BRIGHTNESS
update_leds()
@anvil.server.callable
def led_off(idx):
buffer[idx*4] = 0xe0
update_leds()
My Anvil app is very simple - just a couple of buttons I can use to switch the menorah on and off. The clever part is all in the code!
https://electric-menorah-a-go-go.anvil.app
I reused my How Many Candles HebCal querying code using Anvil’s handy Dependencies feature. This means my Electric Menorah code knows which night of Hanukkah it is and lights the correct number of LEDs each time without my needing to tell it. (Outside of the holiday it lights all 9, just to sanity check they are connected ok).
The Pi works everywhere in my house, it just needs to be connected to a power source and my WiFi network. So I could set it up in my front window and control it from the comfort of my couch with my phone:
If you want to take a look at the code, here’s a clone link:
Chag sameach everyone, and I hope you’ve enjoyed our little series of Hanukkah apps!
Give the Gift of Python
Share this post:
Get tomorrow's app in your inbox
Don't miss a day! We'll mail you a new web app every night of Hanukkah: