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/21 07:31] – [Systemd] torsten.roehl | projekt:python_fastapi [2026/02/23 07:26] (aktuell) – [Systemd] torsten.roehl | ||
|---|---|---|---|
| Zeile 3: | Zeile 3: | ||
| [[raspberry_pi: | [[raspberry_pi: | ||
| + | |||
| + | //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.// | ||
| + | |||
| + | |{{ : | ||
| + | |Die LED-Ampel kann nun über den Webbrowser gesteuert und die Temperatur ausgelesen werden.| | ||
| ====== Überblick ====== | ====== Überblick ====== | ||
| * Voraussetzungen | * Voraussetzungen | ||
| Zeile 13: | Zeile 18: | ||
| ==== ENV ==== | ==== ENV ==== | ||
| - | < | + | |
| + | <note important> | ||
| + | **Aktivierung der Python-Environment: | ||
| + | |||
| + | Alle weiteren Schritte erfolgen mit der aktivierten Python-Umgebung. | ||
| + | |||
| + | < | ||
| source ~/ | source ~/ | ||
| - | pip install fastapi uvicorn RPi.GPIO | ||
| </ | </ | ||
| + | |||
| + | </ | ||
| + | Anschließend werden FastAPI und Uvicorn installiert: | ||
| + | |||
| + | <code bash> | ||
| + | pip install fastapi uvicorn | ||
| + | </ | ||
| + | < | ||
| + | **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. | ||
| + | </ | ||
| ==== Projektstruktur ==== | ==== Projektstruktur ==== | ||
| - | '' | + | '' |
| < | < | ||
| course_web/ | course_web/ | ||
| Zeile 26: | Zeile 51: | ||
| ├── app.py | ├── app.py | ||
| ├── core/ | ├── core/ | ||
| + | │ | ||
| │ | │ | ||
| └── html/ | └── html/ | ||
| Zeile 33: | Zeile 59: | ||
| </ | </ | ||
| ==== Apache2 Startseite ==== | ==== 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. | ||
| - | Datei: | + | <note tip> **Tip** |
| + | |||
| + | Bevor die neue '' | ||
| + | <code bash> | ||
| + | sudo mv / | ||
| + | </ | ||
| + | </ | ||
| - | <code html> | + | < |
| < | < | ||
| < | < | ||
| Zeile 59: | Zeile 93: | ||
| ===== Software ===== | ===== 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 ==== | ==== API ==== | ||
| - | Datei: | + | <code python |
| - | + | ||
| - | <code python> | + | |
| import RPi.GPIO as GPIO | import RPi.GPIO as GPIO | ||
| import glob | import glob | ||
| Zeile 159: | Zeile 191: | ||
| === LED === | === LED === | ||
| - | Datei: | ||
| - | <code html> | + | |
| + | < | ||
| < | < | ||
| < | < | ||
| Zeile 171: | Zeile 203: | ||
| < | < | ||
| - | < | + | < |
| + | <p>Status: {{R}}</p> | ||
| <a href="/ | <a href="/ | ||
| <a href="/ | <a href="/ | ||
| - | < | + | < |
| + | <p>Status: {{Y}}</p> | ||
| <a href="/ | <a href="/ | ||
| <a href="/ | <a href="/ | ||
| - | < | + | < |
| + | <p>Status: {{G}}</p> | ||
| <a href="/ | <a href="/ | ||
| <a href="/ | <a href="/ | ||
| Zeile 193: | Zeile 228: | ||
| === Temperature === | === Temperature === | ||
| - | Datei: | ||
| - | <code html> | + | < |
| < | < | ||
| < | < | ||
| Zeile 217: | Zeile 251: | ||
| ==== FASTAPI APP ==== | ==== FASTAPI APP ==== | ||
| - | Datei: | ||
| - | <code python> | + | <code python |
| + | |||
| from fastapi import FastAPI, HTTPException | from fastapi import FastAPI, HTTPException | ||
| from fastapi.responses import HTMLResponse, | from fastapi.responses import HTMLResponse, | ||
| Zeile 233: | Zeile 268: | ||
| def load_template(name, | def load_template(name, | ||
| - | with open(f" | + | |
| - | html = f.read() | + | |
| + | html = f.read() | ||
| + | except FileNotFoundError: | ||
| + | raise HTTPException(status_code=500, | ||
| for key, value in replacements.items(): | for key, value in replacements.items(): | ||
| Zeile 240: | Zeile 278: | ||
| return html | return html | ||
| - | |||
| @app.get("/ | @app.get("/ | ||
| def led_page(): | def led_page(): | ||
| r, y, g = hardware.status() | r, y, g = hardware.status() | ||
| + | |||
| return HTMLResponse( | return HTMLResponse( | ||
| load_template(" | load_template(" | ||
| " | " | ||
| " | " | ||
| - | " | + | " |
| + | " | ||
| + | " | ||
| + | " | ||
| }) | }) | ||
| ) | ) | ||
| - | |||
| @app.get("/ | @app.get("/ | ||
| Zeile 292: | Zeile 332: | ||
| ===== Konfiguration ===== | ===== Konfiguration ===== | ||
| + | In diesem Abschnitt wird die Einbindung der **FastAPI-Anwendung** in den Apache-Webserver sowie die Einrichtung als '' | ||
| ==== Apache Proxy ==== | ==== 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 | ||
| Zeile 305: | Zeile 364: | ||
| </ | </ | ||
| + | === Aktivieren === | ||
| + | Damit die Weiterleitung an die FastAPI-Anwendung funktioniert, | ||
| + | |||
| + | <code bash> | ||
| + | sudo a2enmod proxy | ||
| + | sudo a2enmod proxy_http | ||
| + | sudo systemctl restart apache2 | ||
| + | </ | ||
| ==== Systemd ==== | ==== Systemd ==== | ||
| + | Die Service-Datei muss unter ''/ | ||
| === Service === | === Service === | ||
| - | Datei: | ||
| - | < | + | |
| + | < | ||
| [Unit] | [Unit] | ||
| Description=Python Web FastAPI | Description=Python Web FastAPI | ||
| Zeile 327: | Zeile 395: | ||
| </ | </ | ||
| - | === Aktivieren | + | === 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: | ||
| + | <code bash> | ||
| + | sudo systemctl restart course_web | ||
| + | sudo systemctl status course_web | ||
| </ | </ | ||
projekt/python_fastapi.1771659113.txt.gz · Zuletzt geändert: von torsten.roehl
