import esp from machine import Pin class DHT11(object): def __init__(self, pin): self.pin = Pin(pin) self.buf = bytearray(5) def measure(self): buf = self.buf esp.dht_readinto(self.pin, buf) if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xff != buf[4]: raise Exception("checksum error") return buf def humi(self): return self.measure()[0] def temp(self): return self.measure()[2] def get(self): return [self.measure()[0], self.measure()[2]]
from DHT11 import DHT11
dht = DHT11(5)
dht.get()
dht.temp()
dht.humi()