The Official Raspberry Pi Projects Book - Projects_Book_v4

(singke) #1

Tutorial


raspberrypi.org/magpi The Official Raspberry Pi Projects Book 113


TEMPERATURE CONTROLLED STAIRLIGHTS


Language
>PYTHON

DOWNLOAD:
magpi.cc/Stairlights


STEP-02
Set up the Pi
Connect the Din and GND wires to the male ends of
the jumper wires. Connect the female end to the Pi as
follows: Din to GPIO pin 18; GND to any ground pin.
You could power the Pi through the other 5V wire,
but this can be dangerous for the board; it is best to
use a normal power supply.
Follow the steps at Adafruit to install the NeoPixel
library, rpi_ws281x: magpi.cc/1nRSyYk.
Plug the jack connector into your power supply. Plug
in the power supply and test your strip using the scripts
from the examples folder for some shiny lights!




STEP-03
Set up the weather API
You will need a developer account for a weather API
to get the outside temperature for your area. We used
forecast.io as it allows users to request 1000 forecasts
per day at no cost:




Go to forecast.io
Select Developer
Select ‘Sign up’ to register an account
Once you confirm your email address, you will
get a secret key: you will need this key in Step 04



You could set up another Pi outside your house and
get the real temperature for your neighbourhood.
It depends on how much you want to spend on the
project, and whether you trust the API’s accuracy.



STEP-04
Let there be light!
Download stairlights.py from the box at the top of
this page to the home/pi directory and change:



Line 6: enter your secret key.


Line 7: change longi and lati to your longitude
and latitude coordinates. You can use Google Maps
to find them: right-click on your location and select
‘What’s here?’


Set up a cron task to check the outside temperature
every five minutes and update the lights.
In a Terminal window, type:


sudo –E crontab -e

At the end of the file, enter:

/5 * /usr/bin/python3.4 /home/pi/
stairlights.py
@reboot /usr/bin/python3.4 /home/pi/
stairlights.py


Save the changes.

>STEP-05
Debugging your lights
If the examples from the NeoPixel library didn’t
work, check all your connections. Make sure you have
plugged the data wire into the correct GPIO pin on
your Pi. Are you connected to Din or Dout? The lights
will power on Dout, but nothing will happen.
If the lights are displaying strange colours, some
people have reported the NeoPixel library not working
with Pi’s setup for audio. See magpi.cc/2ovIQhy.
Make sure your script is running properly before
adding it to cron. There’s nothing like sitting on your
stairs waiting for five minutes for a cronjob that isn’t
working to run!

>STEP-06
What next for your lights?
We changed the cron tasks to stop the lights
displaying through the night:

*/5 7-21 * * * /usr/bin/python3.4 /home/pi/stairlights.py
*/5 7-21 * * * /usr/bin/python3.4 /home/pi/nightynight.py

...where nightynight.py is a simple script that turns
the lights off using the first 26 lines of stairlights.py
and this line:

colorMe(strip, Color(0, 0, 0), 0, 239)

Now, with a strip of lights on your stairs, you could
play all kinds of games!
Add some coloured arcade buttons for a quick
reaction game.
Maybe when your phone connects to the Bluetooth
on the Pi, your stairs could flash a welcoming
hello dance. Experiment and have fun!

5V and GND go to the power source
via the female jack connector plug.
GND and Din go to the Pi via the
jumper wires
Free download pdf