Code
The code is used to read the temperature and humidity data of DHT11, and print them out.
C 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 C code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/C_Code/21.1.1_DHT11
2. Code of this project contains a custom header file. Use the following command to compile the code
DHT11.cpp and DHT.cpp and generate executable file DHT11. And the custom header file will be compiled
at the same time.
gcc DHT.cpp DHT11.cpp -o DHT11 -lwiringPi
3. Run the generated file "DHT11".
sudo ./DHT11
After the program is executed, the terminal window will display the current total number of reading times, the
read state, as well as the temperature and humidity value. As is shown below:
The following is the program code:
#include <wiringPi.h>
#include <stdio.h>
#include <stdint.h>
#include "DHT.hpp"
#define DHT11_Pin 0 //define the pin of sensor
int main( ){
DHT dht; //create a DHT class object
int chk, sumCnt;//chk:read the return value of sensor; sumCnt:times of reading sensor
if(wiringPiSetup() == -1){ //when initialize wiring failed, print message to screen
printf("setup wiringPi failed !");
return 1;
}
w hile(1){