from machine import Pin, I2C from onewire import OneWire from ds18x20 import DS18X20 from time import sleep_ms from lcd_api import LcdApi from pico_i2c_lcd import I2cLcd import _thread # Relais relais = Pin(15, machine.Pin.OUT, value = 1) led_onboard = Pin(25, machine.Pin.OUT, value = 0) # Temperature temp_sensor = DS18X20(OneWire(Pin(16))) sensor_id = temp_sensor.scan()[0] # LCD Display i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) I2C_ADDR = 0x27 I2C_NUM_ROWS = 2 I2C_NUM_COLS = 16 lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS) # Custom characters degree = ( 0b00111, 0b00101, 0b00111, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, ) # Buttons button1 = Pin(2, Pin.IN, Pin.PULL_DOWN) button2 = Pin(3, Pin.IN, Pin.PULL_DOWN) button3 = Pin(4, Pin.IN, Pin.PULL_DOWN) global button1_pressed global button2_pressed global button3_pressed button1_pressed = False button2_pressed = False button3_pressed = False def ctrl_relais(active = True): if active: relais.value(0) led_onboard.on() else: relais.value(1) led_onboard.off() def button_reader_thread(): global button1_pressed global button2_pressed global button3_pressed while True: if button1.value() == 1: button1_pressed = True if button2.value() == 1: button2_pressed = True if button3.value() == 1: button3_pressed = True sleep_ms(100) lcd.backlight_on() lcd.clear() lcd.custom_char(0, degree) temp_tgt = 28.0 temp_gap = 0.5 is_heating = False heat_string = "" system_on = False system_on_string = "" _thread.start_new_thread(button_reader_thread, ()) while True: temp_sensor.convert_temp() sleep_ms(750) temp_curr = temp_sensor.read_temp(sensor_id) if button1_pressed == True: temp_tgt -= 0.5 button1_pressed = False if button2_pressed == True: temp_tgt += 0.5 button2_pressed = False if button3_pressed == True: system_on = not system_on button3_pressed = False if system_on == True and temp_curr < temp_tgt - temp_gap: is_heating = True elif system_on == True and temp_curr > temp_tgt + temp_gap: is_heating = False else: is_heating = False ctrl_relais(is_heating) if system_on == True: system_on_string = "ON " else: system_on_string = "OFF" if is_heating == True: heat_string = ">H<" else: heat_string = " " lcd.move_to(0, 0) lcd.putstr("ACT: {0:3.1f}".format(temp_curr) + chr(0) + "C " + heat_string + "\nTGT: {0:3.1f}".format(temp_tgt) + chr(0) + "C " + system_on_string)