streaming-kamera/kamera_server.py

78 lines
2.6 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-03 21:43:50 +01:00
from src.pins import pins_out, pins_in, pins_pwm, freq
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
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Create a dictionary called pins to store the pin number, name, and pin state:
pins = {
2 : {'name' : 'GPIO 2', 'state' : GPIO.HIGH},
3 : {'name' : 'GPIO 3', 'state' : GPIO.HIGH}
}
# 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-03 21:42:00 +01:00
for key, value in pins_pwm.items():
value['pwm'].start(0)
value['pwm'].ChangeFrequency(freq)
value['pwm'].ChangeDutyCycle(0)
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:
changePin = int(changePin)
# Get the device name for the pin being changed:
deviceName = pins[changePin]['name']
# 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
if __name__ == "__main__":
kamera_server.run(host='0.0.0.0')