import machine
import ssd1306
import dht
import time

# راه‌اندازی I2C و OLED
i2c = machine.I2C(0, scl=machine.Pin(1), sda=machine.Pin(0))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

# سنسور DHT22 روی GPIO2
sensor = dht.DHT22(machine.Pin(2))

# ADC برای پتانسیومتر (رطوبت خاک) روی GPIO26 (ADC0)
pot = machine.ADC(26)

# LED نشانگر آبیاری روی GPIO16
led = machine.Pin(16, machine.Pin.OUT)

while True:
    sensor.measure()
    temp_c = sensor.temperature()
    humidity = sensor.humidity()
    pot_val = pot.read_u16()
    soil_moisture = pot_val / 65535 * 100

    # منطق آبیاری اتوماتیک
    if (temp_c > 23) and (humidity < 60) and (soil_moisture < 30):
        led.value(1)  # روشن شدن چراغ (شیر آب باز)
        irrigation_status = "ON"
    else:
        led.value(0)  # خاموش شدن چراغ (شیر آب بسته)
        irrigation_status = "OFF"

    # نمایش اطلاعات روی OLED
    oled.fill(0)
    oled.text("Temp: {:.1f} C".format(temp_c), 0, 0)
    oled.text("Humidity: {:.1f}%".format(humidity), 0, 15)
    oled.text("Soil: {:.1f}%".format(soil_moisture), 0, 30)
    oled.text("Irrigation: {}".format(irrigation_status), 0, 45)
    oled.show()

    time.sleep(2)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT