File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed
micropython/drivers/sensor/dht Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ # DHT11/DHT22 driver for MicroPython on ESP8266
2+ # MIT license; Copyright (c) 2016 Damien P. George
3+
4+ import sys
5+
6+ if sys .platform .startswith ("esp" ):
7+ from esp import dht_readinto
8+ elif sys .platform == "mimxrt" :
9+ from mimxrt import dht_readinto
10+ elif sys .platform == "rp2" :
11+ from rp2 import dht_readinto
12+ elif sys .platform == "pyboard" :
13+ from pyb import dht_readinto
14+ else :
15+ from machine import dht_readinto
16+
17+
18+ class DHTBase :
19+ def __init__ (self , pin ):
20+ self .pin = pin
21+ self .buf = bytearray (5 )
22+
23+ def measure (self ):
24+ buf = self .buf
25+ dht_readinto (self .pin , buf )
26+ if (buf [0 ] + buf [1 ] + buf [2 ] + buf [3 ]) & 0xFF != buf [4 ]:
27+ raise Exception ("checksum error" )
28+
29+
30+ class DHT11 (DHTBase ):
31+ def humidity (self ):
32+ return self .buf [0 ]
33+
34+ def temperature (self ):
35+ return self .buf [2 ]
36+
37+
38+ class DHT22 (DHTBase ):
39+ def humidity (self ):
40+ return (self .buf [0 ] << 8 | self .buf [1 ]) * 0.1
41+
42+ def temperature (self ):
43+ t = ((self .buf [2 ] & 0x7F ) << 8 | self .buf [3 ]) * 0.1
44+ if self .buf [2 ] & 0x80 :
45+ t = - t
46+ return t
Original file line number Diff line number Diff line change 1+ module ("dht.py" , opt = 3 )
You can’t perform that action at this time.
0 commit comments