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