Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

c++ - How do DHT22 and gpio's work?

I just got myself a sensor DHT22 for temperature and humidity and I have some doubts: - Is this device only usable with arduino or Rpi? - If the answer is No, how does it work with the GPIO? As far as I understand, gpio has2 options: -Direcion (in or out) and Value (0 or 1). So according to this, I was checking a lot of examples for arduino and Rpi on how to use this devices, and all are attached to gpio's port. So how is this working? Does c/c++ has bigger capacity to handle this readings? - If the sensor can be used with more devices, is there any linux standard library/code to handle this device?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The DHT22 is, as you described, made to work over GPIO pins. That means that any device with GPIO can use it.

About the libraries. There are two major libraries for communicating via GPIO written, one for C++ and one for Python. That means that any device that has GPIO and can run Python or C++ can use your DHT22.

What about no C++, Python, Java... based libraries? Well, there is no Linux standard library for communicating over GPIO. Of course, if you are eager to learn and explore, you can write a library in Assembly or Bash, which would be as close to LSL as you can get. By doing that you can natively communicate over GPIO from Linux shell without any other programming language. If you are interested in Bash, take a look at this.

How does Linux know the temperature and humidity when it can only read 0s and 1s? Consider two things:

  1. How does computer store number 32? The computer only knows 0's and 1's so he needs to convert 32 to binary. What is 32 = 2^6 in decimal, is 100000 in binary.
  2. How does computer read temperature and humidity? When you execute a function like

    get_temperature()
    

    which requests temperature from DHT22, what actually happens is that your computer sends signal to DHT22 (it "writes" to DHT22 over GPIO through IN on DHT22, which is the same as OUT on computer). Then the DHT22 senses the temperature and "sends" data back. How does it send it? It creates a sequence of electric charges through GPIO (GPIO IN on computer and GPIO OUT on DHT22). In our case, if temperature is 32, then it sends the next sequence: 1-0-0-0-0-0. The computer reads the sequence and when you try to print it to screen it converts it into decimal.

Hope I got myself clear and this is what you wanted.

In this link you find a C code for reading DHT22 temperature and humidity if that is what you want. Please let me know what you want to do in general (read temp. and hum. via library, write new library for communicating over GPIO, write new library in another language for DHT22...).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...