Files
wetterstation_python/wetterstation.py
2026-01-24 20:42:04 +01:00

583 lines
19 KiB
Python

#!/usr/bin/env python3
"""
Wetterstation - MQTT Datenempfang und Web-Visualisierung
"""
import sqlite3
import json
import threading
from datetime import datetime, timedelta
from flask import Flask, render_template, jsonify
import paho.mqtt.client as mqtt
# Konfiguration
MQTT_HOST = "rexfue.de"
MQTT_PORT = 1883
MQTT_TOPIC = "vantage/live" # Bitte anpassen!
MQTT_USER = "stzuhr" # Bitte anpassen!
MQTT_PASSWORD = "74chQCYb" # Bitte anpassen!
DB_FILE = "wetterdaten.db"
app = Flask(__name__)
class WetterDB:
"""Klasse für Datenbankoperationen"""
def __init__(self, db_file):
self.db_file = db_file
self.init_db()
def init_db(self):
"""Datenbank initialisieren"""
conn = sqlite3.connect(self.db_file)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS wetterdaten (
id INTEGER PRIMARY KEY AUTOINCREMENT,
datetime TEXT NOT NULL,
pressure REAL,
wind_gust REAL,
wind_speed REAL,
wind_dir REAL,
rain_rate REAL,
rain REAL,
humidity INTEGER,
temperature REAL
)
''')
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_datetime ON wetterdaten(datetime)
''')
conn.commit()
conn.close()
def save_data(self, data):
"""Wetterdaten speichern"""
conn = sqlite3.connect(self.db_file)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO wetterdaten
(datetime, pressure, wind_gust, wind_speed, wind_dir,
rain_rate, rain, humidity, temperature)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
data['datetime'],
data.get('pressure'),
data.get('wind_gust'),
data.get('wind_speed'),
data.get('wind_dir'),
data.get('rain_rate'),
data.get('rain'),
data.get('humidity'),
data.get('temperature')
))
conn.commit()
conn.close()
print(f"Daten gespeichert: {data['datetime']}")
def get_data(self, hours=24):
"""Daten der letzten X Stunden abrufen"""
conn = sqlite3.connect(self.db_file)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
time_threshold = (datetime.now() - timedelta(hours=hours)).strftime('%Y-%m-%d %H:%M:%S')
cursor.execute('''
SELECT * FROM wetterdaten
WHERE datetime >= ?
ORDER BY datetime ASC
''', (time_threshold,))
rows = cursor.fetchall()
conn.close()
return [dict(row) for row in rows]
def get_hourly_rain(self, hours=24):
"""Regenmenge pro Stunde berechnen"""
conn = sqlite3.connect(self.db_file)
cursor = conn.cursor()
time_threshold = (datetime.now() - timedelta(hours=hours)).strftime('%Y-%m-%d %H:%M:%S')
cursor.execute('''
SELECT
strftime('%Y-%m-%d %H:00:00', datetime) as hour,
SUM(rain_rate) as total_rain
FROM wetterdaten
WHERE datetime >= ?
GROUP BY hour
ORDER BY hour ASC
''', (time_threshold,))
rows = cursor.fetchall()
conn.close()
return [{'hour': row[0], 'rain': row[1] or 0} for row in rows]
# Globale Datenbankinstanz
db = WetterDB(DB_FILE)
class MQTTClient:
"""MQTT Client für Datenempfang"""
def __init__(self):
self.client = mqtt.Client()
self.client.username_pw_set(MQTT_USER, MQTT_PASSWORD)
# Stabilere Verbindungen bei Abbrüchen
try:
self.client.reconnect_delay_set(min_delay=1, max_delay=120)
except Exception:
pass
# Optionale Protokollierung (hilfreich beim Debuggen)
try:
self.client.enable_logger()
except Exception:
pass
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
@staticmethod
def _sanitize_data(payload: dict) -> dict:
"""Payload robust in erwartetes Format wandeln.
- Fehlende `datetime` wird mit aktueller Zeit ergänzt
- Felder in richtige Typen konvertieren
"""
def to_float(x):
try:
return float(x) if x is not None else None
except Exception:
return None
def to_int(x):
try:
return int(x) if x is not None else None
except Exception:
return None
# Zeitstempel: wenn vorhanden, in akzeptables Format bringen, sonst jetzt
dt = payload.get('datetime')
if isinstance(dt, (int, float)):
# Epoch -> ISO
dt = datetime.fromtimestamp(dt).strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(dt, str):
# Versuchen, ISO-Varianten in 'YYYY-MM-DD HH:MM:SS' zu überführen
# Entferne ggf. 'T' oder 'Z'
dt_clean = dt.replace('T', ' ').replace('Z', '').strip()
# Falls Millisekunden enthalten, abschneiden
if '.' in dt_clean:
dt_clean = dt_clean.split('.')[0]
# Bei zu kurzem String: fallback auf jetzt
if len(dt_clean) < 16:
dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
else:
dt = dt_clean
else:
dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
return {
'datetime': dt,
'pressure': to_float(payload.get('pressure')),
'wind_gust': to_float(payload.get('wind_gust')),
'wind_speed': to_float(payload.get('wind_speed')),
'wind_dir': to_float(payload.get('wind_dir')),
'rain_rate': to_float(payload.get('rain_rate')),
'rain': to_float(payload.get('rain')),
'humidity': to_int(payload.get('humidity')),
'temperature': to_float(payload.get('temperature')),
}
def on_connect(self, client, userdata, flags, rc):
"""Callback bei Verbindung"""
if rc == 0:
print(f"Mit MQTT Broker verbunden: {MQTT_HOST}")
client.subscribe(MQTT_TOPIC)
print(f"Topic abonniert: {MQTT_TOPIC}")
else:
print(f"Verbindungsfehler: {rc}")
def on_message(self, client, userdata, msg):
"""Callback bei empfangener Nachricht"""
try:
raw = json.loads(msg.payload.decode())
data = self._sanitize_data(raw)
print(f"Empfangen und gespeichert: {data}")
db.save_data(data)
except Exception as e:
print(f"Fehler beim Verarbeiten der Nachricht: {e}")
def start(self):
"""MQTT Client starten"""
try:
self.client.connect(MQTT_HOST, MQTT_PORT, 60)
self.client.loop_start()
print("MQTT Client gestartet")
except Exception as e:
print(f"MQTT Verbindungsfehler: {e}")
# Flask Routes
@app.route('/')
def index():
"""Hauptseite"""
return render_template('index.html')
@app.route('/api/data/<period>')
def get_data(period):
"""API Endpoint für Wetterdaten"""
hours = 24 if period == 'day' else 168 # 168h = 1 Woche
data = db.get_data(hours)
rain_data = db.get_hourly_rain(hours)
return jsonify({
'data': data,
'rain_hourly': rain_data
})
def create_html_template():
"""HTML Template erstellen"""
import os
os.makedirs('templates', exist_ok=True)
html_content = '''<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wetterstation</title>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1400px;
margin: 0 auto;
background: white;
border-radius: 20px;
padding: 30px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
h1 {
text-align: center;
color: #333;
margin-bottom: 30px;
font-size: 2.5em;
}
.tabs {
display: flex;
justify-content: center;
gap: 20px;
margin-bottom: 30px;
}
.tab {
padding: 15px 40px;
background: #f0f0f0;
border: none;
border-radius: 10px;
cursor: pointer;
font-size: 18px;
transition: all 0.3s;
font-weight: 600;
}
.tab:hover {
background: #e0e0e0;
transform: translateY(-2px);
}
.tab.active {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.charts-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 20px;
margin-top: 20px;
}
.chart-container {
background: #f9f9f9;
border-radius: 15px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
/* feste Mindesthöhe sorgt für konsistente Layouts */
min-height: 420px;
}
/* Stellt sicher, dass Charts nicht breiter als ihre Container werden */
#charts { width: 100%; }
.chart-container > div { width: 100%; max-width: 100%; }
.loading {
text-align: center;
padding: 50px;
font-size: 20px;
color: #666;
}
@media (max-width: 1024px) {
.charts-grid {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="container">
<h1>🌤️ Wetterstation Dashboard</h1>
<div class="tabs">
<button class="tab active" onclick="switchPeriod('day')">Tag (24h)</button>
<button class="tab" onclick="switchPeriod('week')">Woche (7 Tage)</button>
</div>
<div class="loading" id="loading">Lade Daten...</div>
<div class="charts-grid" id="charts" style="display: none;">
<div class="chart-container">
<div id="temp-chart"></div>
</div>
<div class="chart-container">
<div id="humidity-chart"></div>
</div>
<div class="chart-container">
<div id="pressure-chart"></div>
</div>
<div class="chart-container">
<div id="rain-chart"></div>
</div>
<div class="chart-container">
<div id="wind-speed-chart"></div>
</div>
<div class="chart-container">
<div id="wind-dir-chart"></div>
</div>
</div>
</div>
<script>
let currentPeriod = 'day';
const DEFAULT_CHART_HEIGHT = 360;
const POLAR_CHART_HEIGHT = 420;
// HighCharts globale Einstellungen
Highcharts.setOptions({
lang: {
months: ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni',
'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
shortMonths: ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun',
'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'],
weekdays: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
shortWeekdays: ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa']
}
});
function switchPeriod(period) {
currentPeriod = period;
// Tab-Status aktualisieren
document.querySelectorAll('.tab').forEach(tab => {
tab.classList.remove('active');
});
event.target.classList.add('active');
loadData();
}
function loadData() {
document.getElementById('loading').style.display = 'block';
document.getElementById('charts').style.display = 'none';
fetch(`/api/data/${currentPeriod}`)
.then(response => response.json())
.then(data => {
renderCharts(data);
document.getElementById('loading').style.display = 'none';
document.getElementById('charts').style.display = 'grid';
})
.catch(error => {
console.error('Fehler beim Laden:', error);
document.getElementById('loading').innerHTML = 'Fehler beim Laden der Daten';
});
}
function renderCharts(apiData) {
const data = apiData.data;
const rainData = apiData.rain_hourly;
// Formatiere Timestamps: nur Uhrzeit ohne Datum und Sekunden (HH:MM)
const timestamps = data.map(d => d.datetime.substring(11, 16));
const rainTimestamps = rainData.map(d => d.hour.substring(11, 16));
// Berechne step für x-Achsen Labels: zeige nur alle 30 Minuten eine Zeit
// Bei 48 Labels pro Tag (alle 30 Min) ergibt sich der step aus der Datenmenge
const step = Math.max(1, Math.ceil(data.length / 48));
const rainStep = Math.max(1, Math.ceil(rainData.length / 48));
// Temperatur
Highcharts.chart('temp-chart', {
chart: { type: 'line', height: DEFAULT_CHART_HEIGHT, spacingRight: 20 },
title: { text: '🌡️ Temperatur (°C)' },
xAxis: { categories: timestamps, title: { text: 'Zeit' }, labels: { step: step } },
yAxis: { title: { text: '°C' } },
legend: { enabled: true },
series: [{
name: 'Temperatur',
data: data.map(d => d.temperature),
color: '#ff6b6b',
lineWidth: 2
}],
credits: { enabled: false }
});
// Luftfeuchtigkeit
Highcharts.chart('humidity-chart', {
chart: { type: 'line', height: DEFAULT_CHART_HEIGHT, spacingRight: 20 },
title: { text: '💧 Luftfeuchtigkeit (%)' },
xAxis: { categories: timestamps, title: { text: 'Zeit' }, labels: { step: step } },
yAxis: { title: { text: '%' } },
legend: { enabled: true },
series: [{
name: 'Luftfeuchtigkeit',
data: data.map(d => d.humidity),
color: '#4ecdc4',
lineWidth: 2
}],
credits: { enabled: false }
});
// Luftdruck
Highcharts.chart('pressure-chart', {
chart: { type: 'line', height: DEFAULT_CHART_HEIGHT, spacingRight: 20 },
title: { text: '🎈 Luftdruck (hPa)' },
xAxis: { categories: timestamps, title: { text: 'Zeit' } },
yAxis: { title: { text: 'hPa' } },
legend: { enabled: true },
series: [{
name: 'Luftdruck',
data: data.map(d => d.pressure),
color: '#95e1d3',
lineWidth: 2
}],
credits: { enabled: false }
});
// Regenmenge pro Stunde
Highcharts.chart('rain-chart', {
chart: { type: 'column', height: DEFAULT_CHART_HEIGHT, spacingRight: 20 },
title: { text: '🌧️ Regenmenge pro Stunde (mm)' },
xAxis: { categories: rainTimestamps, title: { text: 'Zeit' } },
yAxis: { title: { text: 'mm' } },
legend: { enabled: false },
series: [{
name: 'Regen',
data: rainData.map(d => d.rain),
color: '#3498db'
}],
credits: { enabled: false }
});
// Windgeschwindigkeit
Highcharts.chart('wind-speed-chart', {
chart: { type: 'line', height: DEFAULT_CHART_HEIGHT, spacingRight: 20 },
title: { text: '💨 Windgeschwindigkeit (m/s)' },
xAxis: { categories: timestamps, title: { text: 'Zeit' } },
yAxis: { title: { text: 'm/s' } },
legend: { enabled: true },
series: [{
name: 'Windgeschwindigkeit',
data: data.map(d => d.wind_speed),
color: '#f38181',
lineWidth: 2
}, {
name: 'Böen',
data: data.map(d => d.wind_gust),
color: '#aa96da',
lineWidth: 2,
dashStyle: 'dash'
}],
credits: { enabled: false }
});
// Windrichtung (Scatter-Plot mit Farbcodierung)
Highcharts.chart('wind-dir-chart', {
chart: { type: 'scatter', height: POLAR_CHART_HEIGHT, spacingRight: 20 },
title: { text: '🧭 Windrichtung' },
xAxis: { title: { text: 'Windrichtung (°)' }, min: 0, max: 360 },
yAxis: { title: { text: 'Geschwindigkeit (m/s)' } },
legend: { enabled: true },
colorAxis: { min: 0, max: Math.max(...data.map(d => d.wind_speed || 0)) },
series: [{
name: 'Wind',
data: data.map(d => ({
x: d.wind_dir || 0,
y: d.wind_speed || 0,
colorValue: d.wind_speed || 0
})),
colorByPoint: true,
marker: { radius: 4 }
}],
credits: { enabled: false }
});
}
// Initiales Laden
loadData();
// Auto-Refresh alle 5 Minuten
setInterval(loadData, 5 * 60 * 1000);
</script>
</body>
</html>'''
with open('templates/index.html', 'w', encoding='utf-8') as f:
f.write(html_content)
print("HTML Template erstellt: templates/index.html")
def main():
"""Hauptprogramm"""
print("Wetterstation wird gestartet...")
# HTML Template erstellen
create_html_template()
# MQTT Client starten
mqtt_client = MQTTClient()
mqtt_client.start()
# Flask Server starten
print("\nWeb-Interface verfügbar unter: http://localhost:5003")
print("Drücke CTRL+C zum Beenden\n")
app.run(host='0.0.0.0', port=5003, debug=False)
if __name__ == '__main__':
main()