Chapter 21 Hygrothermograph DHT11
236
31
32
33
e x cept
KeyboardInterrupt
:
GPIO
.
cleanup
()
exit
( )
In this project code, we use a module "
Freenove_DHT
.py", which provide method of reading sensor DHT. It
is located in the same directory with program files "
DHT11
.py". By using this library, we can easily read the
DHT sensor. First create a DHT class object in the code.
dht
=
DHT
.
DHT
(
DHTPin
)
#create a DHT class object
And then in the "while" cycle, use chk = dht.
readDHT11
(DHT11Pin) to read the DHT11, and determine
whether the data read is normal according to the return value "chk". And then use variable sumCnt to record
number of reading times.
w h ile
(
True
):
sumCnt
+ =
1
#counting number of reading times
chk
=
dht
.
readDHT11
(
DHTPin
)
#read DHT11 and get a return value. Then
determine whether data read is normal according to the return value.
p r int
(
"The sumCnt is : %d, \t chk : %d"
% (
sumCnt
,
chk
))
i f
(
chk
i s
dht
.
DHTLIB_OK
):
#read DHT11 and get a return value. Then
determine whether data read is normal according to the return value.
p r int
(
"DHT11,OK!"
)
e l if
(
chk
i s
dht
.
DHTLIB_ERROR_CHECKSUM
):
#data check has errors
p r int
(
"DHTLIB_ERROR_CHECKSUM!!"
)
e l if
(
chk
i s
dht
.
DHTLIB_ERROR_TIMEOUT
):
#reading DHT times out
p r int
(
"DHTLIB_ERROR_TIMEOUT!"
)
e l se
:
#other errors
p r int
(
"Other error!"
)
Finally print the results:
p r int
(
"Humidity : %.2f, \t Temperature : %.2f \n"
% (
dht
.
humidity
,
dht
.
temperature
))
Module "
Freenove_DHT.py
" contains a DHT class. And class functions def
readDHT11
(pin) is used to read
sensor DHT11 and store the temperature and humidity data read to member variables humidity and
temperature.
Freenove_DHT Module
This is a Python module for reading the temperature and humidity data of the DHT sensor. Partial
functions and variables are described as follows:
Variable
humidity
: store humidity data read from sensor
Variable
temperature
: store temperature data read from sensor
def
readDHT11
(pin)
: read the temperature and humidity of sensor DHT11, and return values used to
determine whether the data is normal.
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...