PingMonitor - Monitor your ping times using LedBorg

This is an old driver based LedBorg script
We recommend using the new driver free based scripts for LedBorg.
The new driver free examples can be found here, the installation can be found here.

Ever wanted to check ping times using your Raspberry Pi, maybe check if a game server is good, or simply check your internet connection?
Well now you can some ping times using your LedBorg with PingMonitor.py, it pings a server and displays the ping against a target result visually:


0%
to
22%
23%
to
44%
45%
to
67%
68%
to
89%
90%
to
110%
target :)
111%
to
132%
133%
to
155%
156%
to
177%
178%
or
more
Timeout
or
failed

There are some settings you may wish to change:
  • pingAddress, line 17
    Sets the server address (name or IP) to ping
  • targetPing, line 18
    The target ping time, in milliseconds
  • delayInterval, line 19
    The time to wait between pings, in seconds
  • colourTimeout, line 16
    The colour to use if a ping times out or has an error
  • colourGrade, line 15
    The colour gradient to use, from 0% to 200% of targetPing
Here's the code, you can download the PingMonitor script file as text here
Save the text file on your pi as PingMonitor.py
Make the script executable using
chmod +x PingMonitor.py
and run using
./PingMonitor.py

#!/usr/bin/env python
# coding: Latin-1

# Load the libraries we need
import time
import os

# Make a function to set the LedBorg colour
def SetColour(colour):
    LedBorg=open('/dev/ledborg','w')
    LedBorg.write(colour)
    LedBorg.close()

# User settings
colourGrade = ['002', '012', '022', '021', '020', '120', '220', '210', '200']
colourTimeout = '202'               # Colour to use for ping failures
pingAddress = 'www.google.com'      # Address to ping
targetPing = 30                     # Target ping time in milliseconds
delayInterval = 1.0                 # Time to wait between pings, in seconds

# Main code
SetColour('000')
try:
    while True:
        # Perform the ping using the system ping command (one ping only)
        rawPingFile = os.popen('ping -c 1 %s' % (pingAddress))
        rawPingData = rawPingFile.readlines()
        rawPingFile.close()
        # Extract the ping time
        if len(rawPingData) < 2:
            # Failed to find a DNS resolution or route
            failed = True
            latency = 0
        else:
            index = rawPingData[1].find('time=')
            if index == -1:
                # Ping failed or timed-out
                failed = True
                latency = 0
            else:
                # We have a ping time, isolate it and convert to a number
                failed = False
                latency = rawPingData[1][index + 5:]
                latency = latency[:latency.find(' ')]
                latency = float(latency)
        # Work out the colour to use
        if failed:
            colour = colourTimeout
        else:
            colour = latency / (targetPing * 2.0)
            colour = colour * len(colourGrade)
            colour = int(colour)
            if colour >= len(colourGrade):
                colour = len(colourGrade) - 1
            colour = colourGrade[colour]
        # Display the colour
        SetColour(colour)
        # Wait for the delay interval
        time.sleep(delayInterval)
except KeyboardInterrupt:
    # CTRL+C exit
    SetColour('000')
Subscribe to Comments for &quot;PingMonitor - Monitor your ping times using LedBorg&quot;