commit 1b5b6131759a4e3cf77945e328b4cdc2cd16e24b Author: Xavier Date: Sat Sep 3 06:36:20 2022 +0200 first commit diff --git a/README.md b/README.md new file mode 100755 index 0000000..73588b2 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ + Telecommande Domotique +=========================== + +## Pre-requis + +sudo apt-get install rpi.gpio + +ou en manuel + +wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.11.tar.gz +tar -xvf RPi.GPIO-0.5.11.tar.gz +cd RPi.GPIO-0.5.11 +sudo python setup.py install + +## 1) Installation + +dans /home/pi +des fichiers index.html gohttp.py et relay.py et favicon.ico + +## 2) demarrage auto + +editer dans /etc +le rc.local +ajouter en fin avant exit + +#gestion Relay a 0 +./home/pi/relay.py power 1 +gohttp.sh + +faire un reboot + diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000..c1b264e Binary files /dev/null and b/favicon.ico differ diff --git a/gohttp.py b/gohttp.py new file mode 100755 index 0000000..6c33d61 --- /dev/null +++ b/gohttp.py @@ -0,0 +1,203 @@ +#!/usr/bin/env python3 +""" +Very simple HTTP server in python for logging requests +Usage:: + ./server.py [] +""" +import RPi.GPIO as GPIO +import http.server +import socketserver +import logging +import sys +import os +import subprocess +import cgi +import time +GPIO.setwarnings(False) +os.system('pkill -9 httpd') +GPIO.setmode(GPIO.BCM) +pinList = [2, 3, 4, 17] +SleepTimeL=10 + +class S(http.server.SimpleHTTPRequestHandler): + + def do_GET(self): + + GPIO.setup(pinList[0], GPIO.OUT) + status0=int(GPIO.input(pinList[0])) + GPIO.setup(pinList[1], GPIO.OUT) + status1=int(GPIO.input(pinList[1])) + GPIO.setup(pinList[2], GPIO.OUT) + status2=int(GPIO.input(pinList[2])) + GPIO.setup(pinList[3], GPIO.OUT) + status3=int(GPIO.input(pinList[3])) + #logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers)) + + self._set_response() + pass + + def do_POST(self): + content_length = int(self.headers['Content-Length']) # <--- Gets the size of data + post_data = self.rfile.read(content_length) # <--- Gets the data itself + logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n", + str(self.path), str(self.headers), post_data.decode('utf-8')) + + self._set_response() + self.wfile.write("POST request for {}".format(self.path).encode('utf-8')) + + def _set_response(self): + #time.sleep(5) + GPIO.setup(pinList[0], GPIO.OUT) + status0=int(GPIO.input(pinList[0])) + GPIO.setup(pinList[1], GPIO.OUT) + status1=int(GPIO.input(pinList[1])) + GPIO.setup(pinList[2], GPIO.OUT) + status2=int(GPIO.input(pinList[2])) + GPIO.setup(pinList[3], GPIO.OUT) + status3=int(GPIO.input(pinList[3])) + + objParsedPath = str(format(self.path).encode('utf-8')) + print ('objParsedPath="'+objParsedPath+'"') + + if objParsedPath == "b'/R0'": + print ('R0 run') + if status0 == 1: + os.system('./relay.py open 0') + else: + os.system('./relay.py close 0') + time.sleep(int(SleepTimeL)); + os.system('./relay.py open 0') + + if objParsedPath == "b'/R0O'": + print ('R0 run0') + if status0 == 1: + os.system('./relay.py open 0') + else: + os.system('./relay.py close 0') + + + + if objParsedPath == "b'/R1'": + print ('R1 run') + if status1 == 1: + os.system('./relay.py open 1') + else: + os.system('./relay.py close 1') + time.sleep(int(SleepTimeL)); + os.system('./relay.py open 1') + + if objParsedPath == "b'/R1O'": + print ('R1 run0') + if status1 == 1: + os.system('./relay.py open 1') + else: + os.system('./relay.py close 1') + + + if objParsedPath == "b'/R2'": + print ('R2 run') + if status2 == 1: + os.system('./relay.py open 2') + else: + os.system('./relay.py close 2') + time.sleep(int(SleepTimeL)); + os.system('./relay.py open 2') + + if objParsedPath == "b'/R2O'": + print ('R2 run0') + if status2 == 1: + os.system('./relay.py open 2') + else: + os.system('./relay.py close 2') + + + if objParsedPath == "b'/R3'": + print ('R3 run') + if status3 == 1: + os.system('./relay.py open 3') + else: + os.system('./relay.py close 3') + time.sleep(int(SleepTimeL)); + os.system('./relay.py open 3') + + if objParsedPath == "b'/R3O'": + print ('R3 run0') + if status3 == 1: + os.system('./relay.py open 3') + else: + os.system('./relay.py close 3') + + GPIO.setup(pinList[0], GPIO.OUT) + status0=int(GPIO.input(pinList[0])) + GPIO.setup(pinList[1], GPIO.OUT) + status1=int(GPIO.input(pinList[1])) + GPIO.setup(pinList[2], GPIO.OUT) + status2=int(GPIO.input(pinList[2])) + GPIO.setup(pinList[3], GPIO.OUT) + status3=int(GPIO.input(pinList[3])) + + + + file = read_html_template("index.html") + self.send_response(200, "OK") + self.end_headers() + print ('Relay0 = '+str(status0)) + if status0 == 0: + file = file.replace("CHECKED1", "checked") + else: + file = file.replace("CHECKED1", "") + + print ('Relay1 = '+str(status1)) + if status1 == 0: + file = file.replace("CHECKED2", "checked") + else: + file = file.replace("CHECKED2", "") + + print ('Relay2 = '+str(status2)) + if status2 == 0: + file = file.replace("CHECKED3", "checked") + else: + file = file.replace("CHECKED3", "") + + print ('Relay3 = '+str(status3)) + if status3 == 0: + file = file.replace("CHECKED4", "checked") + else: + file = file.replace("CHECKED4", "") + + self.wfile.write(bytes(file.encode("utf-8"))) + + + + +def read_html_template(path): + """function to read HTML file""" + try: + with open(path) as f: + file = f.read() + except Exception as e: + file = e + return file + +def run(handler_class=S, port=8000): + + logging.basicConfig(level=logging.INFO) + server_address = ('', port) + #httpd = server_class(server_address, handler_class) + logging.info('Starting httpd '+str(port)+' ...\n') + try: + Handler = http.server.SimpleHTTPRequestHandler + with socketserver.TCPServer(server_address, handler_class) as httpd: + httpd.serve_forever() + except KeyboardInterrupt: + pass + httpd.server_close() + logging.info('Stopping httpd...\n') + +if __name__ == '__main__': + from sys import argv + + if len(argv) == 2: + run(port=int(argv[1])) + else: + run() diff --git a/img/funduino_keyes_8_prises.jpg b/img/funduino_keyes_8_prises.jpg new file mode 100644 index 0000000..7474a6c Binary files /dev/null and b/img/funduino_keyes_8_prises.jpg differ diff --git a/img/relais.jpg b/img/relais.jpg new file mode 100644 index 0000000..8c1bca8 Binary files /dev/null and b/img/relais.jpg differ diff --git a/img/schema_funduino_keyes.jpg b/img/schema_funduino_keyes.jpg new file mode 100644 index 0000000..13a5e54 Binary files /dev/null and b/img/schema_funduino_keyes.jpg differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..98d43df --- /dev/null +++ b/index.html @@ -0,0 +1,180 @@ + + + + + + + + RelayCMD + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/relay.py b/relay.py new file mode 100755 index 0000000..434e3b3 --- /dev/null +++ b/relay.py @@ -0,0 +1,135 @@ +#!/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()
RELAY COMMAND

Nom RELAY

RESET Tempo10s

ON/OFF

Relay 1 [HamNet] +
+ + +
+
+
+ + +
+
Relay 2 [Srv/RadioSonde/ADSB] +
+ + + +
+
+
+ + + +
+
Relay 3 [HotSpot] +
+ + + +
+
+
+ + + +
+
Relay 4 [Alim] +
+ + + +
+
+
+ + + +
+