EasyManuals Logo

Freenove Ultimate Starter Kit User Manual

Freenove Ultimate Starter Kit
286 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #232 background imageLoading...
Page #232 background image
Chapter 21 Hygrothermograph DHT11
232
www.freenove.com
support@freenove.com
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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){

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Freenove Ultimate Starter Kit and is the answer not in the manual?

Freenove Ultimate Starter Kit Specifications

General IconGeneral
BrandFreenove
ModelUltimate Starter Kit
CategorySingle board computers
LanguageEnglish