from machine import Pin, ADC
import dht
import time
# --- 1. INITIALISATION DES CAPTEURS ---
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB)
solaire = ADC(Pin(35))
solaire.atten(ADC.ATTN_11DB)
capteur = dht.DHT22(Pin(14))
bouton = Pin(13, Pin.IN, Pin.PULL_UP)
# --- 2. INITIALISATION DES ACTIONNEURS ---
led_lampe = Pin(27, Pin.OUT)
led_vert = Pin(26, Pin.OUT)
led_rouge = Pin(25, Pin.OUT)
led_bleue = Pin(23, Pin.OUT)
SEUIL_LUMIERE = 1500
compteur = 0
# --- 3. BOUCLE DE TRAITEMENT ---
while True:
# Lecture capteurs
lumiere = ldr.read()
presence = bouton.value()
try:
capteur.measure()
temp = capteur.temperature()
humid = capteur.humidity()
except OSError:
temp = 25
humid = 50
valeur_brute = solaire.read()
watts = (valeur_brute / 4095) * 100
# Affichage mesures
print("Luminosite :", lumiere)
print("Temperature =", temp, "C | Humidite =", humid, "%")
print("Production solaire :", round(watts, 1), "W")
# Logique eclairage (lampe)
if lumiere < SEUIL_LUMIERE and presence == 0:
led_lampe.on()
print("Lampe allumee")
else:
led_lampe.off()
print("Lampe eteinte")
# Calcul consommation
conso = 0
if led_lampe.value() == 1:
conso += 10
if temp > 28:
conso += 20
print("Climatisation active +20W")
print("Consommation totale =", conso, "W")
# Comparaison production vs consommation
if watts >= conso:
led_vert.on()
led_rouge.off()
print("Equilibre energetique")
else:
led_vert.off()
led_rouge.on()
print("X Surconsommation !")
# Detection excedent
if watts > conso + 20:
led_bleue.on()
print("Excedent d'energie - peut alimenter le quartier !")
else:
led_bleue.off()
# Communication urbaine
compteur += 1
if compteur % 5 == 0:
print("Donnees transmises au gestionnaire urbain")
print("-" * 35)
time.sleep(2)Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
ldr
Ahmed Mabrouk
Potentiomètre
DHT11