Create a hardware network monitor with a Pi and a Blinkt

Have you ever wanted a quick visual monitor to know that certain devices on your network are up and running?  Well this little project will do exactly that.  You will be able to monitor up to 8 networked devices and have an LED indicate if it is up or not.

This project assumes a few things:

  1. you  can ping the devices in question and get a timely response.
  2. you have the necessary bits of hardware, it’s only 3 bits and you can get them all for around £20 in the UK.
  3. your okay burning images to card, navigating and writing files in Linux and know at least a little bit about networking.

The Hardware

All 3 can be bought from Pimoroni in the UK, they have lots of cool maker stuff. (I’m not affiliated, just a happy customer).

 

Prep the Pi image

Before we start with the project we need to get the Pi up and running and install the Blinkt libraries.

  1. Download the latest Raspbian image and write to your SD card.
  2. Boot up the Pi, let it expand the file system and reboot.
  3. Once you have the desktop you should add the Pi to your WiFi network
  4. Using the preferences tool enable SSH or VNC if you prefer
  5. Once that is done you want to run the following command to download and install the Blinkt libraries, this page offers the full instructions (and links to other Blinkt projects that are fun), but in short open a terminal and run run this command:
    curl https://get.pimoroni.com/blinkt | bash 
  6. Reboot the Pi

 

The Pi Monitor code

This is my Python code that will work through a list of 8 network devices and light up the relevant LED in red or green to let you know if a device is responding or not.  Obviously you will need to modify the IPs and descriptions to suit your own environment.  The comments should help explain what is happening throughout the code.

#!/usr/bin/env python
import os
import time
from blinkt import set_brightness, set_pixel, show, clear

# clear the LEDs
set_brightness(0.1)
clear()
show()

# dictionary data format is - "IP", "name", status where 0 = up and 1 = down
pingdict = {0:["8.8.8.8","Google DNS ",1],
    1:["192.168.1.1","ISP router",1],
    2:["192.168.1.10","storage",1],
    3:["192.168.1.13","esx1",1],
    4:["192.168.1.22","esx2",1],
    5:["192.168.1.31","pi-hole",1],
    6:["192.168.1.121","splunk",1],
    7:["192.168.1.200","serv1",1]}

while True:
    for x in range(8):
        # change the colour slightly whilst we are testing an IP address
        if pingdict[x][2]==0:
            set_pixel(x, 0, 150, 0)
        else:
            set_pixel(x, 255, 0, 0)

        show()
        # ping the IP address (change the ping command params if running under windows)
        response = os.system("ping -c 1 -W 2 " + pingdict[x][0]+ " >nul")
        if response == 0:
            set_pixel(x, 0, 100, 0)
            print x, ' is up - ', pingdict[x][1]
            pingdict[x][2]=0
        else:
            set_pixel(x, 150, 0, 0)
            print x, ' is down - ', pingdict[x][1]
            pingdict[x][2]=1
        show()

      # pause for a few seconds then loop again
      time.sleep(2)

Save the script to a suitable location, I use the following:

/opt/pi-monitor/pi-monitor.py

You can test the script by typing the following in the terminal:

python /opt/pi-monitor/pi-monitor.py

If you want the script to start at boot time add a line near the bottom of /etc/rc.local to run the script:

python /opt/pi-monitor/pi-monitor.py &

You can now run your pi monitor headless and all being well you’ll have something a bit like this:

Posted on September 17, 2017 at 15:42 by simon · Permalink
In: Python, Raspberry Pi, Tech · Tagged with: , , ,

Leave a Reply