import network
import time
import ujson
from umqtt.simple import MQTTClient
import machine
import dht
from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from time import sleep
I2C_ADDR = 0x27
totalRows = 2
totalColummns = 16
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000)
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColummns)
sensor = dht.DHT22(Pin(2))
led_red=Pin(23,Pin.OUT)
led_blue=Pin(19,Pin.OUT)
firsttemp = 0
firsthumidity = 0
while True:
try:
sleep(2)
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
if temp > 10 and temp < 30:
led_red.value(1)
else:
led_red.value(0)
if hum > 20 and hum < 60:
led_blue.value(1)
else:
led_blue.value(0)
if temp!=firsttemp or hum!=firsthumidity:
lcd.clear()
lcd.putstr("Temp: %3.1f C\n" %temp)
lcd.putstr("Hum: %3.1f %%" %hum)
firsttemp=temp
firsthumidity=hum
except OSError as e:
print('lectura fallida del sensor')