PingMonitor with wiringpi (new drivers)

Forums:

Hi,

has anyone rewritten the PingMonitor-Script (https://www.piborg.org/pingmonitor) to work with wiringpi ?

Thanks in advance!

ok, I've tried it myself and this is what I could come up with. It's working for me, when wiringpi2 is installed:

#!/usr/bin/env python
# coding: Latin-1
 
# Load the libraries we need
import time
import os
import wiringpi2 as wiringpi
wiringpi.wiringPiSetup()

# Setup software PWMs on the GPIO pins
PIN_RED = 0
PIN_GREEN = 2
PIN_BLUE = 3
LED_MAX = 100
wiringpi.softPwmCreate(PIN_RED,   0, LED_MAX)
wiringpi.softPwmCreate(PIN_GREEN, 0, LED_MAX)
wiringpi.softPwmCreate(PIN_BLUE,  0, LED_MAX)
wiringpi.softPwmWrite(PIN_RED,   0)
wiringpi.softPwmWrite(PIN_GREEN, 0)
wiringpi.softPwmWrite(PIN_BLUE,  0)

# I'd like to apologize to all python coders for the following lines...
# function to set colors working with wiringpi2 /driverless approach
def SetColour(colourcode):
   red, green, blue = str(colourcode)
   red_calculated = int(red) / 2
   green_calculated = int(green) / 2
   blue_calculated = int(blue) / 2 

   
   wiringpi.softPwmWrite(PIN_RED, red_calculated * LED_MAX)
   wiringpi.softPwmWrite(PIN_GREEN, green_calculated * LED_MAX)
   wiringpi.softPwmWrite(PIN_BLUE, blue_calculated * LED_MAX)

# function used for old drivers, no longer needed
# 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')

piborg's picture

Glad to see you managed to modify the script so it runs again.

Thanks for sharing :)

Subscribe to Comments for &quot;PingMonitor with wiringpi (new drivers)&quot;