added websocktet support.

all sensors should now send data to the webserver at the ip and port defined in sender.py.
This commit is contained in:
Chiara 2025-06-17 22:57:01 +02:00
parent 42ca11f965
commit fc98a1283e
8 changed files with 8071 additions and 518 deletions

View file

@ -3,9 +3,11 @@ import time
import json
from datetime import datetime
import os
from sender import send_data as s
SENSOR_PIN = 17
JSON_FILE = "json/ky037_data.json"
MAX_ENTRIES = 3000
INTERVAL = 1
GPIO.setmode(GPIO.BCM)
@ -14,10 +16,23 @@ GPIO.setup(SENSOR_PIN, GPIO.IN)
datamikro = {
"location": "Hausstrasse - 2",
"id": "sensor_001",
"type": "noise",
"unit": "bool",
"readings": []
"sensors": [
{
"id": "sensor_001",
"type": "noise",
"unit": "bool",
"readings": []
}
]
}
datalive = {
"location": "Hausstrasse - 2",
"sensors": [
{
"id": "sensor_001",
"readings": [{}]
}
]
}
def load_data():
@ -34,17 +49,28 @@ def save_data(data):
def capture_and_store():
state = GPIO.input(SENSOR_PIN)
timestamp = int(time.time())
data = load_data()
noise = True if state == 0 else False
livetemp = datalive.copy()
entry = { "ts": timestamp, "value": noise}
data = load_data()
data["readings"].append(entry)
data["sensors"][0]["readings"].append(entry)
livetemp["sensors"][0]["readings"][0] = (entry)
if len(data["sensors"][0]["readings"]) > MAX_ENTRIES:
data["sensors"][0]["readings"] = data["sensors"][0]["readings"][-MAX_ENTRIES:]
s(livetemp)
livetemp.clear()
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'}")
s(load_data())
if __name__ == "__main__":
print("Starting noise detection with KY-037")
try: