import time
from time import sleep_ms, ticks_ms
import machine
import onewire, ds18x20
from machine import I2C, Pin
from i2c_lcd import I2cLcd 

AddressOfLcd = 0x27

i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
lcd.clear()
#Chatham D18B20 Temp Sensor
#lcd api and i2c files found from some dude
#Control system with LCD temp display. Switch toggles loop. User defined temperature range
#Outputs for a heat lamp and cooling fan modeled with LEDs

# using GPIO12 for temp data
dat = machine.Pin(12)

# required for one-wire library
ds = ds18x20.DS18X20(onewire.OneWire(dat))

# scan for devices on the bus
roms = ds.scan()
print('found devices:', roms)

# Define the switch as an input, activate internal pull up resistor
switch = Pin(4, Pin.IN, Pin.PULL_UP)

# Define output pin for high/low temp warning light
out = Pin(2, Pin.OUT)

# Define output pin for heat lamp, RED LED represents output high/low
lamp_out = Pin(19, Pin.OUT)

#Define output pin for cooling fan, BLUE LED represents output high/low
fan_out = Pin(14, Pin.OUT)

# Define temperature ranges. 
# Fan off, low warning on, lamp on when T <= NOMINAL_LOW_TEMP
NOMINAL_LOW_TEMP = 26

# Fan off, no warning message, lamp on when NOMINAL_LOW_TEMP < T <= NOMINAL_MID_TEMP
NOMINAL_MID_TEMP = 32

# Fan on, no warning message, lamp on when NOMINAL_MID_TEMP < T <= NOMINAL_HIGH_TEMP
# Fan on, high warning message on, lamp off when NOMINAL_HIGH_TEMP < T
NOMINAL_HIGH_TEMP = 40

#statement will run while switch is on
#LCD code amalgamized to display temp in C. 
#The following loop checks the range of T, if T too low, warning is printed, lamp turned on
#If T in low nominal range, shows nominal value, lamp stays on
#If T in high nominal range, shows nominal value, lamp stays on, fan turns on
#If T too high, warning printed, lamp turned off, fan stays on
while True:
    if switch.value() == 1:
        print('temperature deg C:', end=' ')
        ds.convert_temp()
        
        for rom in roms:
            print(ds.read_temp(rom), end=' ')
        print()
        if ds.read_temp(rom) <= NOMINAL_LOW_TEMP:
            lcd.clear()
            lcd.putstr('WARNING TEMP LOW' + 'T = ' + str(ds.read_temp(rom)) + ' C')
            out.on()
            fan_out.off()
            lamp_out.on()
        
        elif ds.read_temp(rom) <= NOMINAL_MID_TEMP:
            lcd.clear()
            lcd.putstr('Temp Nominal    ' + 'T = ' + str(ds.read_temp(rom)) + ' C')
            out.off()
            fan_out.off()
            lamp_out.on()

        elif ds.read_temp(rom) <= NOMINAL_HIGH_TEMP:
            lcd.clear()
            lcd.putstr('Temp Nominal    ' + 'T = ' + str(ds.read_temp(rom)) + ' C')
            out.off()
            fan_out.on()
            lamp_out.on()

        elif ds.read_temp(rom) > NOMINAL_HIGH_TEMP:
            lcd.clear()
            lcd.putstr('WARNING TEMP    HIGH ' + 'T = ' + str(ds.read_temp(rom)) + ' C')
            out.on()
            fan_out.on()
            lamp_out.off()
        
        time.sleep_ms(750)
   
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module