streaming-kamera/kamera_server.py

106 lines
3.3 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-05 15:09:21 +01:00
import time
import pigpio
pi = pigpio.pi()
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:
2021-03-05 15:09:21 +01:00
pi.set_mode(pins_out[pin]['nr'], pigpio.OUTPUT)
pi.write(pins_out[pin]['nr'], 1)
#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-05 15:09:21 +01:00
pi.set_mode(pins_out[pin]['nr'], pigpio.INPUT)
#GPIO.setup(pins_in[pin]['nr'], GPIO.IN)
2021-03-04 10:58:44 +01:00
2021-03-05 15:09:21 +01:00
#GPIO.output(pins_out['kamera']['nr'], GPIO.LOW)
pi.write(pins_out['kamera']['nr'], 0)
2021-03-04 14:04:25 +01:00
2021-03-04 16:09:32 +01:00
m1 = motor('drehen', m1_a1, m1_b1, m1_a2, m1_b2, m1_t1, m1_t2, m1_te, m1_es, 1, 180)
2021-03-04 13:42:57 +01:00
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:38:51 +01:00
m1.start()
m2.start()
2021-03-04 15:28:43 +01:00
m1.eichen()
2021-03-04 15:40:40 +01:00
m2.eichen()
2021-03-04 15:28:43 +01:00
2021-03-04 16:20:11 +01:00
m1.vorwaerts(100)
2021-03-04 14:46:43 +01:00
#m1.stop()
#m2.stop()
2021-03-04 14:43:04 +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-05 15:09:21 +01:00
#pins_out[pin]['state'] = GPIO.input(pins_out[pin]['nr'])
pins_out[pin]['state'] = pi.read(pins_out[pin]['nr'])
2021-03-03 17:35:50 +01:00
for pin in pins_in:
2021-03-05 15:09:21 +01:00
#pins_in[pin]['state'] = GPIO.input(pins_in[pin]['nr'])
pins_in[pin]['state'] = pi.read(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:
2021-03-05 15:09:21 +01:00
#GPIO.output(changePin, GPIO.HIGH)
pi.write(changePin, 1)
2021-03-03 20:35:28 +01:00
# Save the status message to be passed into the template:
message = "Turned " + deviceName + " on."
if action == "off":
2021-03-05 15:09:21 +01:00
#GPIO.output(changePin, GPIO.LOW)
pi.write(changePin, GPIO.LOW)
2021-03-03 20:35:28 +01:00
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:
2021-03-05 15:09:21 +01:00
#pins_out[pin]['state'] = GPIO.input(pins_out[pin]['nr'])
pins_out[pin]['state'] = pi.read(pins_out[pin]['nr'])
2021-03-03 17:35:50 +01:00
for pin in pins_in:
2021-03-05 15:09:21 +01:00
#pins_in[pin]['state'] = GPIO.input(pins_in[pin]['nr'])
pins_in[pin]['state'] = pi.read(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 15:56:42 +01:00
@kamera_server.route("/position/<motor>/<position>")
2021-03-04 15:53:26 +01:00
def motor_test(motor, position):
if motor == "drehen":
2021-03-05 15:09:21 +01:00
#m1.stop()
#m1.start()
2021-03-04 15:53:26 +01:00
m1.gehe_zu(int(position))
2021-03-04 15:58:00 +01:00
return 'Drehen: ' + str(m1.pos)
2021-03-04 12:11:56 +01:00
2021-03-04 16:54:02 +01:00
if __name__ == "__main__":
kamera_server.run(host='0.0.0.0')