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

Binary file not shown.

View file

@ -1,30 +0,0 @@
{
"id": "sensor_002/3",
"type": "environment",
"unit": {
"temperature": "°C",
"humidity": "%"
},
"reading": [
{
"ts": 1748436535,
"temperature": 24.7,
"humidity": 58
},
{
"ts": 1748436538,
"temperature": 24.6,
"humidity": 58
},
{
"ts": 1748436541,
"temperature": 24.7,
"humidity": 58
},
{
"ts": 1748436544,
"temperature": 24.7,
"humidity": 59
}
]
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -3,6 +3,7 @@ import json
import os import os
import board import board
import digitalio import digitalio
from sender import send_data as s
json_file = "json/ky018_data.json" json_file = "json/ky018_data.json"
MAX_ENTRIES = 3000 MAX_ENTRIES = 3000
@ -12,11 +13,24 @@ light_pin.direction = digitalio.Direction.INPUT
dataldr = { dataldr = {
"location": "Hausstrasse - 1", "location": "Hausstrasse - 1",
"sensors": [
{
"id": "sensor_004", "id": "sensor_004",
"type": "light", "type": "light",
"unit": "bool", "unit": "bool",
"readings": [] "readings": []
} }
]
}
datalive = {
"location": "Hausstrasse - 1",
"sensors": [
{
"id": "sensor_004",
"readings": [{}]
}
]
}
def load_data(): def load_data():
if os.path.exists(json_file): if os.path.exists(json_file):
@ -32,14 +46,23 @@ def save_data(data):
def capture_and_store(state): def capture_and_store(state):
timestamp = int(time.time()) timestamp = int(time.time())
data = load_data() data = load_data()
livetemp = datalive.copy()
data["readings"].append({"ts": timestamp,"value": state}) entry = { "ts": timestamp, "value": state}
if len(data["readings"]) > MAX_ENTRIES: data["sensors"][0]["readings"].append(entry)
data["readings"] = data["readings"][-MAX_ENTRIES:] 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) save_data(data)
print(load_data())
s(load_data())
try: try:
while True: while True:
if light_pin.value: if light_pin.value:

View file

@ -3,9 +3,11 @@ import time
import json import json
from datetime import datetime from datetime import datetime
import os import os
from sender import send_data as s
SENSOR_PIN = 17 SENSOR_PIN = 17
JSON_FILE = "json/ky037_data.json" JSON_FILE = "json/ky037_data.json"
MAX_ENTRIES = 3000
INTERVAL = 1 INTERVAL = 1
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
@ -14,11 +16,24 @@ GPIO.setup(SENSOR_PIN, GPIO.IN)
datamikro = { datamikro = {
"location": "Hausstrasse - 2", "location": "Hausstrasse - 2",
"sensors": [
{
"id": "sensor_001", "id": "sensor_001",
"type": "noise", "type": "noise",
"unit": "bool", "unit": "bool",
"readings": [] "readings": []
} }
]
}
datalive = {
"location": "Hausstrasse - 2",
"sensors": [
{
"id": "sensor_001",
"readings": [{}]
}
]
}
def load_data(): def load_data():
if os.path.exists(JSON_FILE): if os.path.exists(JSON_FILE):
@ -34,17 +49,28 @@ 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()) timestamp = int(time.time())
data = load_data()
noise = True if state == 0 else False noise = True if state == 0 else False
livetemp = datalive.copy()
entry = { "ts": timestamp, "value": noise} entry = { "ts": timestamp, "value": noise}
data = load_data() data["sensors"][0]["readings"].append(entry)
data["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) save_data(data)
dt_str = datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S') dt_str = datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
print(f"[{dt_str}] Noise detected: {'YES' if noise else 'NO'}") print(f"[{dt_str}] Noise detected: {'YES' if noise else 'NO'}")
s(load_data())
if __name__ == "__main__": if __name__ == "__main__":
print("Starting noise detection with KY-037") print("Starting noise detection with KY-037")
try: try:

View file

@ -2,7 +2,7 @@ import socket
import json import json
import time import time
SERVER_HOST = "127.0.0.1" SERVER_HOST = "172.20.10.5"
SERVER_PORT = 9999 SERVER_PORT = 9999
def send_data(data): def send_data(data):

View file

@ -4,6 +4,7 @@ import adafruit_dht
import json import json
from datetime import datetime from datetime import datetime
import os import os
from sender import send_data as s
dhtDevice = adafruit_dht.DHT11(board.D23) dhtDevice = adafruit_dht.DHT11(board.D23)
@ -26,6 +27,19 @@ datatemp = {
} }
] ]
} }
datalive = {
"location": "Hausstrasse - 3",
"sensors": [
{
"id": "sensor_002",
"readings": [{}]
},
{
"id": "sensor_003",
"readings": [{}]
}
]
}
def load_data(): def load_data():
if os.path.exists(json_file): if os.path.exists(json_file):
@ -43,18 +57,32 @@ 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()
livetemp = datalive.copy()
temp_sensor = next(s for s in data["sensors"] if s["type"] == "temperature") temp_sensor = next(s for s in data["sensors"] if s["type"] == "temperature")
hum_sensor = next(s for s in data["sensors"] if s["type"] == "humidity") hum_sensor = next(s for s in data["sensors"] if s["type"] == "humidity")
livetemp_sensor = datalive["sensors"][0]
livehum_sensor = datalive["sensors"][1]
temp_sensor["readings"].append({"ts": timestamp, "value": temperature_c}) humentry = { "ts": timestamp, "value": humidity}
hum_sensor["readings"].append({"ts": timestamp, "value": humidity}) tempentry = { "ts": timestamp, "value": temperature_c}
temp_sensor["readings"].append(tempentry)
hum_sensor["readings"].append(humentry)
livetemp_sensor["readings"][0] = tempentry
livehum_sensor["readings"][0] = humentry
if len(temp_sensor["readings"]) > max_entries: if len(temp_sensor["readings"]) > max_entries:
temp_sensor["readings"] = temp_sensor["readings"][-max_entries:] temp_sensor["readings"] = temp_sensor["readings"][-max_entries:]
if len(hum_sensor["readings"]) > max_entries: if len(hum_sensor["readings"]) > max_entries:
hum_sensor["readings"] = hum_sensor["readings"][-max_entries:] hum_sensor["readings"] = hum_sensor["readings"][-max_entries:]
s(livetemp_sensor)
s(livehum_sensor)
livetemp.clear()
livetemp_sensor.clear()
livehum_sensor.clear()
save_data(data) save_data(data)