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/

dht22.h

Committer:
co657_sjc80
Date:
2016-11-03
Revision:
2:e52d76ef24b3
Parent:
1:10ec58346011

File content as of revision 2:e52d76ef24b3:

/*
 * (C) The University of Kent and Simon Cooksey 2015.
 */
 
#ifndef __DHT22_h_
#define __DHT22_h_
 
// We'll pick a point to decide if a signal is 1 or 0 from. 
#define DHT22_SIGNAL_HIGH_LOW_BOUNDARY      40   // uS
#define DHT22_START_BIT_TIME                1000  // uS
#define DHT22_START_BIT_RESPONSE            80   // uS
 
#undef DEBUG_DHT22
 
typedef struct {
    int temp;
    int humidity;
    uint8_t checksum;
    char dummy[3];
} DHT22_data_t;
 
class DHT22 {
public:
    DHT22 (PinName pin) : dht22_s (pin)
#ifdef DEBUG_DHT22
                            , debug (PTB19)     /* GROT! -- hardwired for K64F */
#endif
    {
        dht22_s.input ();
        isinput = 1;
    }
    
    int read (DHT22_data_t *ptr);
private:
    DigitalInOut dht22_s;
    int isinput;
#ifdef DEBUG_DHT22
    DigitalOut debug;
#endif
 
    void wait_2us (void);
    void setinput (void);
    void setoutput (void);
    
    int wait_for_level (int lvl, const int max);
    void send_start (void);
    int wait_start (void);
    int read_byte (void);
};
 
#endif // __DHT22_h_