Let’s say you’re leaving for a long vacation early tomorrow morning and you want to ward off would-be burglars from your home. A lamp timer is a good deterrent, but hardware stores are closed for the night and you won’t have time to get one before your flight in the morning. However, since you’re a Raspberry Pi hobbyist, you have a few supplies lying around, namely:
- Raspberry Pi board
- Breadboard
- Jumper wires, female-to-male.
- PowerSwitch Tail II relay
- Hookup wire
With these supplies, you can make your own programmable lamp timer using two powerful Linux tools: shell scripts and cron.
A shell script is a file that contains a series of commands (just like the ones you’ve been using to control and read the pins). Take a look at the shell script below and the explanation of the key lines.
#!/bin/bash # echo Exporting pin $1. # echo $1 > /sys/class/gpio/export # echo Setting direction to out. echo out > /sys/class/gpio/gpio$1/direction # echo Setting pin high. echo 1 > /sys/class/gpio/gpio$1/value
Save that as a text file called on.sh
and make it executable with the chmod command:
root@raspberrypi:/home/pi# chmod +x on.sh
Note
You still need to be executing theses commands as root. Type sudo su
if you’re get errors like “Permission denied.”
A command line argument is a way of passing information into a program or script by typing it in after name of the command. When you’re writing a shell script, $1 refers to the first command line argument, $2 refers to the second, and so on. In the case of on.sh
you’ll type in the pin number that you want to export and turn on. Instead of hard coding pin 25 into the shell script, it’s more universal by referring to the pin that was typed in at the command line. To export pin 25 and turn it on, you can now type:
root@raspberrypi:/home/pi/# ./on.sh 25
Exporting pin 25.
Setting direction to out.
Setting pin high.
If you still have the LED connected to pin 25 from earlier in the chapter, it should turn on. Let’s make another shell script called off.sh
which will turn the LED off. It will look like this:
#!/bin/bash echo Setting pin low. echo 0 > /sys/class/gpio/gpio$1/value echo Unexporting pin $1 echo $1 > /sys/class/gpio/unexport
Now let’s make it executable and run the script:
root@raspberrypi:/home/pi/temp#chmod +x off.sh
root@raspberrypi:/home/pi/temp#./off.sh 25
Setting pin low. Unexporting pin 25
If everything worked, the LED should have turned off.
Of course, a tiny little LED isn’t going to give off enough light to fool burglars into thinking that you’re home, so let’s hook up a lamp to the Raspberry Pi.
- Remove the LED connected to pin 25.
- Connect two strands of hookup wire to the breadboard, one that connects to pin 25 of the Raspberry Pi and the other to the ground bus.
- The strand of wire that connects to pin 25 should be connected to the “+in” terminal of the PowerSwitch Tail.
- The strand of wire that connects to ground should be connected to the “-in” terminal of the PowerSwitch Tail. Compare your circuit to Figure 7, “Connecting a PowerSwitch Tail II to the Raspberry Pi”.
- Plug the PowerSwitch Tail into the wall and plug a lamp into the PowerSwitch Tail. Be sure the lamp’s switch is in the on position.
- Now when you execute
./on.sh 25
, the lamp should turn on and if you execute./off.sh 25
, the lamp should turn off!
Note
Inside the PowerSwitch Tail there are a few electronic components that help you control high voltage devices like a lamp or blender by using a low voltage signal such as the one from the Raspberry Pi. The “click” you hear from the PowerSwitch Tail when it’s turned on or off is the relay, the core component of the circuit inside. A relay acts like a switch for the high voltage device that can be turned on or off depending on whether the low voltage control signal from the Raspberry Pi is on or off.
So now you’ve packaged up a few different commands into two simple commands that can turn a pin on or off. And with the lamp connected to the Raspberry Pi through the PowerSwitch Tail, you can turn the lamp on or off with a single command. Now you can use cron
to schedule the light to turn on and off at different times of day. cron
is Linux’s job scheduler. With it, you can set commands to execute on specific times and dates, or you can have jobs run on a particular period (for example, once an hour). You’re going to schedule two jobs; one of them will turn the light on at 8:00pm and the other will turn the light off at 2:00 am.
Note
As with other time-dependent programs, you’ll want to make sure you’ve got the correct date and time set up on your Raspberry Pi, as described in ???.
To add these jobs, you’ll have to edit the cron table (a list of commands that Linux executes at specified times):
root@raspberrypi:/home/pi/# crontab -e
This will launch a text editor to change root’s cron table. To change to the root user type sudo su
. At the top of the file, you’ll see some information about how to modify the cron table. Use your arrow keys to get to the bottom of the file and add these two entries at the end of the file.
0 20 * * * /home/pi/on.sh 25 0 2 * * * /home/pi/off.sh 25
Note
cron
will ignore any lines that start with the hash mark. If you want to temporarily disable a line without deleting it or add a comment to the file, put a hash mark in front of the line.
Type Control-X
to exit, type y
to save the file when it prompts you, and hit enter to accept the default file name. When the file is saved and you’re back at the command line, it should say installing new crontab
to indicate that the changes you’ve made are going to be executed by cron
Cron will let you schedule jobs for specific dates and times or on intervals. There are five time fields (or six if you want to schedule by year), each separated by a space followed by another space then the command to execute. Asterisks indicate that the job should execute each period. For example:
Table 1. Cron Entry for Turning Light On at 8:00pm Every Day
0 | 20 | * | * | * | /home/pi/on.sh 25 |
---|---|---|---|---|---|
Minute (:00) | Hour (8pm) | Every Day | Every Month | Every Day of Week | path to command |
Let’s say you only wanted the lamp to turn on every weekday. Here’s what the crontab entry would look like:
Table 2. Cron Entry for Turning Light On at 8:00pm Every Weekday
0 | 20 | * | * | 1-5 | /home/pi/on.sh 25 |
---|---|---|---|---|---|
Minute (:00) | Hour (8pm) | Every Day | Every Month | Monday to Friday | path to command |
Let’s say you have a shell script that checks if you have new mail and emails you if you do. Here’s how you’d get that script to run every five minutes:
Table 3. Cron Entry for Checking for Mail Every Five Minutes
*/5 | * | * | * | * | /home/pi/checkMail.sh |
---|---|---|---|---|---|
Every five minutes | Every Hour | Every Day | Every Month | Every Day of Week | path to command |
The */5 indicates a period of every five minutes.
As you can see, cron
is a powerful tool that’s at your disposal for scheduling jobs for specific dates or times and scheduling jobs to happen on a specific interval.
- eLinux’s Raspberry Pi GPIO Reference Page
- This is the most comprehensive reference guide to the Raspberry Pi’s GPIO pins.
- Adafruit: MCP230xx GPIO Expander on the Raspberry Pi
- If you don’t have enough pins to work with, Adafruit offers this guide to using the MCP23008 chip for 8 extra GPIO pins and the MCP23017 for 16 extra GPIO pins.
ADVERTISEMENT