Hallo Basti,
Aktuell gibt es drei hauptsächliche Dateien
Einmal das Python Script welches die jeweiligen Pins steuert und den Status speichert, ob ein Relais gesteuert wurde oder nicht. Das dient zum gleichzeitigen Ausschalten aller Relais mit einem Button "Alles AUS"
# coding=utf-8
import sys
from flask import Flask, render_template, request, redirect, url_for, make_response
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
pin1 =26 #Relais Pins Nummer
pin2 =19
pin3 =13
pin4 =6
pin5 =27
pin6 =17
pin7 =4
pin8 =21
pin9 =20
pin10 =16
pin11 =12
pin12 =5
pin13 =25
pin14 =24
pin15 =23
pin16 =18
pin17 = 0
GPIO.setmode(GPIO.BCM) #GPIO MODE
GPIO.setup(pin1,GPIO.OUT) #Alle Relais Pins als OUTPUT
GPIO.setup(pin2,GPIO.OUT)
GPIO.setup(pin3,GPIO.OUT)
GPIO.setup(pin4,GPIO.OUT)
GPIO.setup(pin5,GPIO.OUT)
GPIO.setup(pin6,GPIO.OUT)
GPIO.setup(pin7,GPIO.OUT)
GPIO.setup(pin8,GPIO.OUT)
GPIO.setup(pin9,GPIO.OUT)
GPIO.setup(pin10,GPIO.OUT)
GPIO.setup(pin11,GPIO.OUT)
GPIO.setup(pin12,GPIO.OUT)
GPIO.setup(pin13,GPIO.OUT)
GPIO.setup(pin14,GPIO.OUT)
GPIO.setup(pin15,GPIO.OUT)
GPIO.setup(pin16,GPIO.OUT)
GPIO.setup(pin17,GPIO.OUT)
GPIO.output(pin1 , 1)
GPIO.output(pin2 , 1)
GPIO.output(pin3 , 1)
GPIO.output(pin4 , 1)
GPIO.output(pin5 , 1)
GPIO.output(pin6 , 1)
GPIO.output(pin7 , 1)
GPIO.output(pin8 , 1)
GPIO.output(pin9 , 1)
GPIO.output(pin10 , 1)
GPIO.output(pin11 , 1)
GPIO.output(pin12 , 1)
GPIO.output(pin13 , 1)
GPIO.output(pin14 , 1)
GPIO.output(pin15 , 1)
GPIO.output(pin16 , 1)
GPIO.output(pin17 , 1)
app = Flask(__name__) #Flask Server einrichten
#Rückgabe der Seite index.html wenn die IP-Adresse ausgewählt ist
states = []
for i in range(1, 17):
states.append(i)
states.insert(i, 0)
@app.route('/')
def index():
return render_template('index.html')
#Jeder HTML-Taster ergibt einen Nummer zurück
@app.route('/<changepin>', methods=['POST'])
def reroute(changepin):
changePin = int(changepin) #In Integer umwandeln
if changePin < 18:
states[changePin] = 1 if states[changePin] == 0 else 0
if changePin == 1:
print ("Relais1 AN") #Relais1
GPIO.output(pin1 ,0)
time.sleep(0.5)
GPIO.output(pin1 ,1)
elif changePin == 2:
print ("Relais2") #Relais2
GPIO.output(pin2 ,0)
time.sleep(0.5)
GPIO.output(pin2, 1)
elif changePin == 3:
print ("Relais3") #Relais3
GPIO.output(pin3 ,0)
time.sleep(0.5)
GPIO.output(pin3, 1)
elif changePin == 4:
print ("Relais4") #Relais4
GPIO.output(pin4 ,0)
time.sleep(0.5)
GPIO.output(pin4, 1)
elif changePin == 5:
print ("Relais5") #Relais5
GPIO.output(pin5 ,0)
time.sleep(0.5)
GPIO.output(pin5, 1)
elif changePin == 6:
print ("Relais6") #Relais6
GPIO.output(pin6 ,0)
time.sleep(0.5)
GPIO.output(pin6, 1)
elif changePin == 7:
print ("Relais7") #Relais7
GPIO.output(pin7 ,0)
time.sleep(0.5)
GPIO.output(pin7, 1)
elif changePin == 8:
print ("Relais8") #Relais8
GPIO.output(pin8 ,0)
time.sleep(0.5)
GPIO.output(pin8, 1)
elif changePin == 9:
print ("Relais9") #Relais9
GPIO.output(pin9 ,0)
time.sleep(0.5)
GPIO.output(pin9, 1)
elif changePin == 10:
print ("Relais10") #Relais10
GPIO.output(pin10 ,0)
time.sleep(0.5)
GPIO.output(pin10, 1)
elif changePin == 11:
print ("Relais11") #Relais11
GPIO.output(pin11 ,0)
time.sleep(0.5)
GPIO.output(pin11, 1)
elif changePin == 12:
print ("Relais12") #Relais12
GPIO.output(pin12 ,0)
time.sleep(0.5)
GPIO.output(pin12, 1)
elif changePin == 13:
print ("Relais13") #Relais13
GPIO.output(pin13 ,0)
time.sleep(0.5)
GPIO.output(pin13, 1)
elif changePin == 14:
print ("Relais14") #Relais14
GPIO.output(pin14 ,0)
time.sleep(0.5)
GPIO.output(pin14, 1)
elif changePin == 15:
print ("Relais15") #Relais15
GPIO.output(pin15 ,0)
time.sleep(0.5)
GPIO.output(pin15, 1)
elif changePin == 16:
print ("Relais16") #Relais16
GPIO.output(pin16 ,0)
time.sleep(0.5)
GPIO.output(pin16, 1)
elif changePin == 18:
print("Alle AUS") #Alle Relais Aus die An sind
for i in range(1, 17):
state = states[i]
if state == 1:
GPIO.output(globals()['pin' + str(i)], 0)
time.sleep(0.2)
GPIO.output(globals()['pin' + str(i)], 1)
states[i] = 0
response = make_response(redirect(url_for('index')))
return(response)
#app.run(debug=True, host='0.0.0.0') #Einrichten des Servers im Debug-Modus
Die Html Seite für die Buttons mit jeweiliger ID zum schalten im Python Script.
Ist zwar eine unschöne Art wie diese Seite aufgebaut ist aber ich bin froh das es damit funktioniert.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<title> Lichtersteuerung</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="../static/styles/mainpage.css">
<link rel="apple-touch-icon" sizes="180x180" href="../static/styles/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="../static/styles/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="../static/styles/favicon-16x16.png">
</head>
<body>
<div id="container">
<form action="/1" method="POST">
<button class="button" type="submit" id="1">Relais 1</button>
</form>
<form action="/2" method="POST">
<button class="button" type="submit" id="2">Relais 2</button>
</form>
<form action="/3" method="POST">
<button class="button" type="submit" id="3">Relais 3</button>
</form>
<form action="/4" method="POST">
<button class="button" type="submit" id="4">Relais 4</button>
</form>
<form action="/5" method="POST">
<button class="button" type="submit" id="5">Relais 5</button>
</form>
<form action="/6" method="POST">
<button class="button" type="submit" id="6">Relais 6</button>
</form>
<form action="/7" method="POST">
<button class="button" type="submit" id="7">Relais 7</button>
</form>
<form action="/8" method="POST">
<button class="button" type="submit" id="8">Relais 8</button>
</form>
<form action="/9" method="POST">
<button class="button" type="submit" id="9">Relais 9</button>
</form>
<form action="/10" method="POST">
<button class="button" type="submit" id="10">Relais 10</button>
</form>
<form action="/11" method="POST">
<button class="button" type="submit" id="11">Relais 11</button>
</form>
<form action="/12" method="POST">
<button class="button" type="submit" id="12">Relais 12</button>
</form>
<form action="/13" method="POST">
<button class="button" type="submit" id="13">Relais 13</button>
</form>
<form action="/14" method="POST">
<button class="button" type="submit" id="14">Relais 14</button>
</form>
<form action="/15" method="POST">
<button class="button" type="submit" id="15">Relais 15</button>
</form>
<form action="/16" method="POST">
<button class="button" type="submit" id="16">Relais 16</button>
</form>
</div>
<div id="containerv">
<form action="/18" method="POST">
<button class="button" type="submit" id="18">Alles AUS</button>
</form>
</body>
</html>