from machine import Pin, I2C
from time import sleep
import dht
from servo import Servo # ta bibliothèque perso
from led import LED # ta bibliothèque perso
from ssd1306 import SSD1306_I2C # pour l’écran OLED
# === Initialisation des composants ===
# Capteur DHT22
capteur = dht.DHT22(Pin(2))
# Servo moteur (broche GP16)
servo = Servo(16)
# Écran SSD1306 (128x64)
i2c = I2C(0, scl=Pin(1), sda=Pin(0))
oled = SSD1306_I2C(128, 64, i2c)
# Bargraph LEDs (C1→GP4 ... C10→GP13)
leds = [Pin(i, Pin.OUT) for i in range(4, 14)]
# Fonction pour éteindre toutes les LED
def eteindre_leds():
for led in leds:
led.value(0)
# Fonction pour allumer un certain nombre de LED
def allumer_leds(n):
eteindre_leds()
for i in range(min(n, len(leds))):
leds[i].value(1)
# === Boucle principale ===
while True:
capteur.measure()
temp = capteur.temperature()
hum = capteur.humidity()
print("Température:", temp, "°C | Humidité:", hum, "%")
# Efface écran
oled.fill(0)
if temp >= 30 and hum == 100:
allumer_leds(2)
servo.move(0) # reste à 0°
oled.text("Zero mouvement", 0, 0)
print("-> Zéro mouvement")
elif 20 < temp < 30 and hum == 100:
allumer_leds(3)
servo.move(90)
oled.text("Semi-ouvert", 0, 0)
print("-> Semi-ouvert")
elif temp < 10 and hum == 100:
allumer_leds(5)
servo.move(180)
oled.text("Ouvert", 0, 0)
print("-> Ouvert")
else:
eteindre_leds()
oled.text("Conditions neutres", 0, 0)
print("-> Aucune action")
oled.show()
sleep(2)