gbmanager/micropython/main.py

141 lines
3.4 KiB
Python
Raw Permalink Normal View History

2022-04-28 21:25:32 +02:00
from machine import Pin, I2C
from onewire import OneWire
from ds18x20 import DS18X20
2022-05-25 23:01:40 +02:00
from time import sleep_ms, ticks_ms, ticks_diff
2022-04-28 21:25:32 +02:00
from lcd_api import LcdApi
2022-04-30 15:54:29 +02:00
from pico_i2c_lcd import I2cLcd
2022-05-25 23:01:40 +02:00
# Version
VERSION = "1.0.0"
2022-04-27 21:45:24 +02:00
2022-04-28 21:25:32 +02:00
# Relais
2022-05-24 21:35:18 +02:00
relais = Pin(18, machine.Pin.OUT, value = 1)
2022-05-25 23:01:40 +02:00
#led_onboard = Pin(25, machine.Pin.OUT, value = 0)
2022-04-28 21:25:32 +02:00
# Temperature
2022-05-24 21:35:18 +02:00
temp_sensor = DS18X20(OneWire(Pin(28)))
2022-04-28 21:25:32 +02:00
sensor_id = temp_sensor.scan()[0]
# LCD Display
2022-05-24 21:35:18 +02:00
i2c = I2C(1, sda=Pin(26), scl=Pin(27), freq=400000)
2022-04-28 22:05:52 +02:00
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
2022-04-28 21:25:32 +02:00
2022-04-30 15:54:29 +02:00
# Custom characters
degree = (
0b00111,
0b00101,
0b00111,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
)
2022-05-25 23:01:40 +02:00
umlaut_a = (
0b01010,
0b00000,
0b01110,
0b00001,
0b01111,
0b10001,
0b01111,
0b00000,
)
2022-04-27 21:45:24 +02:00
def ctrl_relais(active = True):
if active:
relais.value(0)
2022-05-25 23:01:40 +02:00
#led_onboard.on()
2022-04-27 21:45:24 +02:00
else:
relais.value(1)
2022-05-25 23:01:40 +02:00
#led_onboard.off()
2022-04-30 15:54:29 +02:00
lcd.backlight_on()
lcd.clear()
lcd.custom_char(0, degree)
2022-05-25 23:01:40 +02:00
lcd.custom_char(1, umlaut_a)
2022-04-30 15:54:29 +02:00
temp_tgt = 28.0
temp_gap = 0.5
is_heating = False
heat_string = ""
system_on = False
2022-05-25 23:01:40 +02:00
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
2022-04-27 21:45:24 +02:00
2022-05-25 23:01:40 +02:00
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)
2022-04-27 21:45:24 +02:00
while True:
2022-04-28 21:25:32 +02:00
temp_sensor.convert_temp()
sleep_ms(750)
2022-04-30 15:54:29 +02:00
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
2022-05-25 23:01:40 +02:00
elif system_on == False:
is_heating = False
2022-05-24 21:48:19 +02:00
2022-04-30 15:54:29 +02:00
ctrl_relais(is_heating)
if is_heating == True:
heat_string = ">H<"
else:
heat_string = " "
2022-04-28 22:05:52 +02:00
lcd.move_to(0, 0)
2022-04-30 15:54:29 +02:00
lcd.putstr("ACT: {0:3.1f}".format(temp_curr) + chr(0) + "C " + heat_string +
2022-05-25 23:01:40 +02:00
"\nTGT: {0:3.1f}".format(temp_tgt) + chr(0) + "C " + system_on_string)