I picked up ThePiHut’s RGB Xmas Tree during Black Friday on a whim. Nominally it was an excuse to build something with the kids, introducing them to electronics and coding the same way I was (“if you break it, you fix it”)… but let’s be honest, this was a new fun you for me (and a way to use one of the constituents of my old RPi3B cluster).
You can use the provided library to control it, assuming you’re happy with Python and the command line, and I although I think this is properly cool the only way to make this acceptable for the rest of my family to display anywhere outside of my office is to make it easy to control.
So let’s make it happen by using Flask to run a local web server.
The first step is to make your Pi accessible over SSH so it can be run headless and peripheral-less. Then install dependencies:
sudo apt update
sudo apt install python3-pip
git clone https://github.com/ThePiHut/rgbxmastree
Next we need to a virtual environment in which we will run Flask, and the gpizero library, so Python can access the RPi GPIO:
python3 -m venv ~/rgbxmas_venv
source ~/rgbxmas_venv/bin/activate
pip install flask
pip install gpiozero
Now we essentially need two new files in the rgbxmastree
folder - app.py
to handle interactions with the lights by utilising the provided library in tree.py
, and a HTML frontend in templates\index.html
.
These can be as simple or as complex as you like, but I’ve put scripts that look like this on my Github if that helps.
This code also runs the four example scripts provided by TPH, but does need you to copy tree.py
into that folder using cp tree.py examples\tree.py
. Finally run the app with python app.py
.
Visit the website displayed on your PC or phone (something like http://yourhostname.local:8080
) and have festive Pi fun.
Lastly let’s set up a daemon to start the webserver on boot. Add a new service file with sudo nano /etc/systemd/system/rgbxmastree.service
and add:
[Unit]
Description=RGB Xmas Tree Flask App
After=network.target
[Service]
ExecStart=/home/[username]/rgbxmas_venv/bin/python /home/xmas/rgbxmastree/app.py
WorkingDirectory=/home/[username]/rgbxmastree
Environment="PATH=/home/[username]/rgbxmas_venv/bin:/usr/bin:/bin"
Restart=always
User=[username]
[Install]
WantedBy=multi-user.target
Enable the service to start on boot with sudo systemctl enable rgbxmastree
, and start it with sudo systemctl start rgbxmastree
. Verify that it’s running with sudo systemctl status rgbxmastree
then sudo reboot
to get it started.