Hardware Reference
In-Depth Information
Now you're back to what to do with the output of this value special file. You can't sit there all day and cat the file.
You could modify the code in Listing 5-1. If you wanted to, you could edit the following line:
MATSTAT='cat /sys/class/gpio/gpio4/value'
Replace gpio4 with gpio17 . Of course, this brings up another issue now that you have two sensors on your
Raspberry Pi: you would need to use SSH with the Raspberry Pi twice to see what's going on, and this is not exactly a
good idea. I am going to show you how to write a simple web server in Ruby to solve this problem.
Bring Forth the WEBrick
Why Ruby? That's an easy question to answer. For a start it's already on the Fedora remix. Second, I like Ruby: it's
simple and powerful. I am not a fan of Perl so please don't ask.
Just to be safe, check that Ruby works with this simple command:
# ruby -v
This should output the version of Ruby running on your system. Where is the web server? I won't be using any
type of big web server like Apache or Nginx. It's not because they are bad or would not do what you need; it's more
that they are both very heavy if you are just reading two system files. This is another feature of Ruby I have come to
like: the ability to create a simple web server anywhere.
Welcome WEBrick
I am not sure where the name WEBrick comes from and I have a bad habit of reading the name as “web brick”, mostly
because under a high load the server responds about as well as a brick. WEBrick was not designed for heavy loads or
big amounts of data: it's a simple HTTP and HTTPS server built inside of Ruby. The best part about WEBrick is how
easy it is to use with a few lines of code; you can serve up a directory or just a simple HTML page. It's quite powerful
for small applications or admin pages for your personal applications, but don't go and run a big commercial site on
it. When it comes to system resources, it's quite light as long as what you are serving up is also quite light. This makes
it perfect for serving up an HTML page to display the status of the GPIO ports. With around six lines of Ruby code you
could have a fully functional web server. Let's use WEBrick now.
Split your very basic application into two files:
The first one is called
security-server.rb (see Listing 5-2). This script is the one you will
run. It has two main functions: the first and most important function is to create the WEBrick
instance with the given options inside the file.
reader-loop.rb .
The reader-loop.rb script is where the real work happens (see Listing 5-3). This script will read the gpio4 and
gpio17 value files every second and generate an HTML file called index.htm . This HTML file is what WEBrick serves
up. I have included an HTML element that will refresh the page automatically every one second as well. This allows
you to leave the page on a monitoring screen and watch to see who has breached your sensors. When you start the
script for the first time it will create an index.html file and an access.log so you can keep track of who has accessed
your site.
There are three small notes about setting up this simple application. It must be run as root in order to access the
GPIO pins. It expects the two Ruby scripts to be in the same directory. You must have already exported your GPIO pins
and set their mode. Take a look at Figure 5-16 to see how I have installed the scripts. You may have noticed they are
under /root/ ; because they must be run as root and it's a demo application I can't see any issue with this.
The second function is to launch and clean up the helper script called
 
Search WWH ::




Custom Search