If everything worked, the LED should have turned off.
Connecting a Lamp
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:
1. Remove the LED connected to pin 25.
2. 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.
3. The strand of wire that connects to pin 25 should be connected
to the “+in” terminal of the PowerSwitch Tail.
4. The strand of wire that connects to ground should be con-
nected to the “-in” terminal of the PowerSwitch Tail. Compare
your circuit to Figure 6-8.
5. 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 po-
sition.
6. Now when you execute ./on.sh 25, the lamp should turn on. If
you execute ./off.sh 25, the lamp should turn off!
Figure 6-8.
Connecting a PowerSwitch Tail II to the Raspberry Pi
104 Getting Started with Raspberry Pi
GSW_RASPI_4ED_FIN.indd 104GSW_RASPI_4ED_FIN.indd 104 10/28/21 10:54 AM10/28/21 10:54 AM
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.
Scheduling Commands with cron
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 the day. cron is Linux’s job scheduler. With it, you can set com-
mands 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:00 p.m.,
and the other will turn the light off at 2:00 a.m.
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 “Setting the Date
and Time” on page 47.
To add these jobs, you’ll have to edit the cron table (a list of com-
mands 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
Basic Input and Output 105
GSW_RASPI_4ED_FIN.indd 105GSW_RASPI_4ED_FIN.indd 105 10/28/21 10:54 AM10/28/21 10:54 AM
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
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. cron also expects a blank line at the end of
the file; if you get unexpected behavior from your cron
table, make sure you haven’t filled that blank line with
text/commands and not replaced it.
Press Ctrl-X to exit, press y to save the file when it prompts you,
and hit Enter to accept the default filename. 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.
More About cron
cron will let you schedule jobs for specific dates and times or at
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 (Table 6-1).
Table 6-1.
cron entry for turning light on at 8:00 p.m. everyday
0
20
* * *
/home/pi/on.sh25
Minute
(:00)
Hour
(8 p.m.)
Everyday
Every
month
Every
day of
the week
Path to
command
Let’s say you only wanted the lamp to turn on every weekday. Table
6-2 shows what the crontab entry would look like.
106 Getting Started with Raspberry Pi
GSW_RASPI_4ED_FIN.indd 106GSW_RASPI_4ED_FIN.indd 106 10/28/21 10:54 AM10/28/21 10:54 AM
Table 6-2.
cron entry for turning light on at 8:00 p.m. every weekday
0
20
* *
1-5
/home/pi/on.sh25
Minute
(:00)
Hour
(8 p.m.)
Every-
day
Every
month
Monday
to Friday
Path to
command
As another example, let’s say you have a shell script that checks if
you have new mail and emails you if you do. Table 6-3 shows how
you’d get that script to run every five minutes.
Table 6-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
the 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 at specific intervals.
Going Further
eLinux’s Raspberry Pi GPIO Reference Page
(bit.ly/1vORWl3)
This is the most comprehensive reference guide to the
Raspberry Pi’s GPIO pins.
Gordon Henderson’s Command Line GPIO Utility
(projects.drogon.net/raspberry-pi/wiringpi/the-gpio-utility)
This command line utility makes it easier to work with GPIO pins
from the command line. It’s bundled with all the latest versions
of the Raspberry Pi OS. Try running the command gpioreadall
to get an overview of all your pins.
Basic Input and Output 107
GSW_RASPI_4ED_FIN.indd 107GSW_RASPI_4ED_FIN.indd 107 10/28/21 10:54 AM10/28/21 10:54 AM
GSW_RASPI_4ED_FIN.indd 108GSW_RASPI_4ED_FIN.indd 108 10/28/21 10:54 AM10/28/21 10:54 AM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset