projekt:python_fastapi
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| projekt:python_fastapi [2026/02/20 18:53] – torsten.roehl | projekt:python_fastapi [2026/02/23 07:26] (aktuell) – [Systemd] torsten.roehl | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ====== | + | ====== Python FASTAPI |
| [[raspberry_pi: | [[raspberry_pi: | ||
| - | ===== Ziel ===== | + | //In diesem Projekt wird auf dem Raspberry Pi eine Weboberfläche mit FastAPI erstellt, über die eine LED-Ampel geschaltet und die Temperatur eines DS18B20 angezeigt werden kann. Die Anwendung ist im lokalen Netzwerk erreichbar, sodass LEDs und Temperatursensor bequem über einen Webbrowser im LAN gesteuert und überwacht werden können.// |
| - | * Klare Trennung: Hardware / Web / HTML | + | |{{ : |
| - | * Keine Template-Engine | + | |Die LED-Ampel kann nun über den Webbrowser gesteuert und die Temperatur ausgelesen werden.| |
| - | * HTML als eigene Datei | + | ====== Überblick ====== |
| - | * LED + DS18B20 Temperatur | + | * Voraussetzungen |
| - | * Apache bleibt Hauptserver | + | * Software |
| - | * Reboot-fest über systemd | + | * Konfiguration |
| - | * VENV: ~/ | + | |
| + | ====== Details ====== | ||
| - | ===== 1. Projektordner | + | ===== Voraussetzungen |
| + | |||
| + | ==== ENV ==== | ||
| + | |||
| + | <note important> | ||
| + | **Aktivierung der Python-Environment: | ||
| + | |||
| + | Alle weiteren Schritte erfolgen mit der aktivierten Python-Umgebung. | ||
| + | |||
| + | < | ||
| + | source ~/ | ||
| + | </ | ||
| + | |||
| + | </ | ||
| + | Anschließend werden FastAPI und Uvicorn installiert: | ||
| <code bash> | <code bash> | ||
| - | cd / | + | pip install fastapi uvicorn |
| - | mkdir -p python_web/ | + | |
| - | mkdir -p python_web/ | + | |
| - | cd python_web/ | + | |
| - | touch __init__.py | + | |
| </ | </ | ||
| + | < | ||
| + | **FastAPI / Uvicorn** | ||
| + | * **FastAPI** | ||
| + | * stellt das Web-Framework bereit, mit dem die Webseiten und Routen programmiert werden. | ||
| + | * **Uvicorn** | ||
| + | * startet die Anwendung und sorgt dafür, dass sie im Browser erreichbar ist. | ||
| + | </ | ||
| - | ===== 2. VENV ===== | + | ==== Projektstruktur |
| + | '' | ||
| + | < | ||
| + | course_web/ | ||
| + | └── src/ | ||
| + | ├── app.py | ||
| + | ├── core/ | ||
| + | │ | ||
| + | │ | ||
| + | └── html/ | ||
| + | ├── led.html | ||
| + | └── temp.html | ||
| + | |||
| + | </ | ||
| + | ==== Apache2 Startseite ==== | ||
| + | Im Verzeichnis des Apache2-Webservers (''/ | ||
| + | Beim Aufruf der **IP-Adresse des Raspberry Pi im Browser** wird diese Startseite geladen, über die anschließend das gewünschte Projekt ausgewählt werden kann. | ||
| + | |||
| + | <note tip> **Tip** | ||
| + | |||
| + | Bevor die neue '' | ||
| <code bash> | <code bash> | ||
| - | source ~/devel/projects/course_env/bin/activate | + | sudo mv /var/www/html/index.html / |
| - | pip install fastapi uvicorn RPi.GPIO | + | |
| - | which uvicorn | + | |
| </ | </ | ||
| + | </ | ||
| - | Erwartet: | + | <code html /var/www/html/index.html> |
| - | | + | < |
| + | < | ||
| + | < | ||
| + | < | ||
| + | </head> | ||
| + | < | ||
| + | < | ||
| - | ===== 3. Hardware-Schicht ===== | + | < |
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ul> | ||
| - | Datei: | + | </ |
| - | /home/pi/ | + | </html> |
| + | </code> | ||
| + | ==== Hardware ==== | ||
| + | Die Hardware, also die LED-Ampel und der Temperatursensor, | ||
| - | <code python> | + | ===== Software ===== |
| + | Im folgenden Abschnitt werden die für die Webanwendung benötigten Python- und HTML-Dateien vorgestellt. Dazu gehören die Hardware-Anbindung über GPIO und den Temperatursensor, | ||
| + | ==== API ==== | ||
| + | |||
| + | <code python | ||
| import RPi.GPIO as GPIO | import RPi.GPIO as GPIO | ||
| import glob | import glob | ||
| import time | import time | ||
| - | |||
| # ----------------------------- | # ----------------------------- | ||
| Zeile 58: | Zeile 110: | ||
| _initialized = False | _initialized = False | ||
| - | |||
| def init(): | def init(): | ||
| Zeile 76: | Zeile 127: | ||
| def setLED(pin, value): | def setLED(pin, value): | ||
| - | | + | GPIO.output(pin, |
| - | | + | |
| - | | + | |
| - | GPIO.output(pin, | + | |
| Zeile 95: | Zeile 143: | ||
| def status(): | def status(): | ||
| - | | + | |
| - | y = GPIO.input(PIN_Y) | + | int(GPIO.input(PIN_R)), |
| - | g = GPIO.input(PIN_G) | + | int(GPIO.input(PIN_Y)), |
| - | return r, y, g | + | int(GPIO.input(PIN_G)), |
| - | + | ) | |
| - | + | ||
| - | def cleanup(): | + | |
| - | | + | |
| Zeile 109: | Zeile 154: | ||
| # ----------------------------- | # ----------------------------- | ||
| - | SENSOR_TIMEOUT = 1 # Sekunden | + | SENSOR_TIMEOUT = 1 |
| - | + | ||
| - | + | ||
| - | def is_sensor(): | + | |
| - | sensors = glob.glob("/ | + | |
| - | return len(sensors) > 0 | + | |
| def get_sensor(): | def get_sensor(): | ||
| Zeile 149: | Zeile 188: | ||
| - | ===== 4. HTML ===== | + | ==== HTML ==== |
| - | Datei: | + | === LED === |
| - | / | + | |
| - | <code html> | + | |
| + | < | ||
| < | < | ||
| < | < | ||
| < | < | ||
| - | < | + | < |
| </ | </ | ||
| < | < | ||
| - | < | + | < |
| - | < | + | < |
| - | <a href="/web/ | + | <p>Status: {{R}}</p> |
| - | <a href="/web/ | + | <a href="/ |
| + | <a href="/ | ||
| - | < | + | < |
| - | <a href="/web/ | + | <p>Status: {{Y}}</p> |
| - | <a href="/web/ | + | <a href="/ |
| + | <a href="/ | ||
| - | < | + | < |
| - | <a href="/web/ | + | <p>Status: {{G}}</p> |
| - | <a href="/web/ | + | <a href="/ |
| + | <a href="/ | ||
| - | <hr> | + | <br>< |
| + | <a href="/"> | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Temperature === | ||
| + | |||
| + | |||
| + | <code html / | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | <body> | ||
| < | < | ||
| + | |||
| < | < | ||
| + | |||
| + | <br> | ||
| + | <a href="/"> | ||
| </ | </ | ||
| Zeile 186: | Zeile 249: | ||
| - | ===== 5. Web-Schicht ===== | + | ==== FASTAPI APP ==== |
| + | |||
| + | |||
| + | <code python / | ||
| - | Datei: | ||
| - | / | ||
| - | <code python> | ||
| from fastapi import FastAPI, HTTPException | from fastapi import FastAPI, HTTPException | ||
| from fastapi.responses import HTMLResponse, | from fastapi.responses import HTMLResponse, | ||
| from core import hardware | from core import hardware | ||
| - | app = FastAPI(root_path="/ | + | app = FastAPI() |
| Zeile 204: | Zeile 267: | ||
| - | def load_html(r, y, g, t): | + | def load_template(name, replacements): |
| - | with open("static/index.html", " | + | |
| - | html = f.read() | + | |
| + | html = f.read() | ||
| + | except FileNotFoundError: | ||
| + | raise HTTPException(status_code=500, | ||
| - | | + | |
| - | html = html.replace(" | + | html = html.replace(key, str(value)) |
| - | html = html.replace(" | + | |
| - | + | ||
| - | if t is None: | + | |
| - | html = html.replace(" | + | |
| - | else: | + | |
| - | html = html.replace(" | + | |
| return html | return html | ||
| - | + | @app.get("/ | |
| - | @app.get("/", | + | def led_page(): |
| - | def index(): | + | |
| r, y, g = hardware.status() | r, y, g = hardware.status() | ||
| - | t = hardware.get_temperature() | ||
| - | return HTMLResponse(load_html(r, | ||
| + | return HTMLResponse( | ||
| + | load_template(" | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }) | ||
| + | ) | ||
| @app.get("/ | @app.get("/ | ||
| Zeile 242: | Zeile 309: | ||
| raise HTTPException(status_code=400) | raise HTTPException(status_code=400) | ||
| - | return RedirectResponse(url="/ | + | return RedirectResponse(url="/ |
| + | |||
| + | |||
| + | @app.get("/ | ||
| + | def temp_page(): | ||
| + | t = hardware.get_temperature() | ||
| + | |||
| + | if t is None: | ||
| + | value = " | ||
| + | else: | ||
| + | value = f" | ||
| + | |||
| + | return HTMLResponse( | ||
| + | load_template(" | ||
| + | " | ||
| + | }) | ||
| + | | ||
| </ | </ | ||
| - | ===== 6. Apache ===== | ||
| - | In: | ||
| - | / | ||
| - | < | + | ===== Konfiguration ===== |
| + | In diesem Abschnitt wird die Einbindung der **FastAPI-Anwendung** in den Apache-Webserver sowie die Einrichtung als '' | ||
| + | ==== Apache Proxy ==== | ||
| + | |||
| + | === Konfiguration === | ||
| + | |||
| + | In der Datei '' | ||
| + | innerhalb von ''< | ||
| + | |||
| + | |||
| + | <note tip> **Tip** | ||
| + | |||
| + | Bevor die Datei ''/ | ||
| + | sollte die vorhandene Konfiguration gesichert werden. | ||
| + | |||
| + | <code bash> | ||
| + | cd / | ||
| + | sudo cp 000-default.conf 000-default.conf.course_backup | ||
| + | </ | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | < | ||
| ProxyPreserveHost On | ProxyPreserveHost On | ||
| - | ProxyPass | + | |
| - | ProxyPassReverse /web | + | ProxyPass |
| + | ProxyPassReverse /led | ||
| + | |||
| + | ProxyPass | ||
| + | ProxyPassReverse /temp | ||
| </ | </ | ||
| + | |||
| + | === Aktivieren === | ||
| + | Damit die Weiterleitung an die FastAPI-Anwendung funktioniert, | ||
| <code bash> | <code bash> | ||
| sudo a2enmod proxy | sudo a2enmod proxy | ||
| sudo a2enmod proxy_http | sudo a2enmod proxy_http | ||
| - | sudo systemctl | + | sudo systemctl |
| </ | </ | ||
| + | ==== Systemd ==== | ||
| - | ===== 7. systemd ===== | + | Die Service-Datei muss unter ''/ |
| + | === Service | ||
| - | Datei: | ||
| - | / | ||
| - | < | + | < |
| [Unit] | [Unit] | ||
| Description=Python Web FastAPI | Description=Python Web FastAPI | ||
| Zeile 277: | Zeile 387: | ||
| [Service] | [Service] | ||
| User=pi | User=pi | ||
| - | WorkingDirectory=/ | + | WorkingDirectory=/ |
| ExecStart=/ | ExecStart=/ | ||
| Restart=always | Restart=always | ||
| Zeile 284: | Zeile 394: | ||
| WantedBy=multi-user.target | WantedBy=multi-user.target | ||
| </ | </ | ||
| + | |||
| + | === Registrierung === | ||
| + | Damit die neu erstellte Service-Datei von '' | ||
| <code bash> | <code bash> | ||
| sudo systemctl daemon-reload | sudo systemctl daemon-reload | ||
| - | sudo systemctl enable | + | sudo systemctl enable |
| - | sudo systemctl start python_web | + | sudo systemctl start course_web |
| </ | </ | ||
| - | + | Nützlich: | |
| - | ===== 8. Test ===== | + | |
| - | + | ||
| - | * http:// | + | |
| - | * http:// | + | |
| - | + | ||
| - | ===== 9. Reboot ===== | + | |
| <code bash> | <code bash> | ||
| - | sudo reboot | + | sudo systemctl restart course_web |
| + | sudo systemctl status course_web | ||
| </ | </ | ||
| - | |||
| - | Reboot-fest. | ||
| - | Hardware wird einmal initialisiert. | ||
| - | Struktur entspricht den ursprünglichen Prinzipien. | ||
| - | |||
| - | |||
projekt/python_fastapi.1771613595.txt.gz · Zuletzt geändert: von torsten.roehl
