Installation Guide - Teraskull/music_led_strip_control-wiki GitHub Wiki

MLSC

This installation guide will help you install your own MLSC on your Raspberry Pi.

Check out the tutorial video I created for the manual installation:

Overview

  1. Install Server
    1. Install MLSC
      1. Automatic Setup Script
      2. Manual Setup
        1. Download and install dependencies
        2. Setup Microphone
        3. Install Music LED Strip Controller (MLSC)
    2. Configure Music LED Strip Control
    3. Add MLSC to the autostart.
  2. Install Client/Output
    1. Local Raspberry Pi for Output
    2. ESP8266 Client

Scheme_03


1. Install Server

Install MLSC

Preconditions

Installation was tested on a fresh image of "Raspberry Pi OS Lite" 2021 11th January with the Kernel version 5.4. The hostname and password were changed and SSH was activated to connect in headless mode.

There are two options for installing MLSC: Automatic Setup Script or Manual Setup.

Automatic Setup Script

Run the following command in your terminal:

curl -sSL https://raw.githubusercontent.com/TobKra96/music_led_strip_control/master/setup.sh | sudo bash

After the installation was successful, continue with the following steps: Configure Music LED Strip Control

Manual Setup

Note: You do not need to do the Manual Setup if you already used the Automatic Setup Script.

Download and install dependencies

  1. Update packages:
sudo apt-get update
sudo apt-get upgrade
  1. Install Audio Driver
sudo apt-get install libatlas-base-dev portaudio19-dev
  1. Install Python
sudo apt-get install python3
  1. Install required Python modules:

Install Pip:

sudo apt-get install python3-pip

Upgrade Pip to the newest version:

pip3 install --upgrade pip

Install Numpy (Offers a lot of mathematical functions and matrix manipulation. This version is required because 1.16 has a memory leak when using queues.):

sudo pip3 install -I numpy==1.17.0

Install Cython (Required to build the rpi_ws281x. module):

sudo pip3 install cython

Install Scipy (Offers a gaussian filter.):

sudo pip3 install scipy==1.3.0

Install Flask (The web server component.):

sudo pip3 install flask

Install PyAudio (Offers the audio input stream, which will be processed.):

sudo pip3 install pyaudio

Install coloredlogs:

sudo pip3 install coloredlogs

Install Flask_Login:

sudo pip3 install Flask_Login

Install Jinja2:

sudo pip3 install Jinja2

Install environs:

sudo pip3 install environs

Install waitress:

sudo pip3 install waitress
  1. Build and install the rpi_ws281x module.

Select a place where you want to build the module. /share is used by default.

Create a new directory:

sudo mkdir /share

Navigate to the directory:

cd /share

Install Git:

sudo apt-get install git

Install the dependencies for rpi_ws281x module:

sudo apt-get install build-essential python3-dev scons swig

Ensure you are in the /share directory and clone the repository:

sudo git clone https://github.com/jgarff/rpi_ws281x.git

Move inside the cloned repository and build the project:

cd rpi_ws281x
sudo scons

The Terminal output should look like this:

image

Move inside the python directory and install the python module:

cd python
sudo python3 setup.py install

Setup microphone

Plug-in your USB microphone.

Create/edit /etc/asound.conf using Nano or any other preferred editor:

sudo nano /etc/asound.conf

Write the following text to /etc/asound.conf:

pcm.!default {
    type hw
    card 1
}
ctl.!default {
    type hw
    card 1
}

In Nano, you can save changes by pressing "Ctrl+X", "Y" and "Enter".

Set the USB device to be the default device by editing /usr/share/alsa/alsa.conf:

sudo nano /usr/share/alsa/alsa.conf

Enable the Default Card:

Change

defaults.ctl.card 0
defaults.pcm.card 0

To

defaults.ctl.card 1
defaults.pcm.card 1

Disable unused interfaces:

Change

#
#  PCM interface
#

# redirect to load-on-demand extended pcm definitions
pcm.cards cards.pcm

pcm.default cards.pcm.default
pcm.sysdefault cards.pcm.default
pcm.front cards.pcm.front
pcm.rear cards.pcm.rear
pcm.center_lfe cards.pcm.center_lfe
pcm.side cards.pcm.side
pcm.surround21 cards.pcm.surround21
pcm.surround40 cards.pcm.surround40
pcm.surround41 cards.pcm.surround41
pcm.surround50 cards.pcm.surround50
pcm.surround51 cards.pcm.surround51
pcm.surround71 cards.pcm.surround71
pcm.iec958 cards.pcm.iec958
pcm.spdif iec958
pcm.hdmi cards.pcm.hdmi
pcm.dmix cards.pcm.dmix
pcm.dsnoop cards.pcm.dsnoop
pcm.modem cards.pcm.modem
pcm.phoneline cards.pcm.phoneline

To

#
#  PCM interface
#

# redirect to load-on-demand extended pcm definitions
pcm.cards cards.pcm

pcm.default cards.pcm.default
pcm.sysdefault cards.pcm.default
#pcm.front cards.pcm.front
#pcm.rear cards.pcm.rear
#pcm.center_lfe cards.pcm.center_lfe
#pcm.side cards.pcm.side
#pcm.surround21 cards.pcm.surround21
#pcm.surround40 cards.pcm.surround40
#pcm.surround41 cards.pcm.surround41
#pcm.surround50 cards.pcm.surround50
#pcm.surround51 cards.pcm.surround51
#pcm.surround71 cards.pcm.surround71
#pcm.iec958 cards.pcm.iec958
#pcm.spdif iec958
#pcm.hdmi cards.pcm.hdmi
#pcm.dmix cards.pcm.dmix
#pcm.dsnoop cards.pcm.dsnoop
#pcm.modem cards.pcm.modem
#pcm.phoneline cards.pcm.phoneline

Reboot:

sudo reboot

Install Music LED Strip Controller (MLSC)

Navigate to the /share directory:

cd /share

Clone the repository:

sudo git clone https://github.com/TobKra96/music_led_strip_control.git

Configure Music LED Strip Control

At this stage, MLSC and all dependencies should be installed. Now we can execute the program and configure the main settings. Navigate to the server directory:

cd /share/music_led_strip_control/server

Run the program manually:

sudo python3 main.py

You will get a terminal output like this:

image

The error in the screenshot is caused by the missing microphone config:

INFO     - libs.audio_process_service     - Found the following audio sources:
INFO     - libs.audio_process_service     - 1 - USB PnP Sound Device: Audio (hw:1,0) - 44100.0
INFO     - libs.audio_process_service     - 2 - default - 44100.0
ERROR    - libs.audio_process_service     - ********************************************************
ERROR    - libs.audio_process_service     - *                      Error                           *
ERROR    - libs.audio_process_service     - ********************************************************
ERROR    - libs.audio_process_service     - Could not find the mic with the id: 0
ERROR    - libs.audio_process_service     - Using the first mic as fallback.
ERROR    - libs.audio_process_service     - Please change the id of the mic inside the config.
INFO     - libs.audio_process_service     - Selected ID: 1
INFO     - libs.audio_process_service     - Using 1 - USB PnP Sound Device: Audio (hw:1,0) - 44100.0
INFO     - libs.device                    - Starting device: Raspberry Pi

The output means that MLSC found two audio inputs with the IDs 1 and 2. 1 is the USB Audio Device and 2 is the default audio device. We want to use "2 - default - 44100.0" as the input device.

Keep the program running and open a web browser on a device on the same local network.

Enter the following into the search bar:

http://<IP_OF_RASPBERRY_PI>:8080

Replace <IP_OF_RASPBERRY_PI> with the IP or hostname of the Raspberry Pi.

Make sure the port is set to the default port: 8080

The Dashboard should appear. Select General Settings in the navigation sidebar:

image

Now you should see the General Settings with Audio Source ID and Default Sample Rate.

image

Change it to the matching entry that was in the console output, "2 - default - 44100.0":

Audio Source ID = 2
Default Sample Rate = 44100

Press the "Save" button on the bottom of the General Settings page. The changes should be applied.

image

Restart MLSC:

Press "CTRL+C" and restart main.py:

sudo python3 main.py

The error should disappear and MLSC automatically will select the input device with the ID 2.

Add MLSC to the autostart.

Using a systemd service, each time Raspberry Pi reboots, MLSC will run.

Stop MLSC if it is still running.

  1. Create a file mlsc.service in /etc/systemd/system/:
sudo touch /etc/systemd/system/mlsc.service
  1. Add the following to the created mlsc.service file:
[Unit]
Description=Music LED Strip Control
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/share/music_led_strip_control/server
ExecStart=python3 main.py
Restart=always
RestartSec=5
KillMode=control-group

[Install]
WantedBy=multi-user.target
  1. Run the following commands to enable and start MLSC:
sudo systemctl daemon-reload

sudo systemctl enable mlsc.service

sudo systemctl start mlsc.service
  1. Reboot:
sudo reboot
  1. Check if the service is running:
sudo systemctl status mlsc.service

MLSC should now restart on reboot.

To remove the service from autostart, run the following commands:

sudo systemctl stop mlsc.service

sudo systemctl disable mlsc.service

Note: The WorkingDirectory specified in the service is /share. If you did not change anything during the installation, the service should work without issues.


2. Install Client/Output

i. Local Raspberry Pi for Output

Wiring

The LED Strip, the Power Supply for the LED Strip, and the Raspberry Pi have to share the same GND. This is important for the signal. Connect all three ground connections together (Black wire in the scheme). The signal data cable will be connected between the Raspberry PI and the LED Strip. Use a resistor or potentiometer to reduce the Signal Ringing (see Wikipedia Signal Ringing). The resistor value depends on the cable quality and the signal cable length. It can be between 120 Ohm and 220 Ohm.

I would recommend using a potentiometer and adjust the resistance directly.

Raspberry Pi Pinout

Here is the pinout of the Raspberry Pi:

Pinout_RaspberryPi

Use any Ground Pin for the ground cable.

Pin 12 (GPIO 18): Data Pin for the data cable.

Add output in MLSC

Start MLSC and open the web interface:

http://<IP_OF_RASPBERRY_PI>:8080

Replace <IP_OF_RASPBERRY_PI> with the IP or hostname of the Raspberry Pi.

Make sure the port is set to the default port: 8080

Go to Device Settings:

image

Create a new device:

image

Now change the following inputs according to your needs:

image

  1. Switch to the previously created device. It should appear as "Default Device".
  2. Enter a name for the device.
  3. Set the max FPS. Higher FPS causes higher CPU usage. Using 60 FPS is recommended.
  4. Enter the LED count.
  5. Define the middle of the LED Strip. If you use your LED strip around a corner, you can enter the number of LEDs before the corner.
  6. Select "Output Raspberry Pi" as "Used Output Type".
  7. Adjust the brightness of the LEDs.
  8. The GPIO Data Pin. Use GPIO 18.
  9. Save the changes.

Now you can go back to the Dashboard and select an effect. The LED strip should turn on.

Limitations

MLSC uses the rpi_ws281x library for the output signal. We are using the PWM for signal generation. If your LED strip only flickers or does not set the LED colors as expected, check the resistance of the potentiometer or resistor. Another solution could be:

Since this library and the onboard Raspberry Pi audio both use the PWM, they cannot be used together. You will need to blacklist the Broadcom audio kernel module by creating a file /etc/modprobe.d/snd-blacklist.conf with the following inside:

blacklist snd_bcm2835

If the audio device is still loading after blacklisting, you may also need to comment it out in the /etc/modules file.

On headless systems you may also need to force audio through HDMI. Edit config.txt and add:

hdmi_force_hotplug=1
hdmi_force_edid_audio=1

A reboot is required for this change to take effect.

Some distributions use audio by default, even if nothing is being played. If audio is required, you can use a USB audio device instead.

Example with a WS2815 LED Strip

  • Raspberry Pi 4
  • WS2815 144 LED/m 12V
  • 12V 10A Power Supply. Minimum required current: 144 LEDs * 15mA = 2,16A --> 5A is the next common 12V power supply.
  • Potentiometer 0-2k Ohm
  • TONOR Conference Microphone. You can use any Linux compatible USB Microphone or USB Soundcard with a connected microphone.

RaspberryPiExample~2

ESP8266 Client

Use an ESP8266 as a client. The Raspberry Pi will work as a server.

Concept

For the Server and Client Architecture, we will create our own WiFi Access Point on the Raspberry Pi. This LED Wifi Network will be used for the LED Output data, which will be received by the ESP8266 clients. The Web Interface can be accessed on the default Home Network via Ethernet (LAN Port with cable).

Scheme_05

Setup the Raspberry Pi

We start with the installation of the Access point. It contains hostapd (the Access Point Software), dnsmasq (the DNS Server/Cache and DHCP Server) and dhcpcd (the DHCP Client).

Update packages:

sudo apt-get update
sudo apt-get upgrade

Install required packages:

sudo apt install dnsmasq hostapd

Configure the packages:

If your wifi is already configured with a network, please delete the network config:

  • In the terminal type: sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
  • Delete the relevant wifi network block (including the ‘network=’ and opening/closing braces.
  • Press ctrl-x followed by ‘y’ and enter to save the file.
  • You’re done!

We start with the DHCP settings.

Open/create the following file:

sudo nano /etc/dhcpcd.conf

Paste the following content to the end of the file:

interface wlan0
static ip_address=172.24.26.1/24

You can choose your own static IP address.

It should look like this:

image

In this file, we set the static IP address of the interface wlan0.

In Nano, you can save changes by pressing "Ctrl+X", "Y" and "Enter".

Restart the DHCP Client Service:

sudo systemctl restart dhcpcd

We want to check if the wlan0 interface is available. Enter the following command:

ip l

You should see something like this:

image

Now we continue with the DNS Server. Backup the current configuration:

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak

Create a new config file:

sudo nano /etc/dnsmasq.conf

Enter the following lines:

# Activate the DHCP-Server for the Wifi-Interface
interface=wlan0

# DHCP-Server not active for Ethernet Network
no-dhcp-interface=eth0

# IPv4 Address Scope and Lease-Time
dhcp-range=172.24.26.0,172.24.26.255,255.255.255.0,24h

# DNS
dhcp-option=option:dns-server,172.24.26.1

Change the IP address if needed. Ensure it matches with the static IP you set before.

You can check the config with:

dnsmasq --test -C /etc/dnsmasq.conf

It should return "dnsmasq: syntax check OK."

Restart DNSMASQ:

sudo systemctl restart dnsmasq

Show the status of dnsmasq:

sudo systemctl status dnsmasq

We will see something like this:

image

It will be active and there should be no errors.

Enable the service to start on reboot:

sudo systemctl enable dnsmasq

Now we can configure the access point software.

Create a new config file:

sudo nano /etc/hostapd/hostapd.conf

Enter the following settings:

# Interface and driver
interface=wlan0
#driver=nl80211

# Wifi Configuration
ssid=MLSC-Wifi
channel=1
hw_mode=g
ieee80211n=1
ieee80211d=1
country_code=DE
wmm_enabled=1

# Wifi Encryption
auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
wpa_passphrase=wifipassword

You have to change country_code with your own country code.

Change the Wifi channel if the network performance is low.

Change the password in wpa_passphrase

Change the file rights. Only the admin should see the wifi password:

sudo chmod 600 /etc/hostapd/hostapd.conf

Check if the configuration worked:

sudo hostapd -dd /etc/hostapd/hostapd.conf

You can stop it with "Ctrl+C".

If hostapd won't start, you can try to unblock the WiFi and start hostapd again:

sudo rfkill unblock all
sudo hostapd -dd /etc/hostapd/hostapd.conf

Add hostapd to the autostart:

sudo nano /etc/default/hostapd

Add the following parameters:

RUN_DAEMON=yes
DAEMON_CONF="/etc/hostapd/hostapd.conf"

Start hostapd and check the status:

sudo systemctl unmask hostapd
sudo systemctl start hostapd
sudo systemctl enable hostapd
sudo systemctl status hostapd

The service should be active (running).

Restart the system and check if everything works:

sudo reboot

Setup ESP8266

Flash the firmware.

Arduino IDE was used for flashing the firmware. Ensure you have installed the right board packages.

My Boardmanager URL is:

https://dl.espressif.com/dl/package_esp32_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json

You can add this to the "Files/Preferences" Menu. Install the ESP Board Package inside the Board Manager and configure the Serial connections. This setting depends on your ESP Microcontroller you are using.

In my case it's a NodeMCU with the following settings:

image

Now we have to install the required libraries. Use the library manager for it:

ESP8266WiFi
WiFiUdp
NeoPixelBus

(NeoPixelBus by Makuna)

Open the following file:

music_led_strip_control/client/udp/esp/nodemcu_client/nodemcu_client.ino

Modify the following variables:

image

  1. NUM_LEDS Number of LEDs you are using.
  2. ssid The Wifi SSID of your Access Point.
  3. password The password of your Access Point.
  4. ip The IP, that the ESP will use.
  5. gateway The Gateway IP of your Raspberry Pi. This should be the static IP you set before.
  6. subnet The Subnet Mask that your Access Point uses.

Now you can build the program and flash it to the ESP:

image

The LED of the ESP should blink during the Upload Process and the console of the Arduino IDE should show this:

image

Now we can start with the wiring:

Pinout

Use a Ground Pin to share the same Ground with LED Strip and Power supply.

Use the GPIO 3 Pin (RX) for the Data Signal cable.

Setup MLSC

Start MLSC and open the web interface:

http://<IP_OF_RASPBERRY_PI>:8080

Replace <IP_OF_RASPBERRY_PI> with the IP or hostname of the Raspberry Pi.

Make sure the port is set to the default port: 8080

Go to the Device Settings:

image

Add a new device with the following settings:

image

  1. Switch to the newly created device. It should appear as "Default Device".
  2. Enter a name for the device.
  3. Set the max FPS. Higher FPS causes higher CPU usage. Using 60 FPS is recommended.
  4. Enter the LED count.
  5. Define the mid of the LED Strip. If you use your LED strip around a corner, you can enter the number of LEDs before the corner.
  6. Select "Output Network via UDP" as "Used Output Type".
  7. Set the IP Address of the ESP.
  8. The port of the UDP connection. 7777 is the default setting.
  9. Save the changes.

Now you can turn on the LED Strip and check if the Network Connection and the LED strip works.

Error Handling

There are multiple logging mechanisms implemented. The ESP Led Strip shows an LED Code during the connection phase. LED 1: Connecting - The LED Stip tries to connect. The LED will blink. LED 2: Reconnected - Indicates if the LED Strip reconnected to the local network. LED 3: WL_CONNECTED - ESP connected to the WiFi Network. LED 4: WL_NO_SHIELD - Could not find a WiFi shield. LED 5: WL_IDLE_STATUS - The WiFi is in the idle state. LED 6: WL_NO_SSID_AVAIL - Could not find the Wifi with the configured SSID. LED 7: WL_SCAN_COMPLETED - Network scan completed. LED 8: WL_CONNECTED - Connected to the WiFi. LED 9: WL_CONNECT_FAILED - The connection failed. LED 10: WL_CONNECTION_LOST - The connection to the WiFi network was lost. LED 11: WL_DISCONNECTED - The ESP disconnected from the local network.

Additionally, you can monitor the serial output of the ESP. It will show you the current state. There is a hardware limitation of the maximum amount of clients that could connect to the raspberry pi. On the Raspberry Pi 4 it's ~8 clients. The Raspberry Pi 3b+ supports ~20. You can use a USB WiFi Dongle with the RT5370 Chipset to increase the limit to 100+.

Example with a WS2812b LED Strip

Download the 3D Printer Files here:

Thingiverse Download

Bild1 Bild2 Bild3 Bild5 Bild9 Bild10

Revision 2.2