import utime
from machine import I2C, Pin, ADC
import onewire, ds18x20
from rotary import Rotary
import utime as time
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
pico_temp_threshold = 40 # Set the Pico temperature threshold in Celsius
rotary = Rotary(0,1,2)
SW = Pin(2, Pin.IN, Pin.PULL_UP)
set_temp = 75
current_temp= 0
F_current_temp= 0
ssr_pin = Pin(20, Pin.OUT)
I2C_ADDR = 39
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
sensor_data = ADC(4)
conversion_factor = 3.3/(65535)
V_REF = 3.3 # Reference voltage in volts
i2c = I2C(1, sda=machine.Pin(6), scl=machine.Pin(7), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
oneWireBus = ds18x20.DS18X20(onewire.OneWire(Pin(15)))
sensors = oneWireBus.scan()
if (len(sensors)==0):
print('DS18x20 device not found')
def display():
#lcd.clear()
lcd.move_to(0,0)
lcd.putstr("Current Tem:")
lcd.move_to(12,0)
lcd.putstr("{0:.0f}F".format(F_current_temp))
lcd.move_to(0,1)
lcd.putstr("Set Tem:")
lcd.move_to(10,1)
lcd.putstr("{0:.0f}F".format(set_temp))
def ds18x20():
global current_temp
global F_current_temp
oneWireBus.convert_temp()
time.sleep_ms(750)
for s in sensors:
current_temp = oneWireBus.read_temp(s)
F_current_temp = (current_temp * 1.8) + 32
print(F_current_temp)
time.sleep_ms(500)
def temp_camp():
global set_temp
pico_temp = read_pico_temperature()
# Check if temperature reading from Pico is valid
if pico_temp is None:
print("Unable to read Pico temperature.")
return
if pico_temp >= pico_temp_threshold:
ssr_pin.value(0)
print("Pico temperature above threshold. SSR turned off.")
return # Exit
# If Pico's temperature is below the threshold, proceed to check the set temperature
if F_current_temp <= set_temp:
print("SSR on")
ssr_pin.value(1)
elif F_current_temp >= set_temp + 1:
print("SSR off")
ssr_pin.value(0)
def read_pico_temperature():
sensor_voltage = sensor_data.read_u16() * conversion_factor
# Calculate temperature in degrees Celsius if the reading is within a reasonable range
if 0 <= sensor_voltage <= V_REF:
T = 27 - (sensor_voltage - 0.706)/0.001721
format_temp = "{:.2f}".format(T)
print(format_temp)
return float(format_temp)
else:
return None
def rotary_changed(change):
global set_temp
if change == Rotary.ROT_CW:
set_temp = set_temp - 1
if set_temp <=60:
set_temp= 60
print(set_temp)
elif change == Rotary.ROT_CCW:
set_temp = set_temp + 1
if set_temp >=120:
set_temp= 120
print(set_temp)
print(set_temp)
rotary.add_handler(rotary_changed)
while True:
ds18x20()
display()
temp_camp()
utime.sleep(.1)