This commit is contained in:
mia 2025-05-21 11:47:39 +02:00
commit f42da30372
3 changed files with 217 additions and 0 deletions

58
sensors/MikrofonSensor.py Normal file
View file

@ -0,0 +1,58 @@
import RPi.GPIO as GPIO
import time
import json
from datetime import datetime
import os
SENSOR_PIN = 17
JSON_FILE = "ky037_data.json"
INTERVAL = 1
GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN)
SENSOR_INFO = {
"id": "sensor_001",
"type": "noise", # Geräuschsensor
"unit": "bool", # 1 oder 0 (geräusch ja/nein)
"readings": []
}
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 SENSOR_INFO.copy() # frisches Grundgerüst
def save_data(data):
with open(JSON_FILE, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4, ensure_ascii=False)
def capture_and_store():
state = GPIO.input(SENSOR_PIN)
timestamp = int(time.time()) # Unix-Zeitstempel (int)
noise = 1 if state == 0 else 0
entry = {
"ts": timestamp,
"value": noise
}
data = load_data()
data["readings"].append(entry)
save_data(data)
dt_str = datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
print(f"[{dt_str}] Noise detected: {'YES' if noise else 'NO'}")
if __name__ == "__main__":
print("Starting noise detection with KY-037")
try:
while True:
capture_and_store()
time.sleep(INTERVAL)
except KeyboardInterrupt:
print("Stopped by user.")
finally:
GPIO.cleanup()

159
sensors/ky037_data.json Normal file
View file

@ -0,0 +1,159 @@
{
"id": "sensor_001",
"type": "noise",
"unit": "bool",
"readings": [
{
"ts": 1747819697,
"value": 1
},
{
"ts": 1747819698,
"value": 1
},
{
"ts": 1747819699,
"value": 1
},
{
"ts": 1747819700,
"value": 1
},
{
"ts": 1747819701,
"value": 1
},
{
"ts": 1747819702,
"value": 1
},
{
"ts": 1747819703,
"value": 1
},
{
"ts": 1747819704,
"value": 1
},
{
"ts": 1747819705,
"value": 0
},
{
"ts": 1747819706,
"value": 0
},
{
"ts": 1747819707,
"value": 0
},
{
"ts": 1747819708,
"value": 0
},
{
"ts": 1747819709,
"value": 0
},
{
"ts": 1747819710,
"value": 0
},
{
"ts": 1747819711,
"value": 0
},
{
"ts": 1747819712,
"value": 0
},
{
"ts": 1747819713,
"value": 0
},
{
"ts": 1747819714,
"value": 0
},
{
"ts": 1747819715,
"value": 1
},
{
"ts": 1747819716,
"value": 0
},
{
"ts": 1747819717,
"value": 0
},
{
"ts": 1747819718,
"value": 0
},
{
"ts": 1747819719,
"value": 0
},
{
"ts": 1747819720,
"value": 0
},
{
"ts": 1747819721,
"value": 0
},
{
"ts": 1747819722,
"value": 0
},
{
"ts": 1747819723,
"value": 0
},
{
"ts": 1747819725,
"value": 0
},
{
"ts": 1747819726,
"value": 0
},
{
"ts": 1747819727,
"value": 0
},
{
"ts": 1747819728,
"value": 0
},
{
"ts": 1747819729,
"value": 0
},
{
"ts": 1747819730,
"value": 1
},
{
"ts": 1747819731,
"value": 1
},
{
"ts": 1747819732,
"value": 1
},
{
"ts": 1747819733,
"value": 1
},
{
"ts": 1747819734,
"value": 1
},
{
"ts": 1747819735,
"value": 1
}
]
}

0
sensors/sensor.md Normal file
View file