from machine import Pin, SPI
import time
import random
import ubinascii
import onewire
import ds18x20
import ILI9341
from machine import ADC
desired_temperature = 0.0
relay = Pin(16, Pin.OUT)
led_heating = Pin(15, Pin.OUT)
led_cooling = Pin(14, Pin.OUT)
potentiometer = ADC(Pin(26))
pin = Pin(21)
data_pin = Pin(21)
ow = onewire.OneWire(data_pin)
roms = ow.scan()
ds = ds18x20.DS18X20(ow)
print('sensor DS18B20 exist :', roms)
spi = SPI(1, baudrate=10000000, polarity=0, phase=0)
cs = Pin(28, Pin.OUT)
dc = Pin(22, Pin.OUT)
reset = Pin(27, Pin.OUT)
display = ILI9341.ILI9341(spi, cs, dc, reset)
display.init()
def simulate_temperature():
return random.uniform(18.0, 30.0)
def adjust_temperature():
global desired_temperature
pot_value = potentiometer.read_u16()
desired_temperature = 18 + (pot_value / 65535) * (30 - 18)
print(f"Desired temperature: {desired_temperature:.2f}°C")
def control(temperature):
try:
display.fill(ILI9341.BLACK)
if temperature < desired_temperature:
display.text("Heating", 10, 10, ILI9341.RED)
print(f"Temperature {temperature:.2f}°C: Heating ")
led_heating.on()
led_cooling.off()
relay.on()
elif temperature > desired_temperature:
display.text("Cooling", 10, 10, ILI9341.BLUE)
print(f"Temperature {temperature:.2f}°C: Cooling")
led_cooling.on()
led_heating.off()
relay.off()
else:
display.text("Stable", 10, 10, ILI9341.GREEN)
print(f"Temperature {temperature:.2f}°C: Stable")
led_heating.off()
led_cooling.off()
relay.off()
except Exception as e:
print(f"Erreur dans control(): {e}")
def display_temperature(temperature):
try:
display.fill(ILI9341.BLACK)
display.text(f"T: {temperature:.2f}°C", 10, 40, ILI9341.WHITE)
except Exception as e:
print(f"Erreur dans display_temperature: {e}")
temperature = 0.0
while True:
try:
ds.convert_temp() # Start temperature conversion
time.sleep_ms(750) # Wait for conversion to complete
for rom in roms:
temperature = ds.read_temp(rom)
print('Temperature:', temperature, '°C')
adjust_temperature()
display_temperature(temperature)
control(temperature)
time.sleep(2)
except Exception as e:
print(f"An error occurred: {e}")