Skip to content

Instantly share code, notes, and snippets.

@wlmeng11
Last active July 27, 2016 23:26
Show Gist options
  • Select an option

  • Save wlmeng11/7b1015f8bf0397405a7eac8052416af9 to your computer and use it in GitHub Desktop.

Select an option

Save wlmeng11/7b1015f8bf0397405a7eac8052416af9 to your computer and use it in GitHub Desktop.
Use a Raspberry Pi 2 to control some relays to reboot servers
#!/usr/bin/env python2
#
# server_rebooter.py
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) # use Broadcom numbering system
GPIO.setwarnings(False) # override any warnings about a pin being in use, comment this out to show those warnings
def relay_pin(relay_name):
""" Returns the BCM pin number corresponding to the specified relay name """
pin_mappings = {
'IN1': 26,
'IN2': 19,
'IN3': 13,
'IN4': 6,
}
return pin_mappings.get(relay_name)
# get input from user to specify relay name
while True:
response = raw_input("Please specify which relay to activate: \nIN1: Storm-Power On\nIN2: Storm-Reset\nIN3: Phenom-Power On\nIN4: Phenom-Reset\n")
if response in ['IN1', 'IN2', 'IN3', 'IN4']:
break
else:
print "Name must from IN1 to IN4."
# activate the specified relay for 0.25 seconds
print "Initializing BCM pin %s to activate relay %s." % (relay_pin(response), response)
GPIO.setup(relay_pin(response), GPIO.OUT) # the specified pin is initialized, usually defaulting to the LOW state
GPIO.output(relay_pin(response), GPIO.LOW) # explicitly set the pin to LOW in case it wasn't already
time.sleep(0.25)
GPIO.output(relay_pin(response), GPIO.HIGH) # return the pin to HIGH to deactivate relay
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment