From 50db88150d77899a8dd8cc9f02013b1a6182f82c Mon Sep 17 00:00:00 2001 From: Bernd Reuther Date: Wed, 3 Mar 2021 17:35:50 +0100 Subject: [PATCH] flask gpio test --- kamera_server.py | 18 ++++++++++++++---- src/__init__.py | 0 src/pins.py | 25 +++++++++++++++++++++++++ templates/main.html | 8 ++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/__init__.py create mode 100644 src/pins.py diff --git a/kamera_server.py b/kamera_server.py index c868ffc..88f82fe 100644 --- a/kamera_server.py +++ b/kamera_server.py @@ -1,5 +1,6 @@ import RPi.GPIO as GPIO from flask import Flask, render_template, request +from src.pins import pins_out, pins_in, pins_pwm kamera_server = Flask(__name__) @@ -17,14 +18,20 @@ for pin in pins: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, GPIO.HIGH) +for pin in pins_in: + GPIO.setup(pin.nr, GPIO.IN) + @kamera_server.route("/") def main(): # For each pin, read the pin state and store it in the pins dictionary: - for pin in pins: - pins[pin]['state'] = GPIO.input(pin) + for pin in pins: + pins[pin]['state'] = GPIO.input(pin) + for pin in pins_in: + pins[pin]['state'] = GPIO.input(pin) # Put the pin dictionary into the template data dictionary: templateData = { - 'pins' : pins + 'pins' : pins, + 'pins_in' : pins_in } # Pass the template data into the template main.html and return it to the user return render_template('main.html', **templateData) @@ -49,10 +56,13 @@ def action(changePin, action): # For each pin, read the pin state and store it in the pins dictionary: for pin in pins: pins[pin]['state'] = GPIO.input(pin) + for pin in pins_in: + pins[pin]['state'] = GPIO.input(pin) # Along with the pin dictionary, put the message into the template data dictionary: templateData = { - 'pins' : pins + 'pins' : pins, + 'pins_in' : pins_in } return render_template('main.html', **templateData) diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/pins.py b/src/pins.py new file mode 100644 index 0000000..bd8078b --- /dev/null +++ b/src/pins.py @@ -0,0 +1,25 @@ +pins_out = { + 'kamera' : 2, + 'encoder' : 3 +} + +pins_in = { + 'endschalter1' : {'nr' : 23, 'name' : 'Endschalter, Motor 1', 'state' : GPIO.HIGH}, + 'endschalter2' : {'nr' : 26, 'name' : 'Endschalter, Motor 2', 'state' : GPIO.HIGH}, + 'endschalter3' : {'nr' : 25, 'name' : 'Endschalter, Motor 2', 'state' : GPIO.HIGH} + } + +pins_pwm = { + 'm1_a1' : 17, + 'm1_b1' : 18, + 'm1_a2' : 27, + 'm1_b2' : 22, + 'm2_a1' : 19, + 'm2_b1' : 13, + 'm2_a2' : 6, + 'm2_b2' : 5, + 'm3_a1' : 16, + 'm3_b1' : 12, + 'm3_a2' : 21, + 'm3_b2' : 20, + } \ No newline at end of file diff --git a/templates/main.html b/templates/main.html index ee87d0b..fcc2847 100644 --- a/templates/main.html +++ b/templates/main.html @@ -19,5 +19,13 @@ Turn on {% endif %} {% endfor %} + {% for pin in pins_in %} +

{{ pins_in[pin].name }} + {% if pins_in[pin].state == true %} + ist gerade an

+ {% else %} + ist gerade aus
+ {% endif %} + {% endfor %}