from machine import Pin
import onewire
import ds18x20
import time
green_led = Pin(28, Pin.OUT)
btn = Pin(27, Pin.IN, Pin.PULL_UP)
red_led = Pin(2, Pin.OUT)
switch_pressed = False
last_press = None
DEBOUNCE_MS = 50
ow = onewire.OneWire(Pin(19)) # create a OneWire bus on GPIO12
ds = ds18x20.DS18X20(ow)
def check_temp():
_temp = 0
print("3", ds)
roms = ds.scan()
print("Roms", roms)
ds.convert_temp()
time.sleep_ms(750)
for rom in roms:
_temp = ds.read_temp(rom)
print("Temp: ",_temp)
return _temp
def switch(pin):
global switch_pressed, last_press
now = time.ticks_ms()
if last_press is None or time.ticks_diff(now, last_press) >= DEBOUNCE_MS:
switch_pressed = not switch_pressed
last_press = now
btn.irq(trigger=Pin.IRQ_FALLING, handler=switch)
last_state = switch_pressed
while True:
time.sleep(20)
green_led.on()
if(check_temp() > 50):
red_led.on()
else:
red_led.off()
time.sleep(1)