|
META TOPICPARENT |
name="LectureSlides" |
Slide 1: DHT11 Temperature and Humidity Sensor |
| Let's start slowly:
- Initiating a measurement seems do-able
- Why not just read the data from the device every 5 µs
and print out the information?
|
|
< < | The zeros and ones do not look too convincing. Let’s save those onto a file and look at it with gnuplot
|
> > | The zeros and ones do not look too convincing. Let’s save those onto a file and look at it with gnuplot ---++
What the user of the device wants
The user of the device would like to have a library which hides
all these details. He wants functions to
- Initialize the device
- Start a measurement
- Make sure the checksum is ok
- Read temperature and humidity values
- Maybe know when the measurement was made
The library
How do we write a library?
A library consist of min. 2 files:
- an include file (dht11.h)
- and implementation file (dht11.c)
Very often you have a single include but several implementation files.
The include file of the dht11 library
dht11.h (2)
The library code
- dht11Init simply calls wiringPiSetup()
- dht11Measurement
- Programs the gpio pin as output and sends the start sequence
- Switches back the gpio pin to input and reads the data coming from the dht11 every 5 µs and saves the data into an array
- Analyses the data and extracts temperature, humidity and the checksum
- Calculates the checksum and compares it with the one coming from the device
- If checksums are ok, sends the data to “validData”
- Keeps the time stamp of the measurement
Reading the final data
- dht11GetValidTemperature()
- dht11GetValidHumidity()
- dht11getValidMeasTime()
- dht11getChecksum()
- dht11getDeviceChecksum()
Starting the measurement
The data pin has been programmed as output earlier
You can print these data and have a look at them with gnuplot. Try analyzing be hand.
Analyzing the data
This is the tricky bit and you are invited to give it a try. I have written a
routine reading a single bit, which is called 40 times for all the 40 bits in the data.
Please note one nasty problem: Linux is a multi-tasking system and permanently receives interrupts.
While these interrupts are treated the DHT11 data are continuing to flow
but are not taking into account, leading to corrupt data.
Since the duration of the protocol takes ~ 4 ms the probability is non negligible
and the checksum test in obligatory to make sure the data are consistent.
In case of a checksum error you must repeat the measurement
Creating a shared library
The Makefile says it all
 |
|
|
|
> > | Documentation
When writing a library that is supposed to be used by other people,
documentation is of utmost importance
Of course the function prototypes in the include file give
some indication but this normally not enough.
I use the doxygen in code documentation system which allows to
generate documentation from the source layout and
comments in the code with special tags.
It creates html of latex doc |
|
%SLIDESHOWEND% |
|
META FILEATTACHMENT |
attachment="twoWay.png" attr="" comment="" date="1508516903" name="twoWay.png" path="twoWay.png" size="32717" user="uli" version="1" |
META FILEATTACHMENT |
attachment="zero.png" attr="" comment="" date="1508516904" name="zero.png" path="zero.png" size="185952" user="uli" version="1" |
META FILEATTACHMENT |
attachment="dht11.png" attr="" comment="" date="1508517246" name="dht11.png" path="dht11.png" size="612270" user="uli" version="1" |
|
|
> > |
META FILEATTACHMENT |
attachment="dht11H-1.png" attr="" comment="" date="1508766477" name="dht11H-1.png" path="dht11H-1.png" size="25351" user="uli" version="1" |
META FILEATTACHMENT |
attachment="dht11H-2.png" attr="" comment="" date="1508766477" name="dht11H-2.png" path="dht11H-2.png" size="22372" user="uli" version="1" |
META FILEATTACHMENT |
attachment="measurement.png" attr="" comment="" date="1508766902" name="measurement.png" path="measurement.png" size="30589" user="uli" version="1" |
META FILEATTACHMENT |
attachment="shareLib.png" attr="" comment="" date="1508768720" name="shareLib.png" path="shareLib.png" size="28547" user="uli" version="1" |
|