Introduction: Raspberry Pi Internet Monitor

In my house, you can often hear someone shouting "Is the Internet down?" Sometimes it is but most of the time it's a "user problem". I decided to build a gizmo that would make it easy to tell whether the internet connection was working or not. Hence the Raspberry Pi Internet Monitor.

The Raspberry Pi Internet Monitor is used to provide visual status of a WiFi internet connection. A Raspberry Pi periodically issues Linux ping commands to a set of web sites and based on the results will illuminate different color lamps in an industrial tower lamp mounted on a laser cut case. If the pings are successful more than 50% percent of the time, the green lamp is illuminated (first photo). If the pings are successful 1 to 50% of the time, the amber lamp is illuminated (second photo). If the pings are unsuccessful, the red lamp is illuminated (third photo).

The Raspberry Pi Internet Monitor consists of a model B Raspberry Pi, a circuit for controlling the tower lamp, an illuminated switch used to indicate when the monitor is operational and gracefully shut down the Raspberry Pi when pressed, a barrier strip for connecting the circuit board to the tower lamp, and some jacks for connecting power as shown in the "Internet Monitor Wiring Diagram". Two Python programs are used to monitor the internet connection and control the shutdown switch (for details on how these programs work see the last step of this instructable.)

The circuit for controlling the lamps uses opto-couplers connected in between the Raspberry PI's GPIO pins and the tower lamp. This is needed because the tower lamps require more voltage and current than the Raspberry Pi can deliver. The simple circuit used is shown in the "Internet Monitor Tower Light Controller Schematic". The circuit is built on an Adafruit half-size perma-proto Raspberry Pi breadboard printed circuit board as shown in the "Internet Monitor Circuit Board Layout" diagram.

I made this at TechShop.

Step 1: Parts

The following parts and tools are needed to construct the Raspberry Pi Internet Monitor:

Raspberry Pi Parts:

  • 1 Raspberry Pi model B 512MB RAM (Available from Amazon or Adafruit ID 998) (photo 1)
  • 1 16GB SDHC class 4 card (8GB can also be used) (photo 2)
  • 1 EDIMAX Wireless 802.11b/g/n nano USB Adapter (available from Amazon) or other supported WiFi adapter for Raspberry Pi (photo 3)
  • 2 5/8" #4-40 Machine screws and nuts (photo 4)
  • 2 1/4" Round nylon spacers for #4 machine screws (Amazon Supply part number B000FP9YW0) (photo 4)
  • 2 #4 Flat nylon washers (Amazon Supply part number B000FN1560) (photo 4)
  • 1 6" USB Cable with male type A and male micro B connectors (Adafruit ID 898) (photo 5)
  • 1 Neutrik NAUSB-W-B Reversible USB 2.0 gender changer (type A and B) (available from eBay) (photos 6 and 7)
  • 2 #4-40 3/8" flat head machine screws and nuts for the NAUSB-W-B (photo 8)

Tower Lamp Controller Circuit Board Parts:

  • 1 Half-size Perma-Proto Raspberry Pi Breadboard PCB Kit (circuit board and GPIO socket) (Adafruit ID 1148) (photo 9)
  • 1 GPIO Ribbon Cable for Raspberry Pi Model A and B - 26 pin 6" (Adafruit ID 862) (photo 10)
  • 3 1/4 watt 180 ohm resistors (photo 11)
  • 1 LTV847 Quad Optocoupler (Jameco Part no. 878286) (photo 12)
  • 1 16 Pin IC Socket (Jameco Part no. 37373) (photo 13)
  • 1 Break-away 0.1" male header (Adafruit ID 392) (photo 14)
  • 1 DC Power Jack 2.1mm (Jameco Part no. 151555) (photos 15 and 16)
  • 5 5/8" #4-40 Machine screws and nuts (photo 17)
  • 5 1/4" Round nylon spacers for #4 machine screws (Amazon Supply part number B000FP9YW0) (photo 17)
  • 5 #4 Flat nylon washers (Amazon Supply part number B000FN1560) (photo 17)
  • 4 Spade tongue terminals for 16-22 AWG wires (Radio Shack 64-3031 or Jameco Part no. 842929) (photo 22)
  • Heat shrink tubing (3/32" diameter) (photo 23)

Industrial Tower Lamp Parts:

  • 1 DC 12V Safety red yellow green industrial tower lamp (available from eBay) (photo 18)
  • 3 #6-32 1/2" Machine screws, nuts, and washers (photo 19) for mounting the tower lamp
  • 1 4 Position connector barrier strip (similar to Jameco Part no. 230990) (photo 20)
  • 2 1/2" #6-32 Machine screws and nuts (photo 21) for mounting the barrier strip
  • 4 Spade tongue terminals for 16-22 AWG wires (Radio Shack 64-3031 or Jameco Part no. 842929) (photo 22)
  • Heat shrink tubing 3/32" diameter (photo 23)

Shutdown Switch Parts:

  • 1 16mm Illuminated push button red momentary switch (Adafruit ID 1439) (photo 24)
  • 1 470 Ohm 1/4 watt resistor (photo 25)
  • 4 12" Jumper wires (one end male, one end female) (Sparkfun part number PRT-09385) (photo 26)
  • Heat shrink tubing (1/16" diameter) (photo 26)

Case Parts:

  • 1 Sheet of 16" x 16" 1/8" (3mm) birch plywood (not pictured)
  • 24 #4-40 1/2" Machine screws and nuts for holding the case together (photo 28)
  • 4 Self adhesive rubber/plastic feet/bumpers (height 1/4") (photo 29)

Power Supplies:

  • 1 5 Volt 1 amp USB port power supply (Adafruit ID 501) (photo 30)
  • 1 4 to 6 foot USB cable with a type B male connector and type A male connector (also called USB printer cables) (photo 31)
  • 1 12 Volt 1 Amp Power Supply with Center Positive 2.1mm Connector (Jameco 1940774 or Adafruit ID 798) (photo 32)

Miscellaneous Parts:

  • 18 AWG Wire (photo 33)

Tools (not pictured):

  • Soldering iron and solder
  • Heat source for heat shrink tubing (heat gun, matches, lighter, or candle)
  • Wire cutters
  • Wire strippers
  • Small Philips head screw driver
  • Small flat head screw driver
  • Crimping Tool (Home Depot Store SKU # 131485 or Radio Shack catalog #: 6400225)
  • Needle nose pliers
  • 120 grade sandpaper (optional)
  • Painter's tape (optional)

Equipment for installing Raspberry Pi software (not pictured):

  • HDMI capable computer monitor
  • USB keyboard
  • USB mouse Ethernet cable
  • Ethernet LAN connection to the Internet

Step 2: Cut the Case Parts With a Laser Cutter

The first step is to cut the parts for the case out of a sheet of 1/8" (3mm) birch plywood. A 45 watt Epilog Helix laser cutter at TechShop was used to cut and engrave the parts. All of the case parts can be cut out from one 16" x 16" sheet of plywood.

The design is shown in the first diagram. The black lines will be cut by the laser cutter; the blue lines will be etched using a low power cutting setting; green text will be engraved; red lines will not be cut - they are included to show the outline of the parts. I used the color mapping capability of the Laser cutter to specify the settings for cutting and engraving. The setting used are shown in the right hand side of the "Color Mapping" screen capture.

The individual parts are:

  • Bottom panel with mounting holes for the Raspberry Pi, circuit board, and barrier strip (photo 1)
  • Top panel with mounting holes for the industrial tower lamp (photo 2)
  • Side panel with cutout for Raspberry Pi LAN and USB ports (photo 3)
  • Side panel with cutouts for the power plugs (photo 4)
  • Side panel with the cutout for the shutdown switch (photo 5)
  • Side panel with no cutouts (photo 6)

The burn marks left by the laser can be removed with a gentle sanding using 120 grit sandpaper.

The laser cutting design files are in the ZIP file attached to this step. The file contains CorelDraw X5 (.cdr) and Encapsulated PostScript (.eps) files for the design and the file for loading the color mapping data for the laser cutter. See page 113 of https://www.epiloglaser.com/downloads/pdf/mini_he... for details on how to use color mapping.

Note: Since I was not able to provide an exact part number for the barrier strip I used in this project, you may need to adjust the design slightly. Barrier strips vary in size and mounting hole position so before you laser cut the plywood, make sure that the mounting holes in the design file match your barrier strip.

Step 3: Setup the Raspberry Pi Software

The first step in preparing the Raspberry Pi is to install and configure the Raspbian software:

  1. Download and load Raspbian (http://www.rasbian.com/RaspbianImages) onto a SD card. Instructions for how to do this can be found here (https://learn.adafruit.com/adafruit-raspberry-pi-lesson-1-preparing-and-sd-card-for-your-raspberry-pi/overview). I used the January 31, 2015 version of Raspbian: I would recommend using this version or a later one for this project: don't use an earlier version.
  2. Insert the SD card into the slot on the Raspberry Pi.
  3. Connect the Raspberry Pi to a display, keyboard, mouse, LAN with access to the internet, and a 5 volt USB power adapter as shown in the "Raspberry Pi Setup" diagram.
  4. Boot the Raspberry Pi and perform the first time configuration according to the instructions here (https://learn.adafruit.com/adafruits-raspberry-pi-lesson-2-first-time-configuration). When configuring Raspbian, do not configure the graphical user interface to start automatically. It's also a good idea to change the password for the Raspberry Pi at this time.

The second step is to download the Python programs that controls the shutdown switch and that monitors the internet connection. Create the directory where the Python programs will reside using these commands:

cd /home/pi
mkdir python_programs
cd python_programs

Issue the following two commands to get the Python programs:

wget "https://s3-us-west-1.amazonaws.com/talk2bruce/instructables/rpi-internet-monitor/rpi-halt-btn.py"
wget "https://s3-us-west-1.amazonaws.com/talk2bruce/instructables/rpi-internet-monitor/rpi-internet-monitor.py"

The third step is to configure the Raspberry Pi for use with your wireless network. Edit the file "wpa_supplicant.conf" with the command:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Add the following lines to the bottom of the file substituting the name of your wireless network and your wireless network's password as shown and save the file. If you are unfamiliar with how to use the nano editor, howtogeek has a nice Beginner's Guide to Nano.

network={
  ssid="my wifi network name"
  psk="my wifi password"
}

The file should look like the what is shown in screenshot 1. More details on how to configure the Raspberry Pi for different kinds of WiFi networks can be found on the Sparkfun website.

The fourth step is to configure the system to start the Python program that monitors and illuminates the shutdown button during the system boot process. Edit the file "rc.local" with the command:

sudo nano /etc/rc.local

Add the command below to the bottom of the file before the line that says "exit 0" and save the file.

python /home/pi/python_programs/rpi-halt-btn.py&

The file should look like the one in screenshot 2.

The fifth step is to configure the system to run the Python internet monitoring program when the WiFi on the Raspberry Pi is up and running. Once the Python internet monitoring program is running, it periodically tests the connection to the internet and will illuminate the appropriate lamp. The command to start the program is placed in the "/etc/network/interfaces" file. Edit the file with the command:

sudo nano /etc/network/interfaces

Add the following command to the bottom of the file:

post-up python /home/pi/python_programs/rpi-internet-monitor.py

The file should look like the one in screenshot 3.

Configuration of the Raspberry is now complete.

The last step is to shutdown the Raspberry Pi with the command:

sudo halt

After the Raspberry Pi shuts down, disconnect all the cables but leave the SD Card in the socket on the Raspberry Pi.

Step 4: Prepare Wires for Connecting to the Tower Lamp Barrier Strip

Cut four 7" lengths of wire (photo 1).

Strip 1/4" of wire from one end of each wire, place a spade tongue terminal on the stripped end of each wire and crimp the terminal using a crimping tool (photo 2 and 3).

Step 5: Attach Spade Tongue Terminals to Tower Lamp Wires

Strip 1/4" of insulation off the ends of each of the wires (photo 2)

Using a soldering iron "tin" the end of the wires by applying the tip of your iron to each of the wires for a second or two, then touching the solder to the wire. The solder should flow freely onto the wire and in to the strands, coating it (photo 3).

Cut 1/4" of 3/32" diameter heat shrink tubing for each of the wires and slide the heat shrink tubing onto the wires (photo 4). The heat shrink tubing is very important: the wires from the tower lamp are very thin and the heat shrink tubing will add strength to prevent the wires from breaking off the spade tongue terminals.

Place a spade tongue terminal on the end of each wire and crimp the terminal using a crimping tool (photo 5).

Slide the heat shrink tubing over each of the ends of the spade tongue terminals and using a heat source (heat gun, matches, lighter, or candle) shrink the tubing (photos 6 and 7).

Step 6: Prepare the Power Jack

Cut two 1/4" lengths of 1/16" heat shrink tubing (photo 1).

Cut two 6" lengths of wire, strip both ends and solder one end of each to the inner terminals on the power jacks (photo 2). Make sure to connect the red wire to the center terminal.

Slide the heat shrink tubing onto the wires and move over the inner terminals (photo 3).

Use a heat source(heat gun, matches, lighter, or candle), shrink the tubing over the terminals (photo 4).

Step 7: Prepare the Shutdown Switch

Using the following parts: push button switch (photo 1), jumper wires (photo 2), 470 ohm resistor (photo 3), and heat shrink tubing (photo 4), prepare the switch:

    Cut four lengths of 1/16" heat shrink tubing (photo 5).

    Cut off the male ends of the jumper wires (photo 6).

    Strip the ends off the wires (photo 7). Slide the heat shrink tubing onto the wires (photo 8).

    Using photo 9 as a guide to the terminals on the switch, solder the resistor to the positive (+) terminal (photo 10).

    Attach and solder the red wire to the resistor (photos 11 and 12).

    Cutoff the excess wire on the resistor.

    Attach and solder the black wire to the negative (-) terminal (photos 13 and 14) using photo 9 as a guide.

    Attach and solder the blue wires to the remaining terminals (photo 15).

    Slide the heat shrink tubing over the resistor and over the exposed wires soldered to the terminals (photo 16).

    Use a heat source (heat gun, lighter, candle, or match) to shrink the tubing.

    The completed switch is shown in photo 17.

    Step 8: Assemble and Solder the Circuit Board

    The circuit board is used to isolate the the Raspberry Pi from the higher voltage used by the tower lamp. The diagram shows the wiring for circuit board and the connections to Raspberry Pi and tower lamp.

    Solder the components to the circuit board as follows (before soldering double check to make sure you have the components in the correct holes):

      Using the diagram and photos 1 and 2 as guides, cut and solder wires as shown, solder the resistors in place, and then solder the GPIO connector and IC socket to the board. Make sure the notch on the GPIO connector and the notch on the IC socket are pointing in the same way as in the diagram and the photos.

      Strip the ends of the wires prepared in step 4 of this instructable and solder to the board as illustrated in photos 3 and 4.

      Solder the wires from the power jack prepared in step 6 of this instructable to the board as shown in photo 5.

      Carefully insert the LTV847 Quad Optocoupler into the IC socket (photos 6 and 7).

      Break off 4 of the headers from the Break-away 0.1" male header strip (photo 8).

      Solder the header strip to the circuit board (photo 9).

      Step 9: Connect the Components

      Attach the Raspberry Pi to the circuit board using the GPIO ribbon cable (photos 2 to 6).

      Attach the four wires with the spade tongue connectors to the barrier strip (photos 6 to 8).

      Step 10: Attach the Shutdown Switch to the Case

      Remove the fastener ring from the switch (photo 1).

      Insert the switch through the hole in the shutdown switch side panel (photo 2).

      Slide the fastener ring over the wires with jagged edge facing the side panel and screw onto the switch (photo 3).

      Before tightening the fastener ring, use the engraved lines on the outside of the panel as guides to make sure the switch is not crooked (photo 4).

      Tighten the fastener ring to hold the switch firmly in place (photo 5).

      Step 11: Prepare USB Power Connector and Attach It to the Case

      Disassemble the NAUSB-W-B Reversible USB 2.0 gender changer (photos 1 and 2) by removing the screws (photo 3).

      Reassemble the USB gender changer with the Type B plug facing outwards and the Type A plug on the inside (photos 4 and 5).

      Use two #4-40 3/8" flat head machine screws and nuts (photo 6) to attach the gender changer to the side panel as shown in photos 7 to 10.

      Step 12: Attach the Raspberry Pi to the Bottom Panel

      Using the 5/8" #4-40 machine screws, the 1/4" round nylon spacers, the #4 flat nylon washers, and the #4 nuts (photo 1), attach the Raspberry Pi to the bottom panel as follows:

      Insert the two screws from the bottom (you can use painter's tape to hold them in position if your hands are not large enough to keep a finger on each screw head) (photo 2).

      Place a nylon spacer on each screw (photo 3).

      Place the Raspberry Pi onto the screws (photo 4).

      Place a nylon washer onto each of the screws (photo 5).

      Screw a nut onto each of the screws and tighten enough to hold the board firmly in place (photo 6). Don't over tighten the screws.

      Step 13: Attach the Circuit Board to the Bottom Panel

      Using the 5/8" #4-40 machine screws, the 1/4" #4 round nylon spacers, the #4 flat nylon washers, and the #4 nuts (photo 1), attach the circuit board to the bottom panel as follows:

      Insert the five screws from the bottom (you can use painter's tape to hold them in position if your hands are not large enough to keep a finger on each screw head) (photo 2).

      Place a nylon spacer on each screw (photo 3).

      Place the circuit board onto the screws (photo 4).

      Place a nylon washer onto each of the screws (photo 5).

      Screw a nut onto each of the screws and tighten enough to hold the board firmly in place (photo 6). Don't over tighten the screws.

      Step 14: Attach the Barrier Strip to the Bottom Panel

      Use two 1/2" #6-32 machine screws and nuts (photo 1) to attach the barrier strip to the bottom panel (photo 2) as shown in photos 3 to 5.

      Step 15: Attach the Tower Lamp to the Top Panel

      Insert the wires from the tower lamp (photo 1) into the center hole of top panel (photo 2) as shown in photos 3 and 4.

      Align the tower lamp with the mounting holes on the top panel (photos 5 and 6).

      Use three #6-32 1/2" machine screws, nuts, and washers (photo 7) to attach the tower lamp to the panel (photos 8 and 9). Tighten the screws to securely hold the tower lamp to the panel.

      Step 16: Assemble the Case

      The case for Raspberry Pi Internet Monitor is held together with #4-40 1/2" Machine screws and nuts: gluing is not required. The way to do this is to thread the nuts on the screws (photo 2), fit the panels together (photo 1), place the screw into the T-slot (photo 3) and gently tighten with a screw driver while holding the nut with your fingers. When the whole case is held together using screws this way, it's very sturdy.

      Photo 4 shows the proper alignment of the bottom panel and side panels. Using that as a guide, assemble the case as follows:

        Attach the blank side panel to the bottom panel (photo 5).

        Attach the side panel with the power jack cutouts (photos 6 to 9).

        Attach the side panel with the Raspberry Pi LAN and USB cutouts (photos 10 to 13).

        Attach the side panel with the shutdown switch (photos 14 to 16).

        Attach the four rubber feet (photo 17) to the bottom panel (photo 18).

        Step 17: Connect the Shutdown Switch

        Connect one of the blue wires from the shutdown switch to the male header (photo 1); it does not matter which of the two blue wires you use.

        Connect the other blue wire from the shutdown switch to the male header (photo 2).

        Connect the red wire from the shutdown switch to the male header (photo 3).

        Connect the black wire from the shutdown switch to the male header (photo 4).

        Step 18: Connect the Raspberry Pi to the USB Power Port

        Connect the micro type B male connector of the 6" USB cable (photo 1) to the Raspberry Pi (photo 2).

        Connect the type A male connector to the Reversible USB 2.0 gender changer (photo 3).

        Step 19: Attach the Lamp Power Port to the Case

        Unscrew the nut from the power jack, insert the power jack into the hole on the side panel next to the USB gender changer, and fasten to the panel using the nut (photos 1 and 2).

        Step 20: Connect the Tower Lamp Wires

        Attach the colored wires from the tower lamp to the matching colored wires on the barrier strip as shown in photos 1 to 4.

        Step 21: Attach the Top Panel

        Gently push the top panel onto the case making sure that all of the wires are inside of the box (photo 2).

        Use 1/2" #4-40 screws and nuts to secure the top to the case as shown in photos 3 to 6.

        Step 22: Assembled Raspberry Pi Internet Monitor

        The assembly of the Raspberry Pi Internet Monitor is now complete. Photos of the completed internet monitor taken from various angles are shown in this step.

        Step 23: Attach the WiFi Adapter and Power Cables

        Plug the USB WiFi adapter into one of the USB slots on the Raspberry Pi (photos 1 and 2).

        Attach the power cables to the jacks on the side panel (photo 3) as follows:

          Plug the male type A end of the USB cable (photo 4) into the jack in the USB power adapter (photo 5).

          Plug the male type B end of the USB cable into jack on the side panel of the case (photo 6).

          Plug the connector from the 12 VDC power supply (photo 7) into the jack on side panel of the case (photo 8).

          Step 24: Power Up and Voila!

          Power up the Raspberry Pi Internet Monitor by first plugging the 12 volt power supply in to an AC outlet and then plugging the USB power adapter into an AC outlet.

          You can tell the Raspberry Pi is powered on by looking into the LAN / USB port cutout. You should see a red LED illuminated (photo 2).

          The blue LED on the USB WiFi adapter will illuminate and blink after about 30-45 seconds as the Raspberry Pi connects to the WiFi network (photo 3).

          After about 10-15 more seconds, when the Raspberry Pi has successfully connected to the WiFi network, the Raspberry Pi Internet Monitor will flash the red, amber, and green lamps in sequence five times. The amber lamp will remain on while the status of the internet connection is tested (photo 6).

          The shutdown switch will then illuminate (photos 4 and 5).

          After the The Raspberry Pi Internet Monitor monitor has completed testing, the status of the internet connection will be displayed.

          • Yellow - the internet connection is poor: 50% or less of the tests were successful (photo 6)
          • Green - the internet connection is working well: more than 50% of the tests were successful (photo 7)
          • Red - the internet connection is not working: none of the tests were successful (photo 8)

          The tests will be repeated every 2 minutes.

          To shutdown the Raspberry Pi Internet Monitor, press the shutdown switch. After 20-30 seconds, tower lamp LED and the shutdown switch LED will go out and the Raspberry PI will be shutdown. You can now safely unplug the power adapters.

          Voila! The Raspberry Pi Internet Monitor is now complete!

          Step 25: How the Python Programs Work

          There are two customer Python programs used in this instructable to create the Raspberry Pi Internet Monitor.

          rpi-internet-monitor.py

          The first program "rpi-internet-monitor.py" is started by the command:

          post-up python /home/pi/python_programs/rpi-internet-monitor.py

          in the system file "/etc/network/interfaces". The "post-up" command is run when the network interfaces are up and running.
          Listings 1 to 5 show the source code for the rpi-internet-monitor.py program:

          Lines 3-17 (listing 1) import needed libraries and define constants that will be used within the program.

          • Lines 3-6 import the libraries that are requires to issue system commands, get the time, and allow the program to read and write the GPIO pins.
          • Line 8 defines the GPIO pin used to tell whether the button on the shutdown switch was pressed.
          • Line 9 define the GPIO pin used to control the illumination of the light on the shutdown switch.
          • Lines 10-12 defines the GPIO pins used to control the illumination of the LEDs in the tower lamp.
          • Line 14 sets the number of seconds to wait between issuing the "ping" command.
          • Line 15 sets the number of seconds to wait between issuing the tests of how well the internet connection is working.
          • Line 17 lists the sites that are pinged to see how well the internet connection is working. Google is always a good choice. I chose Comcast because they are my internet service provider. These can be changed to whatever you want and you can additional sites to the list as well.

          Lines 19 - 22 (listing 1) define the "debug_message" function. This function is used to print out messages that help to debug the "rpi-internet-monitor.py" Python program if it isn't working properly. Debugging is turned on by running the program with the -debug switch as follows:

          python /home/pi/python_programs/rpi-internet-monitor.py -debug
          • Lines 21-22 check to see if the input variable "debug_indicator" is set to "True" and if so, print the message specified "output_message", otherwise no messages are printed.

          Lines 24-34 (Listing 2) define the "ping" function. The ping function issues the "ping" system command to determine whether the site specified by "site" is reachable. This function will return a zero if the ping was unsuccessful and a one if the ping was successful.

          • Line 26 constructs the Linux command to do the ping.
          • Line 28 issues the command using the "check_output" function from the "subprocess" library.
          • Line 29 tells the python program to execute lines 30 and 31 if the command fails (e.g., the site is not reachable).
          • Line 30 prints a debugging message indicating the site is not reachable.
          • Line 31 returns a zero to the function that called "ping".
          • Line 32 is where execution continues if the ping was successful.
          • Line 33 prints a debugging message indicating the site is reachable.
          • Line 34 returns a one to the function that called "ping".

          Lines 36-46 (listing 2) define the "ping_sites" function. The "ping_sites" function pings each of the sites specified in "site_list" the number of times specified by "times" with a delay in seconds in between the pings specified by "wait_time".

          • Line 39 sets the number of successful pings to zero.
          • Line 40 sets the total number of pings that will be attempted by multiplying the number of tries specified by "times" by the number of sites listed in "site_list".
          • Line 41 creates a loop that will be executed the number of times specified by "times".
          • Line 42 creates another loop that will be executed for each site in "site_list".
          • Line 43 calls ping and adds the value returned by "ping" (zero if unsuccessful, one if successful) to the number of successful pings.
          • Line 44 calls the "time.sleep" system function to wait for the number of seconds specified by "wait_time".
          • Line 45 is run after all the loops have completed and prints a debug message indicating what percentage of the pings tried were successful.
          • Line 46 returns the percentage of pings that were successful to the function that called "ping_sites".

          Lines 48-53 (listing 3) define the "lamp_amber_on" function. This function is called to turn the amber lamp on to indicate when the internet connection is working but is not optimal.

          • Line 50 prints a debugging message indicating that function was called.
          • Line 51 uses the "output" function in the GPIO library to make sure the red lamp is turned off.
          • Line 52 uses the "output" function in the GPIO library to turn the amber lamp is turned on.
          • Line 53 uses the "output" function in the GPIO library to make sure the green lamp is turned off.

          Lines 55-60 (listing 3) define the "lamp_green_on" function. This function is called to turn the green lamp on to indicate when the internet connection is working.

          • Line 57 prints a debugging message indicating that function was called.
          • Line 58 uses the "output" function in the GPIO library to make sure the red lamp is turned off.
          • Line 59 uses the "output" function in the GPIO library to make sure the amber lamp is turned off.
          • Line 60 uses the "output" function in the GPIO library to turn the green lamp on.

          Lines 62-67 (listing 3) define the "lamp_red_on" function. This function is called to turn the red lamp on to indicate when the internet connection is not working.

          • Line 64 prints a debugging message indicating that function was called.
          • Line 65 uses the "output" function in the GPIO library to turn the red lamp on.
          • Line 66 uses the "output" function in the GPIO library to make sure the amber lamp is turned off.
          • Line 67 uses the "output" function in the GPIO library to make sure the green lamp is turned off.

          Lines 69-74 (listing 4) define the "lamp_all_off" function. This function is called to turn all the lamps off.

          • Line 71 prints a debugging message indicating that function was called.
          • Line 72 uses the "output" function in the GPIO library to make sure the red lamp is turned off.
          • Line 73 uses the "output" function in the GPIO library to make sure the amber lamp is turned off.
          • Line 74 uses the "output" function in the GPIO library to make sure the green lamp is turned off.

          Lines 76-88 define the "lamp_test" function. This function is used during start up of the program to test the lamps. The function flashes all the lamps in sequence five times with a tenth of a second delay between flashes.

          • Line 78 prints a debugging message indicating the lamp test is starting.
          • Line 79 sets the delay between flashes to a tenth of a second.
          • Line 80 creates a loop that will run execute lines 81-86 five times.
          • Line 81 uses the system function "time.sleep" to wait for a tenth of second.
          • Line 82 calls "lamp_red_on" to turn just the red lamp on.
          • Line 83 waits for a tenth of a second.
          • Line 84 calls "lamp_amber_on" to turn just the amber lamp on.
          • Line 85 waits for a tenth of a second.
          • Line 86 calls "lamp_green_on" to turn just the green lamp on.
          • Line 87 is run after the loop finishes and calls the "lamp_all_off" to turn all the lamps off.
          • Line 88 prints a debugging message indicating the lamp test has completed.

          Lines 90-127 (listing 5) is the main program.

          Lines 92-99 check the options passed to the program to determine if debugging messages should be printed.

          • Line 93 sets "debug" to False indicating if the command does not have the debugging option specified that debugging messages should not be printed.
          • Line 94 uses the system variable "sys.argv" to see if any options were specified on the command line. If they were, lines 95-99 will be executed.
          • Line 95 checks to see if "-debug" was specified.
          • Line 96 is run if "-debug" was specified and sets the value of "debug" to True indicating debug messages should be printed.
          • Line 97 makes lines 98 and 99 run if something other than "-debug" was specified.
          • Line 98 prints an error message indicating that an unknown option was specified and what it was.
          • Line 99 stops the program with an error code of 1 because of the unknown option.

          Lines 101-105 setup the GPIO pins used by the program to control the lamps.

          • Line 102 uses the "setmode" function from the GPIO library to set the numbering scheme the program will use for the GPIO pins
          • Line 103 uses the "setup" function from the GPIO library to set the pin used to control the green lamp to an output pin.
          • Line 104 uses the "setup" function from the GPIO library to set the pin used to control the amber lamp to an output pin.
          • Line 105 uses the "setup" function from the GPIO library to set the pin used to control the red lamp to an output pin.

          Lines 107-111 flash the lamps to indicate the program is starting and leave the amber lamp on until the program starts testing the internet connection status.

          • Line 108 uses the "turn_all_off" function to turn all the lamps off.
          • Line 109 uses the "lamp_test" function to flash the lamps in sequence five times.
          • Line 110 waits for half a second.
          • Line 111 uses the "lamp_amber_on" function to turn the amber lamp on.

          Lines 113-126 is the main loop that pings sites, turns the appropriate lamp on based on the pings, waits, and the repeats until the shutdown switch is pressed or power is turned off.

          • Line 114 sets the count of tests run to zero.
          • Line 115 creates a loop that will run until the shutdown switch is pressed or power is turned off.
          • Line 116 adds one to number of tests run.
          • Line 117 prints a debugging message indicating that a test is starting.
          • Line 118 calls the "ping_sites" function specifying the list of sites to test, the delay between pings, and the number of pings to do.
          • Lines 119 and 120 check to see if the percentage of successful tests were zero and, if so, use the "lamp_red_on" function to turn the red lamp on.
          • Lines 121 and 122 check to see if the percentage of successful tests was less than or equal to 50% and, if so, use the "lamp_amber_on" function to turn the amber lamp on.
          • Lines 123 and 124 are executed if the percentage of successful tests were between 51% and 100% and use the "lamp_green_on" to turn the green lamp on.
          • Line 125 prints a debugging message indicating that the program will wait for the specified number of seconds before running the next test.
          • Line 126 uses the "time.sleep" system function to wait for specified number of seconds. After the delay ends, the program goes back to line 116.

          rpi-halt-btn.py

          The second program "rpi-halt-btn.py" is started during the boot process by the command

          python /home/pi/python_programs/rpi-halt-btn.py&

          in the system file "/etc/rc.local". The "&" at the end of the command makes the program run as a separate process that keeps running until the program exits.
          Listing 6 shows the source code for the rpi-halt-btn.py program:

          Lines 2-13 initialize the program.

          • Lines 2-3 import the libraries that are required to control the devices connected to the GPIO pins, issue system commands, allows the program to sleep.
          • Lines 6-7 define the pin that will monitor the shutdown switch and the pin that will control the illumination of the LED in the switch.
          • Line 8 uses the "setmode" function from the GPIO library to set what numbering scheme the program will use for the GPIO pins.
          • Line 9 uses the "setup" function from the GPIO library to set the GPIO pin for controlling the LED in the switch to be an output pin.
          • Line 10 uses the "setup" function from the GPIO library to set the GPIO pin for monitoring the switch for presses to be an input pin.
          • Line 11 uses the "output" function from the GPIO library to illuminate the LED in the switch by setting the value of the pin to true.
          • Line 13 prints a message indicating that the switch is now being monitored.

          Line 18 uses the "wait_for_edge" function from the GPIO library to cause the program to wait for the switch to be pressed.

          Lines 19-21 are used for debugging. When the program is run from the command line and Ctrl/C is pressed these lines will be executed.

          • Line 20 will print a message indicating that the user stopped the program.
          • Line 21 uses the "cleanup" function from the GPIO library to stop the monitoring of the GPIO pins.

          Lines 22-26 are run when the shutdown switch has been pressed.

          • Line 23-25 cause the LED on the switch to turn off briefly to give the user feedback that the switch was pressed.
            • Line 22 will print a message indicating that the shutdown switch was pressed.
            • Line 23 uses the "output" function from the GPIO library to turn the LED off.
            • Line 24 causes the program to sleep for half a second.
            • Line 25 turns the LED back on.
          • Line 26 uses the "subprocess.call" function to execute the "sudo halt" command which will cause the Raspberry Pi to shutdown.

          When the Raspberry Pi has shutdown the LED on the shutdown switch will turn off indicating it's safe to unplug the camera from the power outlet.