MikrofonSensor und TemperaturSenor die zwei Python programme funktionieren. mit den jeweiligen 2 json Datein. Beim TemperaturSensor wird im Terminal keine Wertre ausgegeben aber in der json Datei kann man die Temp und Hum sehen.
This commit is contained in:
parent
4c654ec969
commit
1751076592
2614 changed files with 349009 additions and 0 deletions
79
sensors/temperaturSensor.py
Normal file
79
sensors/temperaturSensor.py
Normal file
|
@ -0,0 +1,79 @@
|
|||
import time
|
||||
import board
|
||||
import adafruit_dht
|
||||
import json
|
||||
from datetime import datetime
|
||||
import os
|
||||
|
||||
dhtDevice = adafruit_dht.DHT11(board.D23)
|
||||
|
||||
json_file = "ky015_data.json"
|
||||
|
||||
datatemphum = {
|
||||
"id": "sensor_002/3",
|
||||
"type": "environment",
|
||||
"unit": {
|
||||
"temperature": "°C",
|
||||
"humidity": "%"
|
||||
},
|
||||
"reading": []
|
||||
}
|
||||
|
||||
|
||||
def load_data():
|
||||
if os.path.exists(json_file):
|
||||
with open(json_file, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
else:
|
||||
return datatemphum.copy()
|
||||
|
||||
def save_data(data):
|
||||
with open(json_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=4, ensure_ascii=False)
|
||||
|
||||
max_entries=3000
|
||||
|
||||
def capture_and_store(temperature_c,humidity):
|
||||
timestamp = int(time.time())
|
||||
|
||||
data = load_data()
|
||||
data["reading"].append({
|
||||
"ts": timestamp,
|
||||
"temperature": temperature_c,
|
||||
"humidity":humidity
|
||||
})
|
||||
|
||||
|
||||
if len(data["reading"]) > max_entries:
|
||||
data["reading"] = data["reading"][-max_entries:]
|
||||
|
||||
save_data(data)
|
||||
|
||||
|
||||
while True:
|
||||
try:
|
||||
temperature_c = dhtDevice.temperature
|
||||
humidity = dhtDevice.humidity
|
||||
|
||||
timestamp = datetime.now().isoformat()
|
||||
|
||||
if temperature_c is not None and humidity is not None:
|
||||
capture_and_store(temperature_c, humidity)
|
||||
else:
|
||||
print("Ungültige Messwerte.")
|
||||
|
||||
except RuntimeError as error:
|
||||
print("Lese-Fehler:", error.args[0])
|
||||
dhtDevice.exit()
|
||||
time.sleep(3.0)
|
||||
dhtDevice=adafruit_dht.DHT11(board.D23)
|
||||
continue
|
||||
except KeyboardInterrupt:
|
||||
dhtDevice.exit()
|
||||
print("Beendet durch Benutzer.")
|
||||
break
|
||||
except Exception as error:
|
||||
dhtDevice.exit()
|
||||
raise error
|
||||
|
||||
time.sleep(3.0)
|
Loading…
Add table
Add a link
Reference in a new issue