Skip to content

Instantly share code, notes, and snippets.

@spynappels
Created November 30, 2017 08:05
Show Gist options
  • Save spynappels/c812d314c605cf9ba107881553f81918 to your computer and use it in GitHub Desktop.
Save spynappels/c812d314c605cf9ba107881553f81918 to your computer and use it in GitHub Desktop.
Simple ping based host monitor with option to disable checks by toggling a push button. Pin numbers to suit Pi Hut StatusBoard.
#!/usr/bin/python3
import RPi.GPIO as GPIO
from time import sleep
import os
# Define Pin Numbers
red1 = 4
green1 = 17
button1 = 14
red2 = 27
green2 = 22
button2 = 19
red3 = 10
green3 = 9
button3 = 15
red4 = 11
green4 = 5
button4 = 26
red5 = 6
green5 = 13
button5 = 18
# Setup GPIO Pins
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(red1,GPIO.OUT)
GPIO.setup(green1,GPIO.OUT)
GPIO.setup(red2,GPIO.OUT)
GPIO.setup(green2,GPIO.OUT)
GPIO.setup(red3,GPIO.OUT)
GPIO.setup(green3,GPIO.OUT)
GPIO.setup(red4,GPIO.OUT)
GPIO.setup(green4,GPIO.OUT)
GPIO.setup(red5,GPIO.OUT)
GPIO.setup(green5,GPIO.OUT)
GPIO.setup(button1,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button2,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button3,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button4,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button5,GPIO.IN, pull_up_down=GPIO.PUD_UP)
for i in (red1,green1,red2,green2,red3,green3,red4,green4,red5,green5):
GPIO.output(i, 0)
# By default enable all checks
en1 = True
en2 = True
en3 = True
en4 = True
en5 = True
# Define buttonpress callback functions
def callback1(channel):
global en1
en1 = not en1
def callback2(channel):
global en2
en2 = not en2
def callback3(channel):
global en3
en3 = not en3
def callback4(channel):
global en4
en4 = not en4
def callback5(channel):
global en5
en5 = not en5
# Configure button press interrupts
GPIO.add_event_detect(button1, GPIO.FALLING, callback=callback1, bouncetime=300)
GPIO.add_event_detect(button2, GPIO.FALLING, callback=callback2, bouncetime=300)
GPIO.add_event_detect(button3, GPIO.FALLING, callback=callback3, bouncetime=300)
GPIO.add_event_detect(button4, GPIO.FALLING, callback=callback4, bouncetime=300)
GPIO.add_event_detect(button5, GPIO.FALLING, callback=callback5, bouncetime=300)
def status_check():
if en1 == True: # Encoder
# Test is a single ping with a time limit of 1 second
retval = os.system("ping -c 1 -w 1 192.168.13.1 > /dev/null 2>&1")
if retval == 0: # ping successful
GPIO.output(green1, 1)
GPIO.output(red1, 0)
else: # ping unsuccessful
GPIO.output(green1, 0)
GPIO.output(red1, 1)
else: # channel is disabled
GPIO.output(green1, 0)
GPIO.output(red1, 0)
if en2 == True: # Bridge Rx Device
retval = os.system("ping -c 1 -w 1 192.168.13.105 > /dev/null 2>&1")
if retval == 0:
GPIO.output(green2, 1)
GPIO.output(red2, 0)
else:
GPIO.output(green2, 0)
GPIO.output(red2, 1)
else:
GPIO.output(green2, 0)
GPIO.output(red2, 0)
if en3 == True: # Player1
retval = os.system("ping -c 1 -w 1 192.168.13.114 > /dev/null 2>&1")
if retval == 0:
GPIO.output(green3, 1)
GPIO.output(red3, 0)
else:
GPIO.output(green3, 0)
GPIO.output(red3, 1)
else:
GPIO.output(green3, 0)
GPIO.output(red3, 0)
if en4 == True: # Player 2
retval = os.system("ping -c 1 -w 1 192.168.13.128 > /dev/null 2>&1")
if retval == 0:
GPIO.output(green4, 1)
GPIO.output(red4, 0)
else:
GPIO.output(green4, 0)
GPIO.output(red4, 1)
else:
GPIO.output(green4, 0)
GPIO.output(red4, 0)
if en5 == True: # Player 3
retval = os.system("ping -c 1 -w 1 192.168.13.129 > /dev/null 2>&1")
if retval == 0:
GPIO.output(green5, 1)
GPIO.output(red5, 0)
else:
GPIO.output(green5, 0)
GPIO.output(red5, 1)
else:
GPIO.output(green5, 0)
GPIO.output(red5, 0)
# Run checks
try:
while True:
status_check()
sleep(10) # Adjust for desired ping interval
except KeyboardInterrupt:
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment