#Purpose
- This program lets your Raspberry Pi control control RGB LEDs.
#Features
-
Color correction. Some RGB LED strips have imbalanced LEDs, and this program allows you compensate by reducing brightness on any color channel.
-
Simple file-based API. During continuous operation, simply put your desired target color into a CSV file containing RGB values ranging from 0-255, and the program will implement your target color. Change it as often as you like.
-
Transition options. You can fade from the current color to the target color directly, by fading to black and then fading to the target color, or by changing to the target color instantaneously. You can change these preferences on the fly.
-
Integration with openHAB via the
exec
binding.
#HOWTO
##What You Need
-
Raspberry Pi (Rev B tested, but any revision should work.)
-
An RGB LED strip (I bought this one, but any 4-pin common-anode RGB LED strip will work. Deviations will work, but you'll need to come up with your own wiring diagram.)
-
A DC power supply (My strip is 12VDC, with a maximum current of 0.6A, and I have a 12VDC 8.5A power supply.)
-
Three MOSFETs (I used these N-channel MOSFETs. Deviations can work, but you'll need to come up with your own wiring diagram.)
-
Three free GPIO pins
-
At least 60% available CPU on your Pi
-
Basic Linux skills (package installing, file copying and editing, running commands)
##Hardware Setup
-
Wire up your MOSFETs and RGB LED strip as you see on the first diagram in this Adafruit tutorial for the Arduino, except that:
-
You have a Pi, not an Arduino, but the basic principle is the same.
-
You power the LED strip with your dedicated power supply, not any pin from the Pi.
-
You will run a ground wire from your Pi to the ground terminal of the power supply.
-
Pick three GPIO pins to control red, green, and blue. Read the pigpio documentation carefully before picking your pins. If you get this wrong, various bad things can happen.
-
##Software Setup
-
As shown here, download, build, and install
pigpio
on your Raspberry Pi, then start thepigpio
daemon.-
Download, build, and install.
wget abyz.co.uk/rpi/pigpio/pigpio.zip unzip pigpio.zip cd PIGPIO make make install
-
Start the pigpio daemon.
sudo pigpiod
-
-
Install, configure, and run Hugh.
-
Copy the Hugh directory tree to your Pi. Mine is in
/home/pi/hugh
-
Edit
hugh_config.ini
to match the BCM-indexed GPIO pins that you chose above. -
Run Hugh from its directory:
cd /home/pi/hugh sudo python ./hugh.py
-
Any edits to the files
hugh_config.ini
orrgb
will be loaded and quickly implemented by Hugh.
-
##openHAB Integration (optional)
- My solution builds on this example for KNX and this example using MQTT.
###items
-
Add this
Color
item to any of your *.items
files, which gives you a color picker UI.Color HSB "Color Light" <slider> (Lights)
-
Add a
String
item to any of your *.items
files, as your interface to the Hugh software running on your Pi.-
If the Pi running Hugh is also your openHAB server, add this:
String RGB "RGB Value" { exec=">[*:echo %2$s > /home/pi/hugh/rgb]" }
-
Otherwise, add this entry:
String RGB "RGB Value" { exec=">[*:ssh pi@pi2 echo %2$s > /home/pi/hugh/rgb]" }
-
-
DON'T put
RGB
item into any groups or sitemaps. It's not something that users should directly interact with. -
Put the
HSB
item into any groups and sitemaps you prefer. The reference example assumes you want it in theLights
group. -
Edit the
RGB
item to match the device name, username, and target directory for Hugh's RGB file.
###rules
-
Add this rule to any of your *
.rules
files. This converts the HSB (hue, saturation, brightness) color to comma-separated RGB (red, green, blue) values, and puts them into theRGB
String
item, which in turn gives the RGB values to Hugh.rule "Set RGB Value from HSB" when Item HSB changed then var HSBType hsbValue = HSB.state as HSBType // scale the values to 256 levels var int red = Math::round(hsbValue.red.floatValue * 2.55) var int green = Math::round(hsbValue.green.floatValue * 2.55) var int blue = Math::round(hsbValue.blue.floatValue * 2.55) sendCommand(RGB, red+","+green+","+blue) postUpdate(RGB, red+","+green+","+blue) end
###SSH
-
If the Pi running Hugh is also your openHAB server, then you can skip this step. Otherwise:
-
As the
root
user, generate an SSH keypair on your openHAB server with an empty passphrase. -
On the Pi, as a regular user (e.g.
pi
), append the public key to~/.ssh/authorized_keys
in the user account on the Pi (e.g./home/pi/.ssh/authorized_keys
) to allow your openHAB server to automatically log in when it runsssh
via theexec
binding in theRGB
item.
-
###usage
- openHAB will pick up on the
items
andrules
changes. You should be able to interact with theHSB
color picker in any openHAB UI and see the color reproduced by your RGB LEDs.