# Bibliothèque
from machine import Pin, I2C, PWM
from time import sleep
import dht
from ssd1306 import SSD1306_I2C
# Variables
capteur = dht.DHT22(Pin(6)) # DHT22 sur GP6
servo = PWM(Pin(20)) # Servo sur GP20
servo.freq(50)
# I2C pour l'écran OLED
I2 = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, I2)
# Bargraph LEDs (broches A1 à A10 dans le JSON)
bar = [
Pin(5, Pin.OUT), # A1
Pin(6, Pin.OUT), # A2
Pin(7, Pin.OUT), # A3
Pin(8, Pin.OUT), # A4
Pin(9, Pin.OUT), # A5
Pin(10, Pin.OUT), # A6
Pin(11, Pin.OUT), # A7
Pin(12, Pin.OUT), # A8
Pin(13, Pin.OUT), # A9
Pin(14, Pin.OUT) # A10
]
# Fonction pour convertir angle en signal PWM
def angle_to_duty(angle):
return int((angle / 180 * 2 + 0.5) / 20 * 65535)
# Boucle principale
while True:
capteur.measure()
temperature = capteur.temperature()
humidite = capteur.humidity()
# Éteindre toutes les LEDs du bargraph
for led in bar:
led.off()
oled.fill(0) # Effacer l'écran
if temperature >= 30 and humidite == 100:
for i in range(2):
bar[i].on()
servo.duty_u16(angle_to_duty(0))
oled.text("Zero mouvement", 0, 0)
elif 20 < temperature < 30 and humidite == 100:
for i in range(3):
bar[i].on()
servo.duty_u16(angle_to_duty(90))
oled.text("Semi-ouvert", 0, 0)
elif temperature < 10 and humidite == 100:
for i in range(5):
bar[i].on()
servo.duty_u16(angle_to_duty(180))
oled.text("Ouvert", 0, 0)
else:
oled.text("Conditions non", 0, 0)
oled.text("remplies", 0, 10)
oled.show()
sleep(2)