Driving LED’s from a Raspberry Pi

Every lighting tech I know wants to be able to send DMX signals to lights over wireless.

While they are commercial units to do this, they are all expensive, or are highly limited in their applications.

This is my notes from putting together a Raspberry Pi to drive an RGB LED strip.

This post is the software and config side.

Next one will be hardware to drive 12v LED’s from the 3.3v GPIO pins

Conventions

Red is a variable that you will need to change to fit your environment

Italics is a command that can be copied and run

Entity in a config file

Prerequisites

I used the following version of Raspbian

2014-09-09-wheezy-raspbian

Boot and expand the rootfs, set the local system time, and overclock if you want to, and set a password.

I I have not tested the GPIO configuration with an overclocked Pi, so I can not be sure if it will work.

Update the system to the current state

  • apt-get update
  • apt-get upgrade

Set the host name and IP address

 

  • nano /etc/hostname
www.example.com

 

  • nano /etc/network/interfaces
auto lo

iface lo inet loopback
iface eth0 inet static
       address 192.168.1.5
       netmask 255.255.255.0
       gateway 192.168.1.1

 

Download and install OLA

This is the main peace of code that handles the DMX reception.

Add the repo to sources.list

  • nano /etc/apt/sources.list
deb http://apt.openlighting.org/raspbian wheezy main
  • apt-get update
  • apt-get install ola ola-python ola-rdm-tests autoconf

Setup Pi-blaster

Pi blaster is a really useful bit of code that can be used to drive the GPIO pins on a Pi using a file in /dev

This is how we will control the voltage that each pin is set to.

  • git clone https://github.com/sarfata/pi-blaster.git
  • cd pi-blaster
  • ./autogen.sh
  • ./configure
  • make
  • make install

Setup OLA

Add the user pi to the olad group (this is the user that OLA runs as)

  • adduser pi olad

Load the OLA management page up in a web browser

The link will be http://192.168.1.5:9090

First click “universes”

And click “add Universe”

Set the universe ID to 1

Give the universe a name, and selcet

“E1.31 (DMX over ACN) [192.168.5.65]” as the input

(EDIT)

You can use ARTNET as the protocol since QLC fixed a regression in version 4.8.3.

and

“Dummy Device” as the output

At this stage, the Pi will be listening for a signal

Setup QLCPlus

At this stage, you need something to generate a DMX signal

Firstly big shout out to the guys at QLCPlus, this software is free, and amazing. I have been using it for 6 months to run lighting shows, and it has never missed a beet.

You can obtain the software from here

http://qlcplus.sourceforge.net/

Once you have installed it, go to the Inputs / Outputs tab

And for Universe 1, select the “E1.31″ output that corresponds to your main IP address

At this point, your PC will start broadcasting the signal.

Screenshot of the config page below.

qlc-setup

Code for the Pi

The guys over at OLA have got a nice python API that we are going to exploit, more details on it can be found here

http://www.openlighting.org/ola/developer-documentation/python-api/

 Testing

A python code snip-it from their site is quite useful for debugging.

from ola.ClientWrapper import ClientWrapper

def NewData(data):
  print data

universe = 1

wrapper = ClientWrapper()
client = wrapper.Client()
client.RegisterUniverse(universe, client.REGISTER, NewData)
wrapper.Run()

Copy this into a file called “dmxtest.py” and run it using

  • chmod +x dmxtest.py
  • python dmxtest.py

 

If everything is working, you should see something like this

array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

If you go into QLC, and move one of the faders on the “Simple desk” tab, you should see something like this on the pi terminal

array('B', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [78, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [89, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
array('B', [111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

So before we get the rest of the programming, This array is the basis of what we are going to use to drive the GPIO pins.

Driving the Pins

DMX has a value range from 0 – 255

The GPIO driver has a range from 0 – 1 Where 1 = 3.3 volts

To convert between this, and to drive the pins, I wrote this little bit of code.

This is far from complete, but it works well enough for me at the moment.

from __future__ import division
from ola.ClientWrapper import ClientWrapper
import time
import numpy
##to change the starting value of the DMX change this. 
##If you want it to start from DMX 4 
##(useful if you have more then 1)
##dmx = 4
dmx = 1
##used to correct for the array starting at 0
offset = dmx - 1
##Used to set the pins that will be driven
##Pin1 uses channel 1, Pin2 uses channel 2 Ect
##These are the 
pin1 = 17
pin2 = 22
pin3 = 18

def NewData(data):
   pinData = [x/255 for x in data]
#   print pinData[offset+0] ##Debug
#   print pinData[offset+1]
#   print pinData[offset+2]
   f = open('/dev/pi-blaster', 'w')
   f.write('%d=%s\n'%(pin1, pinData[offset+0]))
   f.write('%d=%s\n'%(pin2, pinData[offset+1]))
   f.write('%d=%s\n'%(pin3, pinData[offset+2]))
   f.close()

##If you want a different universe
##Then change this value
universe = 1
wrapper = ClientWrapper()
client = wrapper.Client()
client.RegisterUniverse(universe, client.REGISTER, NewData)
wrapper.Run()

If you want to work out which pin is which, this page is quite useful

http://www.hobbytronics.co.uk/raspberry-pi-gpio-pinout

 

I copied this into a file called “ledstrip-rgb.py”
and called it using

  • python /root/ledstrip-rgb.py

To configure this to run at startup (so I could run the pi headless)

Add the following line to /etc/rc.local

  • nano /etc/rc.local
python /root/ledstrip-rgb.py &

Note that on average the pi sits around 50% CPU usage while running the script (30% for the python and 20% for ola)

Nginx and WordPress on a Raspberry Pi

This how to assumes you have completed the tutorial

Maria DB on a Raspberry Pi

This can be done on a second Pi, or on the one you are now working on.

Conventions

Red is a variable that you will need to change to fit your environment

Italics is a command that can be copied and run

Entity in a config file

Prerequisites

This assumes the setup of wordpress on the nginx site. Hence it includes the PHP config for it.

Note that without the php-apc package, your performance will be rather poor, since there is not a lot of power available on a Pi.

I used the following version of Raspbian

2014-09-09-wheezy-raspbian

Boot and expand the rootfs, set the local system time, and overclock if you want to, and set a password.

I normally over clock mine to 800mhz 2 volts over.

Update the system to the current state

  • apt-get update
  • apt-get upgrade

Set the host name and IP address

 

  • nano /etc/hostname
www.example.com

 

  • nano /etc/network/interfaces
auto lo

iface lo inet loopback
iface eth0 inet static
       address 192.168.1.5
       netmask 255.255.255.0
       gateway 192.168.1.1

 

Download and install PHP and nginx

  • apt-get install nginx
  • apt-get install php5-fpm php5-mysql

Secure the PHP install

  • nano /etc/php5/fpm/php.ini

Search for the variable “cgi.fix_pathinfo”

It will look like this by default

;cgi.fix_pathinfo=1

You will want to change it to this

cgi.fix_pathinfo=0
  • service php5-fpm restart

Setup the nginx default site

nano /etc/nginx/sites-available/default

server {
        #listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

        root /var/www/html;
        index index.html index.php index.htm;

        # Make site accessible from http://localhost/
        server_name localhost www.example.com;

        location / {
                try_files $uri $uri/ /index.html /index.php?q=$uri&$args;
        }
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

 Maria DB setup

Note that this assumes that you have a working database already setup. If you need one you can follow Maria DB on a Raspberry Pi

  • mysql -p
  • [none]> USE mysql;
  • [mysql]> CREATE DATABASE databasename;
  • CREATE USER databaseuser@webserveripIDENTIFIED BY ‘yourpasswordhere‘;
  • [mysql]>GRANT ALL PRIVILEGES ON databasename.* TO wordpressuser@webserverip;
  • [mysql]>FLUSH PRIVILEGES;
  • [mysql]>exit

 

 Wordpress setup

You can ignore this if you are running a different PHP application.

  • cd ~
  • wget http://wordpress.org/latest.tar.gz
  • tar xzvf latest.tar.gz
  • cd ~/wordpress
  • cp wp-config-sample.php wp-config.php

Setup the database settings for wordpress

  • nano wp-config.php

Find the section in the config file under the comment

// ** MySQL settings - You can get this info from your web host ** //

You will want to change the settings as follows

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'databasename');
/** MySQL database username */
define('DB_USER', 'databaseuser');
/** MySQL database password */
define('DB_PASSWORD', 'yourpasswordhere');
/** MySQL hostname */
define('DB_HOST', 'databaseserverip');

 

Configure Nginx to host wordpress

Quick note

I normally put my web root in /var/www/html

This is because I have a background in apache, and redhat.

If you want to use the default folder you can.

Just remember to change the following line in the

/etc/nginx/sites-available/default file

        root /var/www/html;

Configure Nginx to host wordpress

  • mkdir -p /var/www/html
  • rsync -aP ~/wordpress/ /var/www/html/
  • cd /var/www/html/
  • chown -R www-data:www-data /var/www/html/*
  • mkdir wp-content/uploads
  • chown -R www-data:www-data /var/www/html/wp-content/uploads

 Restart nginx and hit your site

  • service nginx restart

Open your web browser and go to the following URL

http://www.example.com

 

 

Maria DB on a Raspberry Pi

Conventions

Red is a variable that you will need to change to fit your environment

Italics is a command that can be copied and run

Entity in a config file

Prerequisites

I used the following version of Raspbian

2014-09-09-wheezy-raspbian

Boot and expand the rootfs, set the local system time, and overclock if you want to, and set a password.

I normally over clock mine to 800mhz 2 volts over.

Update the system to the current state

  • apt-get update
  • apt-get upgrade

Set the host name and IP address

 

  • nano /etc/hostname
hostname.example.com

 

  • nano /etc/network/interfaces
auto lo

iface lo inet loopback
iface eth0 inet static
       address 192.168.1.2
       netmask 255.255.255.0
       gateway 192.168.1.1

 

Download Maria DB

  • cd ~
  • wget https://downloads.mariadb.org/f/mariadb-10.0.14/source/mariadb-10.0.14.tar.gz

Extract the files

  • tar -xvf mariadb
  • mv mariadb-5.5.29 maria

Install the build dependencies

  • apt-get install cmake libncurses5-dev bison

Build the source

  • cd maria/BUILD
  • ./autorun.sh
  • cd ..
  • cmake .
  • make   (note that this step takes up to 8 hours to run on a Pi)
  • make install

Create the user

  • adduser mysql

Setup the directories and scripts

  • chown -R mysql /usr/local/mysql
  • cd /usr/local/mysql
  • scripts/mysql_install_db –user=mysql
  • cp support-files/mysql.server /etc/init.d/mysqld
  • /etc/init.d/mysqld start

Secure the database install

This is because the default install options are highly insecure.

  • ./bin/mysql_secure_installation

Setup the config file

  • cp /usr/local/mysql/support-files/my-medium.cnf /etc/mysql/my.cnf
  • service mysqld restart
  • update-rc.d mysqld defaults

Setup access from a workstation

This step is entirely optional, I did it because I like running the SQL client on my PC

  • mysql -p
  • [(none)]> use mysql
  • [mysql]> GRANT ALL ON *.* to root@’192.168.1.5‘ IDENTIFIED BY ‘yourpasswordhere‘;
  • [mysql]> FLUSH PRIVILEGES;
  • exit