Hardware Reference
In-Depth Information
This script won't give you any feedback, and you cannot query the GPIO pin to get an indication of the current
outlet status. This is where a handy flag file comes in hand. This little file gets written to the filesystem and echoes the
status of the physical outlet. You can see the flag file in this line:
/bin/echo power-on > outlet-status
This will write a file to the current directory called outlet-status , with the value of power-on . When you turn off
the outlet the file contents will get overwritten with the value of power-off . This will also persist across a reboot so you
will always know the status of the outlet.
Save this script into /usr/local/bin and call it power-ctl.sh . Make sure you set the execution bit on the script.
This script only accepts the command-line arguments on and off . I am sure you can guess what they do. Take a look
at Figure 10-15 to see the script in action.
Figure 10-15. The power-ctl.sh script in action
You're going to need to take my word when I say that my mains device did turn on and off. After all it's a little hard
to embed video into a book. This script uses two functions to replicate what you did on the command line. The reason
that you see “ sleep 1 ” in between each value toggle is that the remote control takes a few milliseconds to register the
button press. Without this you may toggle the GPIO pins too quickly for the remote to notice. If you read all the lines of
code you would have also noticed I referenced another script called gpio-setup.sh . This little script is part of a bigger
project. After all I am sure you don't want to use SSH to get into the Raspberry Pi every time you want to turn this
mains device on. What happens if you have many mains devices?
Back to the WEBrick
I will be giving you another nice little web page to provide convenient management. I will be using Ruby's WEBrick
again for a lightweight web server. Just like in Chapter 5 this web server needs to be run as root for this example.
To get started, create a folder called power-ctl ; however, the name of this folder or its location do not matter.
After all I have set up WEBrick to work from its running directory. In your newly created directory, place the following
files into the directory:
gpio-setup.sh
index.html
off.cgi
on.cgi
server-power-control.rb
Take a look at Figure 10-16 to see my directory listing.
 
Search WWH ::




Custom Search