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 19:19] – torsten.roehl | projekt:python_fastapi [2026/02/23 07:26] (aktuell) – [Systemd] torsten.roehl | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ====== | + | ====== Python FASTAPI |
| [[raspberry_pi: | [[raspberry_pi: | ||
| - | ====== Python Web (LED + DS18B20) – sauber getrennt ====== | + | //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 |
| - | [[raspberry_pi: | + | |{{ :raspberry_pi: |
| + | |Die LED-Ampel kann nun über den Webbrowser gesteuert und die Temperatur ausgelesen werden.| | ||
| + | ====== Überblick ====== | ||
| + | * Voraussetzungen | ||
| + | * Software | ||
| + | * Konfiguration | ||
| - | ===== 0. ENV ===== | + | ====== Details ====== |
| - | < | + | ===== Voraussetzungen ===== |
| + | |||
| + | ==== 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: | ||
| - | ===== 1. PROJEKTSTRUKTUR ===== | + | <code bash> |
| + | pip install fastapi uvicorn | ||
| + | </ | ||
| + | < | ||
| + | **FastAPI / Uvicorn** | ||
| - | python_web/ | + | * **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 ==== | ||
| + | |||
| + | '' | ||
| + | < | ||
| + | course_web/ | ||
| └── src/ | └── src/ | ||
| ├── app.py | ├── app.py | ||
| ├── core/ | ├── core/ | ||
| + | │ | ||
| │ | │ | ||
| - | └── | + | └── |
| ├── led.html | ├── led.html | ||
| └── temp.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. | ||
| - | ===== 2. HARDWARE | + | <note tip> **Tip** |
| + | |||
| + | Bevor die neue '' | ||
| + | <code bash> | ||
| + | sudo mv / | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <code html / | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | < | ||
| + | |||
| + | < | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | ==== Hardware | ||
| + | Die Hardware, also die LED-Ampel und der Temperatursensor, | ||
| - | Datei: | + | ===== 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> | + | <code python |
| import RPi.GPIO as GPIO | import RPi.GPIO as GPIO | ||
| import glob | import glob | ||
| Zeile 125: | Zeile 188: | ||
| - | ===== 3. HTML – LED ===== | + | ==== HTML ==== |
| - | Datei: | + | === LED === |
| - | / | + | |
| - | <code html> | + | |
| + | < | ||
| < | < | ||
| < | < | ||
| Zeile 140: | 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 160: | Zeile 226: | ||
| - | ===== 4. HTML – Temperatur ===== | + | === Temperature |
| - | Datei: | ||
| - | / | ||
| - | <code html> | + | < |
| < | < | ||
| < | < | ||
| Zeile 185: | Zeile 249: | ||
| - | ===== 5. FASTAPI APP ===== | + | ==== 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, | ||
| Zeile 204: | 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 211: | 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 260: | Zeile 329: | ||
| - | ===== 6. APACHE STARTSEITE ===== | ||
| - | Datei: | ||
| - | / | ||
| - | <code html> | + | ===== 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 '' |
| - | < | + | |
| - | <li><a href="/ | + | |
| - | </ul> | + | |
| - | </body> | + | |
| - | </html> | + | <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 | ||
| </ | </ | ||
| + | </ | ||
| - | ===== 7. APACHE PROXY ===== | ||
| - | < | + | < |
| ProxyPreserveHost On | ProxyPreserveHost On | ||
| Zeile 298: | Zeile 364: | ||
| </ | </ | ||
| + | === Aktivieren === | ||
| + | Damit die Weiterleitung an die FastAPI-Anwendung funktioniert, | ||
| - | ===== 8. SYSTEMD ===== | + | <code bash> |
| + | sudo a2enmod proxy | ||
| + | sudo a2enmod proxy_http | ||
| + | sudo systemctl restart apache2 | ||
| + | </ | ||
| - | Datei: | + | ==== Systemd ==== |
| - | / | + | |
| - | < | + | Die Service-Datei muss unter ''/ |
| + | === Service === | ||
| + | |||
| + | |||
| + | < | ||
| [Unit] | [Unit] | ||
| Description=Python Web FastAPI | Description=Python Web FastAPI | ||
| Zeile 312: | Zeile 387: | ||
| [Service] | [Service] | ||
| User=pi | User=pi | ||
| - | WorkingDirectory=/ | + | WorkingDirectory=/ |
| ExecStart=/ | ExecStart=/ | ||
| Restart=always | Restart=always | ||
| Zeile 319: | 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: | ||
| + | <code bash> | ||
| + | sudo systemctl restart course_web | ||
| + | sudo systemctl status course_web | ||
| </ | </ | ||
projekt/python_fastapi.1771615176.txt.gz · Zuletzt geändert: von torsten.roehl
