gbmanager/micropython/main.py

141 lines
3.4 KiB
Python

from machine import Pin, I2C
from onewire import OneWire
from ds18x20 import DS18X20
from time import sleep_ms, ticks_ms, ticks_diff
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
# Version
VERSION = "1.0.0"
# Relais
relais = Pin(18, machine.Pin.OUT, value = 1)
#led_onboard = Pin(25, machine.Pin.OUT, value = 0)
# Temperature
temp_sensor = DS18X20(OneWire(Pin(28)))
sensor_id = temp_sensor.scan()[0]
# LCD Display
i2c = I2C(1, sda=Pin(26), scl=Pin(27), 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,
)
umlaut_a = (
0b01010,
0b00000,
0b01110,
0b00001,
0b01111,
0b10001,
0b01111,
0b00000,
)
def ctrl_relais(active = True):
if active:
relais.value(0)
#led_onboard.on()
else:
relais.value(1)
#led_onboard.off()
lcd.backlight_on()
lcd.clear()
lcd.custom_char(0, degree)
lcd.custom_char(1, umlaut_a)
temp_tgt = 28.0
temp_gap = 0.5
is_heating = False
heat_string = ""
system_on = False
system_on_string = "OFF"
temp_curr = 0
lcd.move_to(0, 0)
lcd.putstr(" G" + chr(1) + "rbox Manager\n (Ver. "+ VERSION + ")")
sleep_ms(3000)
# Buttons
PIN_BUTTON_1 = 17
PIN_BUTTON_2 = 16
PIN_BUTTON_3 = 15
button1 = Pin(PIN_BUTTON_1, Pin.IN, Pin.PULL_DOWN)
button2 = Pin(PIN_BUTTON_2, Pin.IN, Pin.PULL_DOWN)
button3 = Pin(PIN_BUTTON_3, Pin.IN, Pin.PULL_DOWN)
button1_last = ticks_ms()
button2_last = ticks_ms()
button3_last = ticks_ms()
def button_action(pin):
global temp_tgt
global button1_last
global button2_last
global button3_last
global system_on
global system_on_string
if pin is button1:
if ticks_diff(ticks_ms(), button1_last) > 750:
temp_tgt -= 0.5
#lcd.move_to(5, 1)
#lcd.putstr("{0:3.1f}".format(temp_tgt))
button1_last = ticks_ms()
elif pin is button2:
if ticks_diff(ticks_ms(), button2_last) > 750:
temp_tgt += 0.5
#lcd.move_to(5, 1)
#lcd.putstr("{0:3.1f}".format(temp_tgt))
button2_last = ticks_ms()
elif pin is button3:
if ticks_diff(ticks_ms(), button3_last) > 750:
system_on = not system_on
if system_on == True:
system_on_string = "ON "
else:
system_on_string = "OFF"
#lcd.move_to(13, 1)
#lcd.putstr(system_on_string)
button3_last = ticks_ms()
button1.irq(trigger = Pin.IRQ_RISING, handler = button_action)
button2.irq(trigger = Pin.IRQ_RISING, handler = button_action)
button3.irq(trigger = Pin.IRQ_RISING, handler = button_action)
while True:
temp_sensor.convert_temp()
sleep_ms(750)
temp_curr = temp_sensor.read_temp(sensor_id)
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
elif system_on == False:
is_heating = False
ctrl_relais(is_heating)
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)