Gestion_Relay/relay.py
2022-09-03 06:36:20 +02:00

136 lines
3.7 KiB
Python
Executable File

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import sys
import re
GPIO.setwarnings(False)
# loop through pins and set mode and state to 'high'
def reset_relay(pinrelayList):
for i in pinrelayList:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.LOW)
#Power is 0 OFF or 1 ON
def power(pinListpower,ONOFF):
j=1
for i in pinListpower:
GPIO.setup(i, GPIO.OUT)
if int(ONOFF) == 0:
print ('Relay='+str(j)+' '+str(ONOFF))
GPIO.output(i, GPIO.HIGH)
if int(ONOFF) == 1:
print ('Relay='+str(j)+' '+str(ONOFF))
GPIO.output(i, GPIO.LOW)
j+=1
#Status RELAY
def status(pinListcheck,pincheck):
j=1
if pincheck==10:
for i in pinListcheck:
GPIO.setup(i, GPIO.OUT)
if int(GPIO.input(i)) == 1:
print ('Relay '+str(j)+' = OFF')
else:
print ('Relay '+str(j)+' = ON')
j+=1
else:
GPIO.setup(pinListcheck[int(pincheck)], GPIO.OUT)
if int(GPIO.input(pinListcheck[int(pincheck)])) == 1:
print ('Relay '+str(pincheck)+' = OFF')
else:
print ('Relay '+str(pincheck)+' = ON')
# init funct
def main(argv=sys.argv):
# init var
GPIO.setmode(GPIO.BCM)
pinList = [2, 3, 4, 17]
SleepTimeL = 5
#GPIO.cleanup()
x = 1
#Cmd HELP
if (len(argv) > x and argv[x] == 'help') or len(argv) == 1:
print ('usage: ./relay.py ....')
print ('options:')
print (' help', 'This aide')
print ('commands:')
print (' close R', 'Close relay number R of 0 to 3')
print (' open R', 'Open relay R of number 0 to 3')
print (' reset R T', 'Reset device, changes ON OFF ON by tempo T.')
print (' status', 'Print the states of all relays.')
print ('example: ./relay.py reset 0 10')
print (' relay0 is ON but make a reset by OFF STEP 10s and ON')
print ('example: ./relay.py open 0')
print ('example: ./relay.py close 1')
print ('example: ./relay.py status')
print ('(c) F4IYT Xavier 2022 v1.00')
GPIO.cleanup()
sys.exit(1)
#Cmd STATUS
if len(argv) > x and argv[x] == 'status':
pinstatus=10
if len(argv) > 2:
x +=1
pinstatus=argv[x]
status(pinList,pinstatus)
#Cmd Close Relay
if len(argv) > x and argv[x] == 'close':
x +=1
relay=argv[x]
if int(relay) < 4:
relayclose=pinList[int(relay)]
print ("Close Relay:"+relay)
GPIO.setup(int(relayclose), GPIO.OUT)
GPIO.output(int(relayclose), GPIO.HIGH)
else:
print ('Error number of Relay check input only 0 at 3!')
#Cmd Open Relay
if len(argv) > x and argv[x] == 'open':
x +=1
relay=argv[x]
if int(relay) < 4:
relayopen=pinList[int(relay)]
print ("Open Relay:"+relay)
GPIO.setup(int(relayopen), GPIO.OUT)
GPIO.output(int(relayopen), GPIO.LOW)
else:
print ('Error number of Relay check input only 0 at 3!')
#Cmd Reset Relay
if len(argv) > x and argv[x] == 'reset':
x +=1
relay=argv[x]
if int(relay) < 4:
relayreset=pinList[int(relay)]
if len(argv) > 3:
x +=1
SleepTimeL=argv[x]
print ("Reset Relay:"+relay+" tempo="+str(SleepTimeL))
time.sleep(int(SleepTimeL));
GPIO.setup(int(relayreset), GPIO.OUT)
GPIO.output(int(relayreset), GPIO.HIGH)
time.sleep(int(SleepTimeL));
GPIO.setup(int(relayreset), GPIO.OUT)
GPIO.output(int(relayreset), GPIO.LOW)
else:
print ('Error number of Relay check input only 0 at 3!')
#Cmd Power Relay
if len(argv) > x and argv[x] == 'power':
x +=1
ponoff=argv[x]
print ("Power = :"+ponoff)
power(pinList,ponoff)
# Main part
if __name__ == '__main__':
main()