from machine import Pin, I2C, ADC
import ssd1306, dht, time
# Inicialización I2C y OLED
i2c = I2C(0, scl=Pin(1), sda=Pin(0))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Sensor DHT22
sensor = dht.DHT22(Pin(2))
# Logo 16x8 (sin rotación)
logo = bytearray([
0x00, 0x00, 0x18, 0x3C, 0x7E, 0xDB, 0xFF, 0x7E,
0x3C, 0x18, 0x24, 0x18, 0x24, 0x42, 0x00, 0x00
])
# Relé (bomba)
rele = Pin(15, Pin.OUT)
# LDR como "sensor de humedad de suelo"
ldr = ADC(Pin(26)) # ADC0
# Umbral en % para activar bomba
UMBRAL_HUMEDAD = 70
def mostrar_logo():
oled.fill(0)
oled.text("VINKE$", 30, 0)
for y in range(8):
for x in range(16):
if (logo[x] >> (7 - y)) & 0x01:
oled.pixel(56 + x, 20 + y, 1)
oled.show()
def mostrar_datos(humedad_suelo, estado_bomba):
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
oled.fill(0)
oled.text("Temp: {:.1f}C".format(temp), 0, 0)
oled.text("Hum Air: {:.1f}%".format(hum), 0, 10)
oled.text("Hum Suelo:{:.0f}%".format(humedad_suelo), 0, 20)
oled.text("Bomba: {}".format("ON" if estado_bomba else "OFF"), 0, 30)
oled.text("Univ Kevin", 0, 50)
oled.show()
except OSError:
oled.fill(0)
oled.text("Error sensor", 0, 0)
oled.text("Reintentando...", 0, 10)
oled.show()
def controlar_bomba():
# Leer valor crudo 0-65535
valor = ldr.read_u16()
# Convertir a humedad % (invertido: más luz = menos humedad)
humedad_suelo = 100 - (valor / 65535 * 100)
if humedad_suelo < UMBRAL_HUMEDAD:
rele.value(1) # encender bomba
estado = True
else:
rele.value(0) # apagar bomba
estado = False
return humedad_suelo, estado
# Bucle principal
while True:
mostrar_logo()
time.sleep(2)
humedad_suelo, estado_bomba = controlar_bomba()
mostrar_datos(humedad_suelo, estado_bomba)
time.sleep(3)