Migrated to arduino
This commit is contained in:
43
esp32-lora/light-sensor-python/main.py
Normal file
43
esp32-lora/light-sensor-python/main.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import time
|
||||
import _thread
|
||||
from machine import Pin, ADC, I2C
|
||||
from screen import Screen
|
||||
|
||||
light_adc = ADC(Pin(1))
|
||||
light_adc.atten(ADC.ATTN_11DB)
|
||||
light_adc.width(ADC.WIDTH_12BIT)
|
||||
|
||||
screen = Screen()
|
||||
|
||||
def read_light(samples=8):
|
||||
total = 0
|
||||
for _ in range(samples):
|
||||
total += light_adc.read()
|
||||
time.sleep_ms(5)
|
||||
return total // samples
|
||||
|
||||
def sensor_task():
|
||||
while True:
|
||||
raw = read_light()
|
||||
percent = int((raw / 2439) * 100)
|
||||
advice = None
|
||||
if percent > 80:
|
||||
advice = "Too bright !"
|
||||
elif percent < 15:
|
||||
advice = "Too dark !"
|
||||
else :
|
||||
advice = "I'm fine :)"
|
||||
|
||||
lines = [
|
||||
"Light Sensor",
|
||||
f"Level: {percent}%",
|
||||
f"{advice}"
|
||||
]
|
||||
|
||||
screen.display_lines(lines, 0)
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
_thread.start_new_thread(sensor_task, ())
|
||||
|
||||
print("Main thread running")
|
||||
Reference in New Issue
Block a user