flask gpio test

This commit is contained in:
Bernd Reuther 2021-03-03 15:06:33 +01:00
parent 7a05e0eede
commit df1c44bca5
7 changed files with 153 additions and 7 deletions

View File

@ -1,9 +1,61 @@
from flask import Flask
import RPi.GPIO as GPIO
from flask import Flask, render_template, request
kamera_server = Flask(__name__)
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:
for pin in pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
@kamera_server.route("/")
def first_function():
return "<html><body><h1 style='color:red'>I am hosted on Raspberry Pi !!!</h1></body></html>"
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)
# Put the pin dictionary into the template data dictionary:
templateData = {
'pins' : pins
}
# Pass the template data into the template main.html and return it to the user
return render_template('main.html', **templateData)
# 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):
# 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."
# For each pin, read the pin state and store it in the pins dictionary:
for pin in pins:
pins[pin]['state'] = GPIO.input(pin)
# Along with the pin dictionary, put the message into the template data dictionary:
templateData = {
'pins' : pins
}
return render_template('main.html', **templateData)
if __name__ == "__main__":
kamera_server.run(host='0.0.0.0')

View File

@ -24,7 +24,7 @@ sudo raspi-config
## Zusatzprogramme installieren
```
sudo apt install mc git zbar-tools build-essential python-dev
sudo apt install mc git zbar-tools build-essential python-dev nginx
```
## Benutzer anlegen
@ -66,6 +66,7 @@ deactivate
```
cd /opt/kamera
git clone https://git.jgz-energie.net/ENERGIE/streaming-kamera.git
```
Außerhalb der virtuellen Umgebung kann das Programm mit `/opt/kamera/bin/python /opt/kamera/streaming-kamera/kamera.py gestartet` werden.
@ -79,17 +80,71 @@ Der uWSGI-Server kann mit `/opt/kamera/bin/uwsgi --socket 0.0.0.0:8000 --protoco
[uwsgi]
chdir = /opt/kamera/streaming-kamera
module = kamera_server:server
module = kamera_server:kamera_server
master = true
processes = 1
threads = 2
uid = kamera
gid = kamera
uid = www-data
gid = www-data
socket = /tmp/sample_app.sock
chmod-socket = 664
vacuum = true
die-on-term = true
```
/etc/systemd/system/uwsgi.service
```
[Unit]
Description=uWSGI Service
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/opt/kamera/streaming-kamera/
ExecStart=/opt/kamera/bin/uwsgi --ini /opt/kamera/uwsgi_config.ini
[Install]
WantedBy=multi-user.target
```
Dienst starten
```
sudo systemctl daemon-reload
sudo systemctl start uwsgi.service
sudo systemctl status uwsgi.service
sudo systemctl enable uwsgi.service
```
### nginx anpassen
```
sudo rm /etc/nginx/sites-enabled/default
```
/etc/nginx/sites-available/kamera_server
```
server {
listen 80;
server_name localhost;
location / { try_files $uri @app; }
location @app {
include uwsgi_params;
uwsgi_pass unix:/tmp/kamera_server.sock;
}
}
```
Konfiguration aktivieren
```
sudo ln -s /etc/nginx/sites-available/kamera_server /etc/nginx/sites-enabled
sudo systemctl restart nginx
```

7
templates/css/bootstrap.min.css vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

7
templates/js/bootstrap.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

23
templates/main.html Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<head>
<title>RPi Web Server</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="/js/bootstrap.min.js"></script>
</head>
<body>
<h1>RPi Web Server</h1>
{% for pin in pins %}
<h2>{{ pins[pin].name }}
{% if pins[pin].state == true %}
ist gerade <strong>an</strong></h2><div class="row"><div class="col-md-2">
<a href="/{{pin}}/off" class="btn btn-block btn-lg btn-default" role="button">Turn off</a></div></div>
{% else %}
ist gerade <strong>aus</strong></h2><div class="row"><div class="col-md-2">
<a href="/{{pin}}/on" class="btn btn-block btn-lg btn-primary" role="button">Turn on</a></div></div>
{% endif %}
{% endfor %}
</body>
</html>