import machine
import time
import dht
from ssd1306 import SSD1306_I2C
# ==========================================
# 1. PARAMÈTRES ET SEUILS
# ==========================================
TEMP_SEUIL = 35.0
HUMIDITE_SEUIL = 80.0
GAZ_SEUIL = 25000
# VARIABLES
current_screen = 1
alarme_active = False
# ==========================================
# 2. CONFIGURATION DU MATÉRIEL
# ==========================================
i2c = machine.I2C(0, sda=machine.Pin(8), scl=machine.Pin(9), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
dht_z1 = dht.DHT22(machine.Pin(16))
dht_z2 = dht.DHT22(machine.Pin(17))
mq2 = machine.ADC(machine.Pin(26))
led_r = machine.Pin(10, machine.Pin.OUT)
led_g = machine.Pin(11, machine.Pin.OUT)
led_b = machine.Pin(12, machine.Pin.OUT)
servo = machine.PWM(machine.Pin(13))
servo.freq(50)
buzzer = machine.PWM(machine.Pin(18))
btn_nav = machine.Pin(14, machine.Pin.IN)
btn_reset = machine.Pin(15, machine.Pin.IN)
print("ATLAS lance !")
# ==========================================
# 3. BOUCLE PRINCIPALE
# ==========================================
while True:
try:
# LECTURE DES CAPTEURS
dht_z1.measure()
t1 = dht_z1.temperature()
h1 = dht_z1.humidity()
dht_z2.measure()
t2 = dht_z2.temperature()
h2 = dht_z2.humidity()
val_gaz = mq2.read_u16()
# LECTURE DES BOUTONS (SANS INTERRUPTIONS)
if btn_nav.value() == 0:
current_screen = current_screen + 1
if current_screen > 3:
current_screen = 1
time.sleep_ms(200)
if btn_reset.value() == 0:
alarme_active = False
time.sleep_ms(200)
# LOGIQUE D'ALERTE
if t1 > TEMP_SEUIL or t2 > TEMP_SEUIL or val_gaz > GAZ_SEUIL:
alarme_active = True
# ACTIONS MATÉRIEL
if alarme_active == True:
led_r.value(1)
led_g.value(0)
led_b.value(0)
buzzer.freq(1200)
buzzer.duty_u16(32768)
servo.duty_u16(4800)
else:
led_r.value(0)
led_g.value(1)
led_b.value(0)
buzzer.duty_u16(0)
servo.duty_u16(1500)
# GESTION DE L'ÉCRAN OLED
oled.fill(0)
if current_screen == 1:
oled.text("1. TEMPERATURES", 0, 0)
oled.text("Zone 1: {} C".format(t1), 0, 24)
oled.text("Zone 2: {} C".format(t2), 0, 44)
elif current_screen == 2:
oled.text("2. HUMIDITES", 0, 0)
oled.text("Zone 1: {} %".format(h1), 0, 24)
oled.text("Zone 2: {} %".format(h2), 0, 44)
elif current_screen == 3:
oled.text("3. QUALITE AIR", 0, 0)
oled.text("Gaz: {}".format(val_gaz), 0, 24)
if val_gaz > GAZ_SEUIL:
oled.text("DANGER !", 0, 44)
else:
oled.text("Statut: OK", 0, 44)
oled.show()
except OSError:
pass
time.sleep(0.2)