projekt:python_fastapi1
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| projekt:python_fastapi1 [2026/02/21 10:57] – angelegt torsten.roehl | projekt:python_fastapi1 [2026/02/21 13:22] (aktuell) – gelöscht torsten.roehl | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ====== Python FASTAPI ====== | ||
| - | [[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.// | ||
| - | ====== Überblick ====== | ||
| - | * Voraussetzungen | ||
| - | * Software | ||
| - | * Konfiguration | ||
| - | |||
| - | ====== Details ====== | ||
| - | |||
| - | ===== 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> | ||
| - | 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 ==== | ||
| - | |||
| - | '' | ||
| - | < | ||
| - | 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> | ||
| - | sudo mv / | ||
| - | </ | ||
| - | </ | ||
| - | |||
| - | <code html / | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | < | ||
| - | |||
| - | < | ||
| - | |||
| - | <ul> | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | </ul> | ||
| - | |||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | ==== Hardware ==== | ||
| - | Die Hardware, also die LED-Ampel und der Temperatursensor, | ||
| - | |||
| - | ===== 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 glob | ||
| - | import time | ||
| - | |||
| - | # ----------------------------- | ||
| - | # API-Funktionen GPIO LED Ampel | ||
| - | # ----------------------------- | ||
| - | |||
| - | PIN_R = 17 | ||
| - | PIN_Y = 27 | ||
| - | PIN_G = 22 | ||
| - | |||
| - | _initialized = False | ||
| - | |||
| - | def init(): | ||
| - | global _initialized | ||
| - | if _initialized: | ||
| - | return | ||
| - | |||
| - | GPIO.setwarnings(False) | ||
| - | GPIO.setmode(GPIO.BCM) | ||
| - | |||
| - | GPIO.setup(PIN_R, | ||
| - | GPIO.setup(PIN_Y, | ||
| - | GPIO.setup(PIN_G, | ||
| - | |||
| - | _initialized = True | ||
| - | |||
| - | |||
| - | def setLED(pin, value): | ||
| - | GPIO.output(pin, | ||
| - | |||
| - | |||
| - | def setRedLED(value): | ||
| - | setLED(PIN_R, | ||
| - | |||
| - | |||
| - | def setYellowLED(value): | ||
| - | setLED(PIN_Y, | ||
| - | |||
| - | |||
| - | def setGreenLED(value): | ||
| - | setLED(PIN_G, | ||
| - | |||
| - | |||
| - | def status(): | ||
| - | return ( | ||
| - | int(GPIO.input(PIN_R)), | ||
| - | int(GPIO.input(PIN_Y)), | ||
| - | int(GPIO.input(PIN_G)), | ||
| - | ) | ||
| - | |||
| - | |||
| - | # ----------------------------- | ||
| - | # API-Funktionen ds18b20 | ||
| - | # ----------------------------- | ||
| - | |||
| - | SENSOR_TIMEOUT = 1 | ||
| - | |||
| - | def get_sensor(): | ||
| - | sensors = glob.glob("/ | ||
| - | if not sensors: | ||
| - | return None | ||
| - | return sensors[0] + "/ | ||
| - | |||
| - | |||
| - | def get_temperature(): | ||
| - | sensor_file = get_sensor() | ||
| - | if sensor_file is None: | ||
| - | return None | ||
| - | |||
| - | start_time = time.time() | ||
| - | |||
| - | while True: | ||
| - | with open(sensor_file, | ||
| - | lines = f.readlines() | ||
| - | |||
| - | if lines[0].strip().endswith(" | ||
| - | break | ||
| - | |||
| - | if time.time() - start_time > SENSOR_TIMEOUT: | ||
| - | return None | ||
| - | |||
| - | time.sleep(0.1) | ||
| - | |||
| - | temp_line = lines[1] | ||
| - | temp_str = temp_line.split(" | ||
| - | return float(temp_str) / 1000.0 | ||
| - | </ | ||
| - | |||
| - | |||
| - | ==== HTML ==== | ||
| - | |||
| - | === LED === | ||
| - | |||
| - | |||
| - | <code html / | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | < | ||
| - | |||
| - | < | ||
| - | |||
| - | < | ||
| - | < | ||
| - | <a href="/ | ||
| - | <a href="/ | ||
| - | |||
| - | < | ||
| - | < | ||
| - | <a href="/ | ||
| - | <a href="/ | ||
| - | |||
| - | < | ||
| - | < | ||
| - | <a href="/ | ||
| - | <a href="/ | ||
| - | |||
| - | < | ||
| - | <a href="/"> | ||
| - | |||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | |||
| - | |||
| - | === Temperature === | ||
| - | |||
| - | |||
| - | <code html / | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | < | ||
| - | </ | ||
| - | < | ||
| - | |||
| - | < | ||
| - | |||
| - | < | ||
| - | |||
| - | <br> | ||
| - | <a href="/"> | ||
| - | |||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | |||
| - | |||
| - | ==== FASTAPI APP ==== | ||
| - | |||
| - | |||
| - | <code python / | ||
| - | |||
| - | |||
| - | from fastapi import FastAPI, HTTPException | ||
| - | from fastapi.responses import HTMLResponse, | ||
| - | from core import hardware | ||
| - | |||
| - | app = FastAPI() | ||
| - | |||
| - | |||
| - | @app.on_event(" | ||
| - | def startup(): | ||
| - | hardware.init() | ||
| - | |||
| - | |||
| - | def load_template(name, | ||
| - | try: | ||
| - | with open(f" | ||
| - | html = f.read() | ||
| - | except FileNotFoundError: | ||
| - | raise HTTPException(status_code=500, | ||
| - | |||
| - | for key, value in replacements.items(): | ||
| - | html = html.replace(key, | ||
| - | |||
| - | return html | ||
| - | |||
| - | |||
| - | @app.get("/ | ||
| - | def led_page(): | ||
| - | r, y, g = hardware.status() | ||
| - | return HTMLResponse( | ||
| - | load_template(" | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | }) | ||
| - | ) | ||
| - | |||
| - | |||
| - | @app.get("/ | ||
| - | def set_led(color: | ||
| - | |||
| - | if value not in (0, 1): | ||
| - | raise HTTPException(status_code=400) | ||
| - | |||
| - | if color == " | ||
| - | hardware.setRedLED(value) | ||
| - | elif color == " | ||
| - | hardware.setYellowLED(value) | ||
| - | elif color == " | ||
| - | hardware.setGreenLED(value) | ||
| - | else: | ||
| - | raise HTTPException(status_code=400) | ||
| - | |||
| - | return RedirectResponse(url="/ | ||
| - | |||
| - | |||
| - | @app.get("/ | ||
| - | def temp_page(): | ||
| - | t = hardware.get_temperature() | ||
| - | |||
| - | if t is None: | ||
| - | value = " | ||
| - | else: | ||
| - | value = f" | ||
| - | |||
| - | return HTMLResponse( | ||
| - | load_template(" | ||
| - | " | ||
| - | }) | ||
| - | ) | ||
| - | </ | ||
| - | |||
| - | |||
| - | |||
| - | |||
| - | ===== Konfiguration ===== | ||
| - | In diesem Abschnitt wird die Einbindung der **FastAPI-Anwendung** in den Apache-Webserver sowie die Einrichtung als '' | ||
| - | ==== Apache Proxy ==== | ||
| - | |||
| - | === Konfiguration === | ||
| - | |||
| - | In der Datei '' | ||
| - | |||
| - | <code bash / | ||
| - | ProxyPreserveHost On | ||
| - | |||
| - | ProxyPass | ||
| - | ProxyPassReverse /led | ||
| - | |||
| - | ProxyPass | ||
| - | ProxyPassReverse /temp http:// | ||
| - | </ | ||
| - | |||
| - | === Aktivieren === | ||
| - | <code bash> | ||
| - | sudo a2enmod proxy | ||
| - | sudo a2enmod proxy_http | ||
| - | sudo systemctl restart apache2 | ||
| - | </ | ||
| - | |||
| - | ==== Systemd ==== | ||
| - | |||
| - | Die Service-Datei muss unter ''/ | ||
| - | === Service === | ||
| - | |||
| - | |||
| - | <code bash / | ||
| - | [Unit] | ||
| - | Description=Python Web FastAPI | ||
| - | After=network-online.target | ||
| - | Wants=network-online.target | ||
| - | |||
| - | [Service] | ||
| - | User=pi | ||
| - | WorkingDirectory=/ | ||
| - | ExecStart=/ | ||
| - | Restart=always | ||
| - | |||
| - | [Install] | ||
| - | WantedBy=multi-user.target | ||
| - | </ | ||
| - | |||
| - | === Registrierung === | ||
| - | <code bash> | ||
| - | sudo systemctl daemon-reload | ||
| - | sudo systemctl enable course_web | ||
| - | sudo systemctl start course_web | ||
| - | sudo systemctl status course_web | ||
| - | </ | ||
projekt/python_fastapi1.1771671474.txt.gz · Zuletzt geändert: von torsten.roehl
