diff --git a/sensors/ky018_data.json b/sensors/ky018_data.json new file mode 100644 index 0000000..f5d81ab --- /dev/null +++ b/sensors/ky018_data.json @@ -0,0 +1,171 @@ +{ + "id": "lightSensorDigital", + "type": "light", + "unit": "state", + "reading": [ + { + "ts": 1748449834, + "state": "dunkel" + }, + { + "ts": 1748449836, + "state": "dunkel" + }, + { + "ts": 1748449838, + "state": "dunkel" + }, + { + "ts": 1748449840, + "state": "dunkel" + }, + { + "ts": 1748449842, + "state": "dunkel" + }, + { + "ts": 1748449844, + "state": "hell" + }, + { + "ts": 1748449846, + "state": "dunkel" + }, + { + "ts": 1748449848, + "state": "dunkel" + }, + { + "ts": 1748449850, + "state": "dunkel" + }, + { + "ts": 1748449852, + "state": "dunkel" + }, + { + "ts": 1748449854, + "state": "dunkel" + }, + { + "ts": 1748449856, + "state": "dunkel" + }, + { + "ts": 1748449858, + "state": "dunkel" + }, + { + "ts": 1748449860, + "state": "dunkel" + }, + { + "ts": 1748449927, + "state": "dunkel" + }, + { + "ts": 1748449929, + "state": "dunkel" + }, + { + "ts": 1748449931, + "state": "dunkel" + }, + { + "ts": 1748449933, + "state": "dunkel" + }, + { + "ts": 1748449935, + "state": "dunkel" + }, + { + "ts": 1748449937, + "state": "dunkel" + }, + { + "ts": 1748449939, + "state": "dunkel" + }, + { + "ts": 1748449941, + "state": "dunkel" + }, + { + "ts": 1748449943, + "state": "dunkel" + }, + { + "ts": 1748449945, + "state": "dunkel" + }, + { + "ts": 1748449947, + "state": "dunkel" + }, + { + "ts": 1748449953, + "state": "dunkel" + }, + { + "ts": 1748449955, + "state": "dunkel" + }, + { + "ts": 1748449957, + "state": "dunkel" + }, + { + "ts": 1748449959, + "state": "dunkel" + }, + { + "ts": 1748449961, + "state": "dunkel" + }, + { + "ts": 1748449963, + "state": "dunkel" + }, + { + "ts": 1748449965, + "state": "dunkel" + }, + { + "ts": 1748449967, + "state": "hell" + }, + { + "ts": 1748449969, + "state": "hell" + }, + { + "ts": 1748449971, + "state": "hell" + }, + { + "ts": 1748449973, + "state": "hell" + }, + { + "ts": 1748449992, + "state": "hell" + }, + { + "ts": 1748449994, + "state": "hell" + }, + { + "ts": 1748449996, + "state": "hell" + }, + { + "ts": 1748449998, + "state": "hell" + }, + { + "ts": 1748450000, + "state": "hell" + } + ] +} \ No newline at end of file diff --git a/sensors/lichtwiderstandsSensor.py b/sensors/lichtwiderstandsSensor.py new file mode 100644 index 0000000..876b2dc --- /dev/null +++ b/sensors/lichtwiderstandsSensor.py @@ -0,0 +1,57 @@ +import time +import json +import os +import board +import digitalio + +json_file = "ky018_data.json" +MAX_ENTRIES = 3000 + +light_pin = digitalio.DigitalInOut(board.D17) +light_pin.direction = digitalio.Direction.INPUT + +data_template = { + "id": "sensor_004", + "type": "light", + "unit": "state", + "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 data_template.copy() + +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): + timestamp = int(time.time()) + data = load_data() + + data["reading"].append({ + "ts": timestamp, + "state": state + }) + + if len(data["reading"]) > MAX_ENTRIES: + data["reading"] = data["reading"][-MAX_ENTRIES:] + + save_data(data) + +try: + while True: + if light_pin.value: + light_state = "hell" + else: + light_state = "dunkel" + + print(f"Lichtzustand: {light_state}") + capture_and_store(light_state) + time.sleep(2) + +except KeyboardInterrupt: + print("Programm beendet.") \ No newline at end of file