from machine import Pin, I2C, PWM
import dht
import time
from SSD1306 import SSD1306_I2C

#Variables écran
WIDTH =128
HEIGHT= 64
i2c=I2C(0,scl=Pin(1),sda=Pin(0),freq=200000)
oled = SSD1306_I2C(WIDTH,HEIGHT,i2c)

#Variables Servo moteur
servo_pin = Pin(19, Pin.OUT)
servo = PWM(servo_pin, freq=50)#/hertz

# Initialiser les LEDs
ledr = Pin(2, Pin.OUT)
ledo = Pin(3, Pin.OUT)
ledv = Pin(4, Pin.OUT)

# Initialiser le capteur DHT22
temp = dht.DHT22(Pin(28))

while True:
    # Lire la température et l'humidité du capteur
    temp.measure()
    temperature = temp.temperature()
    
    # Éteindre toutes les LEDs initialement
    ledr.value(0)
    ledo.value(0)
    ledv.value(0)
    
    # Vérifier la température et allumer la LED correspondante
    if temperature >= 30:
        servo.duty_u16(1638)
        ledr.value(1)  # Allumer la LED rouge
        oled.text("Volets fermer",0 ,0)
        oled.show()
    elif temperature >= 20:
        servo.duty_u16(2900)
        ledo.value(1)  # Allumer la LED orange
        oled.text("Volets à moitié fermer",0 ,0)
        oled.show()
    else:
        servo.duty_u16(4780)
        ledv.value(1)  # Allumer la LED verte
        oled.text("Volets ouvert",0,0)
        oled.show()
    
    # Attendre 5 secondes avant la prochaine mesure
    time.sleep(1)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT