Flask Controlled WS2812b LED Strip
(Coming soon)
Once everything works, the current version's web UI will look like this:
Here's a small Fritz diagram of how to wire the Pi with your WS2812b LED strip:
I normally start the lite version of Raspberry Pi OS without desktop and recommended software so it usually doesn't come with Git installed. So let's install it:
sudo apt install git -y
After we install git, clone this repo:
git clone https://github.com/naztronaut/FCW.git
sudo mv FCW fcw
The last command is to just rename the folder to lower case.
Then let's head into our install directory and continue to the next step:
cd fcw/install
install.py
Once you are in the install directory, run this command:
sudo python3 install.py
The installation should take a few minutes (depends on your internet speed and how many of the packages need a full install). The script is very simple. It runs a bunch of sudo apt install
and pip3 install
commands to make sure all dependencies are installed.
The script also installs the rpi_ws281x
library which is used to actually turn the lights on and off.
config.py
has been edited as follows:LED_COUNT = 142 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
The only thing you need to change is the LED_COUNT
if it's different from 142. My LED strip has 142 lights. Feel free to play with the other settings as well.
I recommend running everything in a virtual environment. Makes it really easy to undo as well as run multiple projects at the same time without collision. For more information on how to run Flask behind Apache, check out https://www.easyprogramming.net/raspberrypi/pi_flask_app_server.php
Here are some quick steps to take once you are inside your fcw
folder:
sudo python3 -m venv venv
sudo chown -R pi:www-data venv
. venv/bin/activate
pip3 install flask
cd venv/bin/
wget https://raw.githubusercontent.com/naztronaut/RaspberryPi-RGBW-Control/master/utils/activate_this.py
Then in your Apache config, place the contents from utils/apache-led.conf
file into a separate .conf file like below:
cd /etc/apache2/sites-available
sudo nano fcw.conf
Paste in the contents:
<VirtualHost *:80>
ServerName fcw
WSGIDaemonProcess fcw user=pi group=www-data threads=5
WSGIScriptAlias /fcw /var/www/html/fcw/fcw.wsgi
<Directory /var/www/html/fcw>
WSGIProcessGroup fcw
WSGIApplicationGroup &{GLOBAL}
Require all granted
</Directory>
</VirtualHost>
Then disable the default config and add the new one and restart Apache:
sudo a2ensite fcw.conf
sudo a2dissite 000-default.conf
sudo service apache2 restart
If things go well, you should be able to access the app by going to http://ip_addr/fcw
The way the lights work is a background watchdog process is run looking for updates to file/status.txt
and then it parses that data to turn on the lights.
We need to do this so that the lights are continuously updating when new commands are sent instead of restarting. I'm still working on trying to figure out a way to run that script from the Flask app, still a bit behind, hopefully I'll figure it out soon. If you have ideas, let me know!
In the meantime, run the script in the background with this command (activate your virtual environment if you are using it):
sudo nohup python3 /var/www/html/fcw/led_watchdog.py >> /dev/null 2>&1 &
Now any updates sent from the UI will make the lights trigger.
To kill the script, run:
sudo pkill -f led_watchdog.py
This project is licensed under the MIT License - see the LICENSE file for details
Have questions? You can reach me through several different channels. You can ask a question in the issues forum, on EasyProgramming.net, or on the video comments on YouTube.
I will accept Pull requests fixing bugs or adding new features after I've vetted them. Feel free to create pull requests!