In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how the raspberry pie uses the DHT11 temperature and humidity sensor. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
1. Relevant introduction
DHT11 introduction:
DHT11 is a relatively cheap temperature and humidity sensor module. Only one IO port is needed to read the data. Can measure temperature and relative humidity at the same time.
It should be noted that a sentence is written in the document: the decimal part is used for expansion, and now only 0 can be read. So the decimal part is always 0 at present!
IO introduction of raspberry pie
I use B version of raspberry pie, and the interface is shown below. The NAME column is the actual IO port function of the raspberry pie. The PIN# column is the interface number used for programming the wiringpi and pi4j library files that will be described later.
Wiringpi introduction
Wiringpi is a header file that controls the GPIO port of raspberry pie through the C language. After including this header file in C language, you can easily call the encapsulated method to control the raspberry pie GPIO port. The program needs to be installed first.
Wiringpi official website: http://wiringpi.com/
Download and install wiringpi: http://wiringpi.com/download-and-install/
Wiringpi documents: http://wiringpi.com/reference/
Compile and run:
After writing the C file, compile with the following command:
Gcc-Wall-o executefilename cfilename.c-lwiringPi
Gcc is the compiler,-Wall displays a warning message at compile time,-o executefilename cfilename.c compiles the cfilename.c file into an executable file named executefilename, and-lwiringPi includes the wiringPi header file in the executable.
After compilation, a file named executefilename is generated, which can be run by executing the following command with root permissions:
Sudo. / executefilename
Pi4j introduction
Pi4j is a library file developed based on wiringpi to control the GPIO port of raspberry pie through java. By introducing related classes into the java program, you can use the packaged method to control the raspberry pie GPIO port.
Pi4j official website: http://pi4j.com/
Pi4j download: http://pi4j.com/download.html
Pi4j installation: http://pi4j.com/install.html
Pi4j documents: http://pi4j.com/apidocs/index.html
Compile and run:
After using pi4j, you need to use root permissions when compiling .java files and running .class files. For example:
Compile:
Sudo javac-classpath.: classes:/opt/pi4j/lib/'*' YourJavaFile.java
Run:
Sudo java-classpath.: classes:/opt/pi4j/lib/'*' YourClassFile.class
If you use sudo compilation and runtime prompts that javac and java cannot be found, this is because the root user does not load environment variables, so the execution command for java cannot be found. You can use the following command to switch to the root user with environment variables before compiling and running:
Sudo su-root
II. Hardware connection
Because I am lazy, and I am a novice at present using ubuntu, I don't know what software to use to do the drawing, so I use words to briefly describe the hardware connection.
The DHT11 pin with grid space is the front, the pin is facing down, and the four pins are counted from the left
DHT11 pin name
DHT11 pin function
Connect to the GPIO of raspberry pie
1.VCC
Positive, connected to 3.3V or 5V
Physical interface 1, that is 3.3V
2.DATA
Data input and output
Physical interface 7, or GPIO 7
3.NC
Hanging in the air (that is, not connecting anywhere)
Do not connect
4.GND
Negative electrode
Physical interface 6, namely GND
In addition, according to the requirements of the DHT11 data manual, I connected a 4.7 K ohm resistor between DATA and VCC to play a pull-up effect.
III. Software programming
Use wiringpi to write C programs:
The following procedure draws lessons from the program of a foreigner on the Internet and forgets where the address of the original post is.
# include # define MAX_TIME 85#define DHT11PIN 7#define ATTEMPTS 5 / / retry 5 times when no responseint dht11_val [5] = {0 uint8_t lststate=HIGH; / / last state uint8_t counter=0; uint8_t jag0 times when no responseint dht11_val 0}; int dht11_read_val () {uint8_t lststate=HIGH; / / last state uint8_t counter=0; uint8_t jade 0 Magna I; for (iMag0) The first three times are: 1 low level, 2 high level (that is, the response signal), 3 low level (that is, the first low level of the data) / / I% 2 voltage 0 because the low level and high level are read in a loop each time. It takes two cycles to read out one bit process dht11_ [j / 8] = 40) & & (dht11_val [4] = ((dht11_val [0] + dht11_val [1] + dht11_val [2] + dht11_val [3]) & 0xFF)) {/ / where (dht11_val [0] + dht11_val [1] + dht11_val [2] + dht11_val [3]) & 0xFF) is the addition of five numbers, and 1 and. / / the purpose is to prevent the readout data from being 0, 0, 0, 0, 1 and 0, so the if judgment condition is not valid, and the read failure code is returned. / / if the readout data is normal data that is not zero, and 1 and then get the original number.
Write java programs using pi4j:
When using pi4j to write the timing required by DHT11, there is no DHT11 response signal all the time, and I feel that java can not reach the microsecond timing required by DHT11, so the java program is not successful.
This is the end of the article on "how raspberry pie uses DHT11 temperature and humidity sensors". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.