From 5f5de3ccc6c18307c178e2edf1033d7139a6dcc0 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 25 Dec 2025 02:29:37 +0100 Subject: [PATCH] Prevent REPL from freezing --- esp32-lora/main.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/esp32-lora/main.py b/esp32-lora/main.py index 24b0ba4c..b4b9bdbc 100644 --- a/esp32-lora/main.py +++ b/esp32-lora/main.py @@ -1,11 +1,11 @@ import time +import _thread from machine import Pin, ADC, I2C from screen import Screen -# ----- ADC (Grove Light Sensor) ----- light_adc = ADC(Pin(1)) -light_adc.atten(ADC.ATTN_11DB) # 0–3.3V range -light_adc.width(ADC.WIDTH_12BIT) # 0–4095 +light_adc.atten(ADC.ATTN_11DB) +light_adc.width(ADC.WIDTH_12BIT) screen = Screen() @@ -16,12 +16,18 @@ def read_light(samples=8): time.sleep_ms(5) return total // samples -while True: - raw = read_light() - percent = int((raw / 4095) * 100) +def sensor_task(): + while True: + raw = read_light() + percent = int((raw / 4095) * 100) - screen.display("Light Sensor", 15, 0) - screen.display("Raw: {}".format(raw), 15, 20) - screen.display("Level: {}%".format(percent), 15, 40) + screen.clear() + screen.display("Light Sensor", 15, 0) + screen.display("Raw: {}".format(raw), 15, 20) + screen.display("Level: {}%".format(percent), 15, 40) - time.sleep(0.5) \ No newline at end of file + time.sleep(1) + +_thread.start_new_thread(sensor_task, ()) + +print("Main thread running") \ No newline at end of file