from machine import Pin, time_pulse_us, I2C
import dht
import time
import ssd1306
# ================== پینها ==================
dht_sensor = dht.DHT22(Pin(15))
led = Pin(2, Pin.OUT)
trig_a = Pin(5, Pin.OUT)
echo_a = Pin(18, Pin.IN)
trig_b = Pin(17, Pin.OUT)
echo_b = Pin(16, Pin.IN)
pir = Pin(4, Pin.IN)
# ================== OLED ==================
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
oled.fill(0)
oled.text("System Ready", 0, 0)
oled.show()
# ================== تنظیمات ==================
people_count = 0
detection_distance = 50
waiting_time_ms = 10000
cooldown_ms = 500
first_sensor = None
first_time = 0
last_event_time = -10000
motion_confirmed = False
# ================== منطق فن ==================
def decide_fan_speed(temperature, people):
if temperature is not None and temperature >= 30:
return (1023, "RED")
if people > 10 or (temperature is not None and temperature >= 28):
return (700, "YELLOW")
if people > 3 or (temperature is not None and temperature >= 25):
return (400, "GREEN")
return (0, "OFF")
# ================== نمایش OLED ==================
def show_on_oled(temperature, people, fan_speed):
fan_percent = int(fan_speed / 1023 * 100)
status = "ON" if fan_speed > 0 else "OFF"
oled.fill(0)
oled.text("Smart Ventilation", 0, 0)
oled.text("Temp: {}C".format(temperature if temperature else "--"), 0, 16)
oled.text("People: {}".format(people), 0, 32)
oled.text("Fan: {}% ({})".format(fan_percent, status), 0, 48)
oled.show()
# ================== شروع ==================
print("=== Smart Ventilation System Started ===")
while True:
# ---------- دما ----------
temperature = None
try:
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
print("Temperature:", temperature, "C | Humidity:", humidity, "%")
except Exception as e:
print("DHT22 error:", e)
# ---------- اولتراسونیک A ----------
distance_a = None
try:
trig_a.value(0)
time.sleep_us(2)
trig_a.value(1)
time.sleep_us(10)
trig_a.value(0)
dur_a = time_pulse_us(echo_a, 1, 30000)
if dur_a > 0:
d = dur_a * 0.0343 / 2
if d <= 400:
distance_a = d
except Exception:
pass
# ---------- اولتراسونیک B ----------
distance_b = None
try:
trig_b.value(0)
time.sleep_us(2)
trig_b.value(1)
time.sleep_us(10)
trig_b.value(0)
dur_b = time_pulse_us(echo_b, 1, 30000)
if dur_b > 0:
d = dur_b * 0.0343 / 2
if d <= 400:
distance_b = d
except Exception:
pass
sensor_a_active = (distance_a is not None and distance_a < detection_distance)
sensor_b_active = (distance_b is not None and distance_b < detection_distance)
# ---------- PIR ----------
if pir.value() == 1:
motion_confirmed = True
# ---------- منطق ورود/خروج ----------
now = time.ticks_ms()
if time.ticks_diff(now, last_event_time) >= cooldown_ms:
if first_sensor is None:
if sensor_a_active and not sensor_b_active:
first_sensor = "A"
first_time = now
motion_confirmed = False
elif sensor_b_active and not sensor_a_active:
first_sensor = "B"
first_time = now
motion_confirmed = False
else:
if time.ticks_diff(now, first_time) > waiting_time_ms:
first_sensor = None
motion_confirmed = False
elif first_sensor == "A" and sensor_b_active:
if motion_confirmed:
people_count += 1
print(">>> Person ENTERED! Total inside:", people_count)
first_sensor = None
last_event_time = now
motion_confirmed = False
elif first_sensor == "B" and sensor_a_active:
if motion_confirmed:
if people_count > 0:
people_count -= 1
print(">>> Person EXITED! Total inside:", people_count)
first_sensor = None
last_event_time = now
motion_confirmed = False
# ---------- فن و LED ----------
fan_speed, led_color = decide_fan_speed(temperature, people_count)
led.value(1 if fan_speed > 0 else 0)
# ---------- OLED ----------
show_on_oled(temperature, people_count, fan_speed)
# ---------- چاپ ----------
print("Sensor A:", distance_a, "cm | Sensor B:", distance_b, "cm")
print("PIR motion:", motion_confirmed)
print("People inside:", people_count)
print("Fan speed:", int(fan_speed / 1023 * 100), "% | LED:", led_color)
print("--------------------------------")
time.sleep_ms(100)Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306