Die Sensoren sind fertig. Der Python Code müsste passen.

This commit is contained in:
Chiara 2025-06-17 09:53:16 +02:00
parent 879c5eb881
commit 4a58d6cce5
36 changed files with 41 additions and 91 deletions

View file

@ -1 +0,0 @@
[]

View file

@ -1,15 +0,0 @@
{
"id": "sensor_004",
"type": "light",
"unit": "state",
"reading": [
{
"ts": 1748452265,
"state": "dunkel"
},
{
"ts": 1748452267,
"state": "dunkel"
}
]
}

View file

@ -1,35 +0,0 @@
{
"id": "sensor_001",
"type": "noise",
"unit": "bool",
"readings": [
{
"ts": 1748452253,
"value": 1
},
{
"ts": 1748452254,
"value": 1
},
{
"ts": 1748452255,
"value": 1
},
{
"ts": 1748452256,
"value": 1
},
{
"ts": 1748452257,
"value": 1
},
{
"ts": 1748452258,
"value": 1
},
{
"ts": 1748452259,
"value": 1
}
]
}

View file

@ -4,17 +4,18 @@ import os
import board import board
import digitalio import digitalio
json_file = "ky018_data.json" json_file = "json/ky018_data.json"
MAX_ENTRIES = 3000 MAX_ENTRIES = 3000
light_pin = digitalio.DigitalInOut(board.D17) light_pin = digitalio.DigitalInOut(board.D17)
light_pin.direction = digitalio.Direction.INPUT light_pin.direction = digitalio.Direction.INPUT
data_template = { dataldr = {
"location": "Hausstrasse - 1",
"id": "sensor_004", "id": "sensor_004",
"type": "light", "type": "light",
"unit": "bool", "unit": "bool",
"reading": [] "readings": []
} }
def load_data(): def load_data():
@ -22,7 +23,7 @@ def load_data():
with open(json_file, 'r', encoding='utf-8') as f: with open(json_file, 'r', encoding='utf-8') as f:
return json.load(f) return json.load(f)
else: else:
return data_template.copy() return dataldr.copy()
def save_data(data): def save_data(data):
with open(json_file, 'w', encoding='utf-8') as f: with open(json_file, 'w', encoding='utf-8') as f:
@ -32,10 +33,10 @@ def capture_and_store(state):
timestamp = int(time.time()) timestamp = int(time.time())
data = load_data() data = load_data()
data["reading"].append({"ts": timestamp,"value": state}) data["readings"].append({"ts": timestamp,"value": state})
if len(data["reading"]) > MAX_ENTRIES: if len(data["readings"]) > MAX_ENTRIES:
data["reading"] = data["reading"][-MAX_ENTRIES:] data["readings"] = data["readings"][-MAX_ENTRIES:]
save_data(data) save_data(data)

View file

@ -5,17 +5,18 @@ from datetime import datetime
import os import os
SENSOR_PIN = 17 SENSOR_PIN = 17
JSON_FILE = "ky037_data.json" JSON_FILE = "json/ky037_data.json"
INTERVAL = 1 INTERVAL = 1
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN) GPIO.setup(SENSOR_PIN, GPIO.IN)
SENSOR_INFO = { datamikro = {
"location": "Hausstrasse - 2",
"id": "sensor_001", "id": "sensor_001",
"type": "noise", # Geräuschsensor "type": "noise",
"unit": "bool", # 1 oder 0 (geräusch ja/nein) "unit": "bool",
"readings": [] "readings": []
} }
@ -24,7 +25,7 @@ def load_data():
with open(JSON_FILE, 'r', encoding='utf-8') as f: with open(JSON_FILE, 'r', encoding='utf-8') as f:
return json.load(f) return json.load(f)
else: else:
return SENSOR_INFO.copy() # frisches Grundgerüst return datamikro.copy()
def save_data(data): def save_data(data):
with open(JSON_FILE, 'w', encoding='utf-8') as f: with open(JSON_FILE, 'w', encoding='utf-8') as f:
@ -32,7 +33,7 @@ def save_data(data):
def capture_and_store(): def capture_and_store():
state = GPIO.input(SENSOR_PIN) state = GPIO.input(SENSOR_PIN)
timestamp = int(time.time()) # Unix-Zeitstempel (int) timestamp = int(time.time())
noise = True if state == 0 else False noise = True if state == 0 else False
entry = { "ts": timestamp, "value": noise} entry = { "ts": timestamp, "value": noise}

View file

@ -1,9 +0,0 @@
Temperatur Sensor: Ich müss eine extra venv umgebung erstellen für den Sensor DHT11. Ich mache alles in Visula Studio Code. Ich habe eine Test programm versucht. test.temp.py
Fehlermeldungen:
1. Board nicht gefunden: pip install board
2. adafruit_dht nicht funktioniert: pip3 install adafruit-circuitpython-dht
3. RPI nicht gefunden: pip install RPI.GPIO
TemperaturSensor:

View file

@ -9,23 +9,30 @@ dhtDevice = adafruit_dht.DHT11(board.D23)
json_file = "json/ky015_data.json" json_file = "json/ky015_data.json"
datatemphum = { datatemp = {
"id": "sensor_002/3", "location": "Hausstrasse - 3",
"type": "environment", "sensors": [
"unit": { {
"temperature": "°C", "id": "sensor_002",
"humidity": "%" "type": "temperature",
"unit": "°C",
"readings": []
}, },
"reading": [] {
"id": "sensor_003",
"type": "humidity",
"unit": "%",
"readings": []
}
]
} }
def load_data(): def load_data():
if os.path.exists(json_file): if os.path.exists(json_file):
with open(json_file, 'r', encoding='utf-8') as f: with open(json_file, 'r', encoding='utf-8') as f:
return json.load(f) return json.load(f)
else: else:
return datatemphum.copy() return datatemp.copy()
def save_data(data): def save_data(data):
with open(json_file, 'w', encoding='utf-8') as f: with open(json_file, 'w', encoding='utf-8') as f:
@ -35,17 +42,18 @@ max_entries=3000
def capture_and_store(temperature_c,humidity): def capture_and_store(temperature_c,humidity):
timestamp = int(time.time()) timestamp = int(time.time())
data=load_data()
data = load_data() temp_sensor = next(s for s in data["sensors"] if s["type"] == "temperature")
data["reading"].append({ hum_sensor = next(s for s in data["sensors"] if s["type"] == "humidity")
"ts": timestamp,
"temperature": temperature_c,
"humidity":humidity
})
temp_sensor["readings"].append({"ts": timestamp, "value": temperature_c})
hum_sensor["readings"].append({"ts": timestamp, "value": humidity})
if len(data["reading"]) > max_entries: if len(temp_sensor["readings"]) > max_entries:
data["reading"] = data["reading"][-max_entries:] temp_sensor["readings"] = temp_sensor["readings"][-max_entries:]
if len(hum_sensor["readings"]) > max_entries:
hum_sensor["readings"] = hum_sensor["readings"][-max_entries:]
save_data(data) save_data(data)