A simple library for reading data from a DHT22 sensor

Dependents:   frdm_dht22_example DISCO-L053C8_ePD_demo

Simple library for doing DHT22 sensor readings. I've tested this on an FRDM K64F, but I see no reason it wouldn't work on other platforms.

Note that the data in `dht22_data_t` is fixed point, to convert to a normal number, divide by 10.

There is an example application here: https://developer.mbed.org/users/co657_sjc80/code/frdm_dht22_example/

Committer:
co657_sjc80
Date:
Thu Nov 03 14:06:39 2016 +0000
Revision:
2:e52d76ef24b3
Parent:
1:10ec58346011
Patches by Fred. Apparently the built in wait_us is a bit delicate, so Fred has added his own. Much nicer!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co657_sjc80 0:257ba13e416e 1 /*
co657_sjc80 0:257ba13e416e 2 * (C) The University of Kent and Simon Cooksey 2015.
co657_sjc80 0:257ba13e416e 3 */
co657_sjc80 2:e52d76ef24b3 4
co657_sjc80 0:257ba13e416e 5 #ifndef __DHT22_h_
co657_sjc80 0:257ba13e416e 6 #define __DHT22_h_
co657_sjc80 2:e52d76ef24b3 7
co657_sjc80 0:257ba13e416e 8 // We'll pick a point to decide if a signal is 1 or 0 from.
co657_sjc80 2:e52d76ef24b3 9 #define DHT22_SIGNAL_HIGH_LOW_BOUNDARY 40 // uS
co657_sjc80 2:e52d76ef24b3 10 #define DHT22_START_BIT_TIME 1000 // uS
co657_sjc80 0:257ba13e416e 11 #define DHT22_START_BIT_RESPONSE 80 // uS
co657_sjc80 2:e52d76ef24b3 12
co657_sjc80 2:e52d76ef24b3 13 #undef DEBUG_DHT22
co657_sjc80 2:e52d76ef24b3 14
co657_sjc80 0:257ba13e416e 15 typedef struct {
co657_sjc80 0:257ba13e416e 16 int temp;
co657_sjc80 0:257ba13e416e 17 int humidity;
co657_sjc80 0:257ba13e416e 18 uint8_t checksum;
co657_sjc80 2:e52d76ef24b3 19 char dummy[3];
co657_sjc80 0:257ba13e416e 20 } DHT22_data_t;
co657_sjc80 2:e52d76ef24b3 21
co657_sjc80 0:257ba13e416e 22 class DHT22 {
co657_sjc80 0:257ba13e416e 23 public:
co657_sjc80 2:e52d76ef24b3 24 DHT22 (PinName pin) : dht22_s (pin)
co657_sjc80 2:e52d76ef24b3 25 #ifdef DEBUG_DHT22
co657_sjc80 2:e52d76ef24b3 26 , debug (PTB19) /* GROT! -- hardwired for K64F */
co657_sjc80 2:e52d76ef24b3 27 #endif
co657_sjc80 2:e52d76ef24b3 28 {
co657_sjc80 2:e52d76ef24b3 29 dht22_s.input ();
co657_sjc80 2:e52d76ef24b3 30 isinput = 1;
co657_sjc80 2:e52d76ef24b3 31 }
co657_sjc80 2:e52d76ef24b3 32
co657_sjc80 2:e52d76ef24b3 33 int read (DHT22_data_t *ptr);
co657_sjc80 0:257ba13e416e 34 private:
co657_sjc80 0:257ba13e416e 35 DigitalInOut dht22_s;
co657_sjc80 2:e52d76ef24b3 36 int isinput;
co657_sjc80 2:e52d76ef24b3 37 #ifdef DEBUG_DHT22
co657_sjc80 2:e52d76ef24b3 38 DigitalOut debug;
co657_sjc80 2:e52d76ef24b3 39 #endif
co657_sjc80 2:e52d76ef24b3 40
co657_sjc80 2:e52d76ef24b3 41 void wait_2us (void);
co657_sjc80 2:e52d76ef24b3 42 void setinput (void);
co657_sjc80 2:e52d76ef24b3 43 void setoutput (void);
co657_sjc80 2:e52d76ef24b3 44
co657_sjc80 2:e52d76ef24b3 45 int wait_for_level (int lvl, const int max);
co657_sjc80 2:e52d76ef24b3 46 void send_start (void);
co657_sjc80 2:e52d76ef24b3 47 int wait_start (void);
co657_sjc80 2:e52d76ef24b3 48 int read_byte (void);
co657_sjc80 0:257ba13e416e 49 };
co657_sjc80 2:e52d76ef24b3 50
co657_sjc80 0:257ba13e416e 51 #endif // __DHT22_h_
co657_sjc80 2:e52d76ef24b3 52