diff options
author | Robert Scheibe <rob.scheibe@gmail.com> | 2024-12-22 15:52:40 +0100 |
---|---|---|
committer | Robert Scheibe <rob.scheibe@gmail.com> | 2024-12-22 15:52:40 +0100 |
commit | bd0a388682dae9da9146a9d37ebfd4d2c2527b6e (patch) | |
tree | 6a555b1347dbfab3a956a4b6ede508c1559628b5 /read_temperature.py | |
parent | 44a4a65f8c7244244d9b9102193d682ba89d3048 (diff) |
added history.html and cpu temperature plotting
Diffstat (limited to 'read_temperature.py')
-rwxr-xr-x | read_temperature.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/read_temperature.py b/read_temperature.py new file mode 100755 index 0000000..0d9249e --- /dev/null +++ b/read_temperature.py @@ -0,0 +1,61 @@ +#!/usr/bin/python3 +import sqlite3 +import subprocess +from datetime import datetime + +# Funktion zur Messung der CPU-Temperatur +def get_cpu_temperature(): + try: + # Führt den Befehl vcgencmd aus, um die CPU-Temperatur zu messen + output = subprocess.run(['vcgencmd', 'measure_temp'], stdout=subprocess.PIPE) + temp_str = output.stdout.decode('utf-8') + # Extrahiert die Temperatur als Float-Wert + temp = float(temp_str.split('=')[1].split("'")[0]) + return temp + except Exception as e: + print(f"Fehler beim Abrufen der CPU-Temperatur: {e}") + return None + +# Funktion zur Initialisierung der SQLite-Datenbank +def init_db(db_name): + conn = sqlite3.connect(db_name) + cursor = conn.cursor() + # Erstellt eine Tabelle, falls sie noch nicht existiert + cursor.execute(""" + CREATE TABLE IF NOT EXISTS cpu_temperature ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + timestamp TEXT NOT NULL, + temperature REAL NOT NULL + ) + """) + conn.commit() + return conn + +# Funktion zum Einfügen von Daten in die Datenbank +def insert_data(conn, timestamp, temperature): + cursor = conn.cursor() + cursor.execute(""" + INSERT INTO cpu_temperature (timestamp, temperature) + VALUES (?, ?) + """, (timestamp, temperature)) + conn.commit() + +# Hauptfunktion +def main(): + db_name = "/var/www/html/cpu_temperature.db" + conn = init_db(db_name) + + # Ruft die aktuelle CPU-Temperatur ab + temperature = get_cpu_temperature() + if temperature is not None: + # Holt das aktuelle Datum und die Uhrzeit + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + # Speichert die Daten in der Datenbank + insert_data(conn, timestamp, temperature) + print(f"Gespeichert: {timestamp} - {temperature}°C") + + conn.close() + +if __name__ == "__main__": + main() + |