streaming-kamera/kamera_server.py

89 lines
2.9 KiB
Python
Raw Normal View History

2021-03-03 15:06:33 +01:00
import RPi.GPIO as GPIO
from flask import Flask, render_template, request
2021-03-04 10:26:23 +01:00
from src.einstellungen import *
from src.schrittmotor import motor
2021-03-03 15:06:33 +01:00
2021-03-03 14:07:15 +01:00
kamera_server = Flask(__name__)
2021-03-03 15:06:33 +01:00
# Set each pin as an output and make it low:
2021-03-03 21:42:00 +01:00
for pin in pins_out:
GPIO.setup(pins_out[pin]['nr'], GPIO.OUT)
GPIO.output(pins_out[pin]['nr'], GPIO.HIGH)
2021-03-03 15:06:33 +01:00
2021-03-03 17:35:50 +01:00
for pin in pins_in:
2021-03-03 20:58:08 +01:00
GPIO.setup(pins_in[pin]['nr'], GPIO.IN)
2021-03-03 17:35:50 +01:00
2021-03-04 10:58:44 +01:00
GPIO.output(pins_out['kamera']['nr'], GPIO.HIGH)
2021-03-04 14:04:25 +01:00
GPIO.setup(20, GPIO.OUT)
m3_pwm4 = GPIO.PWM(20, freq)
m3_pwm4.start(0)
2021-03-04 13:42:57 +01:00
m1 = motor('drehen', m1_a1, m1_b1, m1_a2, m1_b2, m1_t1, m1_t2, m1_te, m1_es, 0, 180)
m2 = motor('kippen', m2_a1, m2_b1, m2_a2, m2_b2, m2_t1, m2_t2, m2_te, m2_es, 1, 90)
2021-03-04 12:22:29 +01:00
2021-03-04 14:04:25 +01:00
print(dir(m2.pwm4))
print(dir(m3_pwm4))
2021-03-04 14:01:13 +01:00
2021-03-04 13:42:57 +01:00
#m1.schritte_max = 180
#m2.schritte_max = 90
2021-03-03 21:42:00 +01:00
2021-03-03 14:07:15 +01:00
@kamera_server.route("/")
2021-03-03 15:06:33 +01:00
def main():
2021-03-03 20:35:28 +01:00
# For each pin, read the pin state and store it in the pins dictionary:
2021-03-03 21:42:00 +01:00
for pin in pins_out:
2021-03-03 21:46:06 +01:00
pins_out[pin]['state'] = GPIO.input(pins_out[pin]['nr'])
2021-03-03 17:35:50 +01:00
for pin in pins_in:
2021-03-03 21:01:37 +01:00
pins_in[pin]['state'] = GPIO.input(pins_in[pin]['nr'])
2021-03-03 20:35:28 +01:00
# Put the pin dictionary into the template data dictionary:
templateData = {
2021-03-03 21:46:06 +01:00
'pins' : pins_out,
2021-03-03 20:35:28 +01:00
'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)
2021-03-03 15:06:33 +01:00
# The function below is executed when someone requests a URL with the pin number and action in it:
@kamera_server.route("/<changePin>/<action>")
def action(changePin, action):
2021-03-03 20:35:28 +01:00
# Convert the pin from the URL into an integer:
2021-03-04 10:35:24 +01:00
2021-03-03 20:35:28 +01:00
# Get the device name for the pin being changed:
2021-03-04 10:29:09 +01:00
deviceName = pins_out[changePin]['name']
2021-03-04 10:35:24 +01:00
changePin = pins_out[changePin]['nr']
2021-03-03 20:35:28 +01:00
# If the action part of the URL is "on," execute the code indented below:
if action == "on":
# Set the pin high:
GPIO.output(changePin, GPIO.HIGH)
# Save the status message to be passed into the template:
message = "Turned " + deviceName + " on."
if action == "off":
GPIO.output(changePin, GPIO.LOW)
message = "Turned " + deviceName + " off."
2021-03-03 15:06:33 +01:00
2021-03-03 20:35:28 +01:00
# For each pin, read the pin state and store it in the pins dictionary:
2021-03-03 21:42:00 +01:00
for pin in pins_out:
pins_out[pin]['state'] = GPIO.input(pins_out[pin]['nr'])
2021-03-03 17:35:50 +01:00
for pin in pins_in:
2021-03-03 21:01:37 +01:00
pins_in[pin]['state'] = GPIO.input(pins_in[pin]['nr'])
2021-03-03 15:06:33 +01:00
2021-03-03 20:35:28 +01:00
# Along with the pin dictionary, put the message into the template data dictionary:
templateData = {
2021-03-03 21:46:06 +01:00
'pins' : pins_out,
2021-03-03 20:35:28 +01:00
'pins_in' : pins_in
}
2021-03-03 15:06:33 +01:00
2021-03-03 20:35:28 +01:00
return render_template('main.html', **templateData)
2021-03-03 14:07:15 +01:00
2021-03-04 12:11:56 +01:00
@kamera_server.route("/motor1_test")
2021-03-04 12:13:38 +01:00
def motor_test():
2021-03-04 13:42:57 +01:00
#m2.pwm4.ChangeFrequency(freq_halten)
2021-03-04 12:39:28 +01:00
m2.vorwaerts(10)
2021-03-04 12:19:36 +01:00
#print('Motortest')
2021-03-04 12:11:56 +01:00
# Pass the template data into the template main.html and return it to the user
2021-03-04 12:13:38 +01:00
return 'fertig'
2021-03-04 12:11:56 +01:00
2021-03-03 14:07:15 +01:00
if __name__ == "__main__":
kamera_server.run(host='0.0.0.0')