Raspberry Pi-powered home lighting

Apr 29, 2017

Have a spare raspberry pi? Why not follow in my footsteps and build your own pi-powered garish multi-coloured lighting system, all controlled via a companion android app?!

Gif of lighting in action

Here’s what you’ll need:

Installing your pHAT array

First you’ll want to attach the pHat to your pi. This will require a bit of soldering, unfortunately.

Phat LED array

The pHat comes in two pieces - simply solder them together, making sure to solder on the LED side of the board. Then simply slide the pHat onto your pi’s GPIO pins, pressing firmbly to ensure they are well connected.

You can check you’ve installed everything correctly by trying out a few of the examples here.

The Pi web server

Now that you have a functioning light array, we want to create a simple web server so that we can control our lights without having to use the command line. To use your pi as a web server, you’ll first want to assign your pi a static IP address. My code is hardcoded to use (because I threw this together in a few hours), but you should be able to modify my code to communicate with a different address.

To create the web server, we can use Flask to easily create a couple of endpoints that we can use to interact with the pHat. For example to set the pHat to a certain colour:

def color():
    hex_color = request.args.get('color')

    red, green, blue = map(ord, hex_color.decode('hex'))

    width, height = unicorn.get_shape()

    for y in range(height):
        for x in range(width):
            unicorn.set_pixel(x, y, red, green, blue)

    return "Success, set to " + hex_color

This lets us set the colour of the pHat by making a request to http://<pi>/?color=FF0000, for example.

You can grab the full code from https://github.com/tom-james-watson/toms-lighting-pi. Download server.py and put it in your home directory.

Ensure you have Flask and unicornhat python modules installed:

pip install Flask
curl https://get.pimoroni.com/unicornhat  | bash

You can then launch the server with:

sudo python server.py

I’d suggest adding an entry to /etc/rc.local to ensure the server starts every time your pi restarts. Simply add the following to the bottom of the file:

sudo python /home/<YOUR USER>/server.py

You should now be able to control the colour of your pi by making a request such as:

http://<ip address of pi>:5000/?color=FF0000

The companion app

Screenshot of companion app

Explaining how to create a react native app is kinda out of the scope of this blog post, but for those interested, the full code and instructions for development/building the APK are available at https://github.com/tom-james-watson/toms-lighting-app.

For those who just want the app, you can download the APK here. As long as your raspberry pi is assigned to then it should work just fine!

comments powered by Disqus