diff --git a/esp32-lora/lora_receiver.py b/esp32-lora/lora_receiver.py index 81dd87fa..fd73e762 100644 --- a/esp32-lora/lora_receiver.py +++ b/esp32-lora/lora_receiver.py @@ -1,8 +1,21 @@ import serial +import json +import re -ser = serial.Serial('/dev/ttyUSB1', 57600, timeout=1) +ser = serial.Serial('/dev/ttyUSB1', 9600, timeout=1) + +# Regex to capture JSON object +json_pattern = re.compile(r'\{.*\}') while True: - line = ser.readline() - if line: - print("Received:", line.decode('utf-8', errors='ignore')) \ No newline at end of file + line = ser.readline().decode('utf-8', errors='ignore').strip() + if not line: + continue + + match = json_pattern.search(line) + if match: + try: + data = json.loads(match.group()) + print("Received JSON:", data) + except json.JSONDecodeError: + pass # ignore lines that aren't valid JSON