import utime
from dht import DHT11
from ST7735 import TFT
from sysfont import sysfont
from machine import SPI, Pin
#
# Initialize the SPI
#
spi = SPI(0, baudrate=20000000, polarity=0, phase=0,
sck=Pin(2), mosi=Pin(3), miso=Pin(4))
tft = TFT(spi, 15, 14, 5)
tft.initg()
tft.rgb(True)
#
# Set the SetTemp, configure LED, relay, and buttons
#
SetTemp = 10.0 # Set point
LED = Pin(21, Pin.OUT) # LED
Relay = Pin(22, Pin.OUT) # Relay
UP = Pin(18,Pin.IN, Pin.PULL_UP) # Button
DOWN = Pin(19,Pin.IN, Pin.PULL_UP) # Button
START = Pin(20,Pin.IN, Pin.PULL_UP) # Button
LED.value(0) # LED OFF
Relay.value(0) # Relay OFF
t = 10.0
#
# Display heading
#
def Heading():
global SetTemp, t
tft.fill(TFT.WHITE) # Clear screen
tft.rect((5, 10), (110, 100), TFT.RED)
tft.text((18, 40), "TEMP CONTROL", TFT.BLACK, sysfont,
1.1, nowrap=True)
tft.hline((15,51),80, TFT.RED)
tft.text((15, 60), "SetTemp:{:.2f}C".format(SetTemp), TFT.BLUE,
sysfont, 1.1, nowrap=True)
tft.text((15, 75), " Room:{:.2f}C".format(t), TFT.RED,
sysfont, 1.1, nowrap=True)
if Relay.value() == 1:
tft.text((30, 100), "RELAY ON", TFT.RED, sysfont, 1.1, nowrap=True)
else:
tft.text((30, 100), "RELAY OFF", TFT.BLUE, sysfont, 1.1, nowrap=True)
#
# Set the desired temperature (SetTemp). UP increments, DOWN
# decrements, and START starts the temperatue control
#
def Desired():
global SetTemp
while START.value() == 1:
tft.fill(TFT.WHITE) # Clear screen
tft.rect((5, 10), (110, 100), TFT.RED)
tft.text((15, 30), "SET TEMP", TFT.BLACK, sysfont,
2, nowrap=True)
tft.text((15, 80), "SetTemp:{:.2f}C".format(SetTemp), TFT.BLUE,
sysfont, 1.1, nowrap=True)
while UP.value() == 1 and DOWN.value() == 1 and START.value() == 1:
pass
if UP.value() == 0:
SetTemp = SetTemp + 1
elif DOWN.value() == 0:
SetTemp = SetTemp - 1
Desired()
#
# Read the ambient temperature every 3 seconds and decide
# what to do
#
while True:
try:
pin = Pin(16, Pin.OUT, Pin.PULL_DOWN)
sensor = DHT11(pin)
t = (sensor.temperature) # Read temp
if SetTemp > t: # If greater
Relay.value(1) # Relay ON
LED.value(1) # ON
else:
Relay.value(0) # Relay OFF
LED.value(0) # LED OFF
Heading() # Heading
except: # Error reading
utime.sleep(10)
continue
utime.sleep(10)