A Raspberry Pi Christmas Tree

Rpi Tree

The following tutorial will show you how to connect a Raspberry Pi computer (Pi) to a relay that can help you make your Christmas tree (or any appliance that can be toggled on and off by cycling its power) programmable for the Internet. I took this project on as a Christmas flavored programming project for my kids (two boys 11 and 8). It is a good project for that age thanks to the fact that there is an hardware assembly component (relatively easy) and a software programming element (with a couple of great concepts for beginners to grapple with). If you follow the instructions below, by the time you finish, you will have a Christmas tree that can turn on and off on a schedule as well as be programmed more generally. In our case my son set it it to blink on and off for 30 seconds when there was snow in the immediate weather forecast.

Before starting, a natural question might be: why not Arduino? While Arduino is a fantastic platform for small non-Internet electronics projects, I wanted to use the Pi for two reasons: 1. the processing programming environment, while certainly better than raw programming of a micro controller, is a kludgey way for kids to learn to program 2. getting an Arduino to talk to the Internet without a computer in the loop is a pain in the butt and always requires grafting another quasi full purpose computer on to it in the form of some sort of networked “shield.”

Though we’ve done project with Arduino, Processing and the Wifi shield, using the Pi proved to be a pleasure by comparison and allowed my older son to use Python as a programming language— a much better choice for budding coders in my opinion. Additionally the Pi feels like a more expansive platform (we’ve already thought of a couple of other projects) with more transferable skills (he can now log into an Amazon EC2 instance with the same skills).

A final note on prep: you (the parent) should have some rudimentary UNIX command line skills or at the very list, a lot of patience and a gift for asking Professor Google questions around making your way around a Linux system.

Getting started: parts list

Presuming that you have a micro USB charger at home and a Christmas tree with lights that plug in (or other ethnically appropriate holiday ornament that can be plugged in), you should get:

  1. a Raspberry Pi
  2. a cheap USB WiFi dongle (presuming you won’t have Ethernet handy near your tree)
  3. an SD card (for loading an OS on your Raspberry Pi)
  4. some male to female jumper cables for wiring your Raspberry Pi to the external relay
  5. an AC relay that is solid state and sealed (so you won’t burn your house down) and can be driven by a low voltage from a micro controller (I found this one after some Googling for $20 but there are many many choices, the key is that you can toggle it with a 3V voltage difference)

I ordered all of these parts for ~$90 and had them delivered within a week.

Pi setup

The Raspberry Pi has come a long way in terms of ease of first time install thanks to the NOOBS (New Out of the Box Software) install package. To get going, download a version of NOOBS and follow the directions as to what files to copy to the root of your SD card. Then plug the card into the Pi, a keyboard (mouse is not needed), and an HDMI capable monitor (I used my TV). You’ll also need an Ethernet connection as you won’t have WiFi working out of the box. After the Pi boots, choose to install Raspian (a full version of Linux), let NOOBS do its thing, and sit back (hint you need a keyboard but not a mouse). It takes about 15-20 minutes of installing but if all goes well, you’ll be rebooted into fully working system.

[Note: the one thing to watch out for here is that you don’t get a flakey Rpi board which is still happening quite a bit 2M units in. I’ve bought 5 of these from two different vendors of Amazon and 2 have been bad so at a 60% yield YMMV. One way to tell the board is bad is if you try to boot the NOOBs card and all you see is a rainbow colored screen. The other way I’ve seen the board fail is when it can’t quite provide enough power to the USB hub thus causing the WiFi connection to be really flakey.]

To get the Wifi configured, you’ll need to edit the file: /etc/network/interfaces. Presuming you’ve got a Wifi USB dongle, add this to the bottom of the file (editing can be done with the ed program):

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid “YOUR-NETWORK-NAME"
wpa-psk “YOUR-NETWORK-PASSWORD” 

(fill in your own network stuff)

After saving the file, unplug the computer and reboot it without Ethernet connected to check that it gets on the network. If something fails here, Professor Google is your friend.

After you’ve got the connection from the computer to the Internet, you are ready to install the Python software required to drive the I/O pins that will control your relay. The Raspberry Pi comes with a GPIO (General Purpose Input Output) socket in the middle of the board that looks like it could take a connector. The software you are about to install will let you set the voltage high or low on one of the pins.

On the PI run these three commands:

sudo apt-get update
sudo apt-get install python-dev python-pip python-smbus
sudo pip install rpi.gpio

If all goes well, you should be able to enter the Python interactive prompt (type python) and type this:

import RPi.GPIO

If you get no errors, you are good to go with all of the software you need installed. Congratulations, your hardware is ready to be wired up.

Wiring it up

Lamp Connecting the Raspberry Pi to the Powertail solid state relay that will turn the tree on and off is trivial. The GPIO pins to focus on are 7 and 5 (which are + voltage and ground). These will respective go into the (+) and (-) terminals on the relay. If you prefer to experiment, you can use a breadboard as is shown in the picture but it is not strictly necessary.

To test the set up, find an appliance or a lamp that you can connect to power through the relay and use the trusty Python interactive prompt to do the following: (note that you need to run all of the GPIO stuff as the root user (sudo su) to be able to set the pins:

>>> import RPi.GPIO as GPIO
>>> GPIO.setmode(GPIO.BOARD)
>>> GPIO.setup(7, GPIO.OUT)
>>> GPIO.output(7,False)


Tree Note that the last line where we send a False to the pin which with my relay model actually closes it and turns on the tree. You may have a slightly different model which closes on high voltage in which case passing True will turn it on. Experiment with your lamp until you figure out which configuration turns it on and off.

Now you are ready to move to your tree! Replace the appliance/lamp for the base of the tree lights and put the Pi somewhere where it won’t get stepped on.

Writing the “tree server” is beyond the scope of this tutorial but you should feel free to use ours (tree_api.py from Github) which relies on the super simple Python web server, bottle to expose an ON API call and an OFF API call. Bottle makes it very easy to write these kinds of REST-based servers and after you have it running (remember to run it as root), you can just call: http://my-rpi-internal-address/on to turn it on and …/off to turn it off. One other trick you may consider is using something like the Unix screen program to keep the server running even after you are not logged in (using init.d startup scripts is also possible if you’d prefer). This part is something an adult should do (or just use ours) at least to start with as there is a bunch of mucking around with UNIX and bottle that offers little pedagogical value.

To install both bottle and screen:

sudo pip install bottle
sudo apt-get install screen


Alex The logic to make the tree do stuff on the other hand is ripe of a beginning programmer. I am including what my 11 year-old wrote to serve as inspiration. In his case, he was able to scrape a weather page (with only a little bit of help on looking for the right string within the DOM element) and then cause the tree to blink as well as turn on and off on a schedule. I love the task for a beginner because it exposes concepts like flow of control, APIs, conditional logic, and web scraping. But you should feel free to take it in a different direction.

Conclusion

This won’t be our last Pi project if only because the tree will soon disappear into a compost. Having spent most of a day doing this, I’d definitely recommend it to anyone looking to spark the programming flame in kids in the 8-11 range. Compared to anything else we’ve done (and considering a sample size of 2), this has been by far and away the most popular of the projects.