Chapter 21 Hygrothermograph DHT11
printf("DHTLIB_ERROR_TIMEOUT! \n");
b r eak;
case DHTLIB_INVALID_VALUE: //other errors
printf("DHTLIB_INVALID_VALUE! \n");
b r eak;
}
Finally print the results:
printf("Humidity is %.2f %%, \t Temperature is %.2f *C\n\n",dht.humidity,dht.temperature);
Library file "DHT.hpp" contains a DHT class and his public member functions int readDHT11 (int pin) is used
to read sensor DHT11 and store the temperature and humidity data read to member variables double
humidity and temperature. The implementation method of the function is included in the file "DHT.cpp".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <wiringPi.h>
#include <stdio.h>
#include <stdint.h>
////read return flag of sensor
#define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1
#define DHTLIB_ERROR_TIMEOUT -2
#define DHTLIB_INVALID_VALUE -999
#define DHTLIB_DHT11_WAKEUP 18
#define DHTLIB_DHT_WAKEUP 1
#define DHTLIB_TIMEOUT 100
class DHT{
public:
double humidity,temperature; //use to store temperature and humidity data read
int readDHT11(int pin); //read DHT11
private:
int bits[5]; //Buffer to receiver data
int readSensor(int pin,int wakeupDelay); //
};
Python Code 21.1.1 DHT11
First observe the project result, and then analyze the code.
1. Use cd command to enter 21.1.1_DHT11 directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/21.1.1_DHT11
2. Use python command to execute code "DHT11.py".
python DHT11.py
After the program is executed, the terminal window will display the current total number of read, the read
state, as well as the temperature and humidity value. As is shown below: