Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
idf_component_register(SRCS "dht.c"
INCLUDE_DIRS include
REQUIRES driver
PRIV_REQUIRES soc)
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# ESP32-DHT11
Esp-idf driver for DHT11 temperature and humidity sensor
Esp-idf driver for DHT11/22 temperature and humidity sensor

## Install
Use Platformio to install this [link](https://platformio.org/lib/show/5817/ESP32-DHT11)<br/>
or <br/>
Clone this repo inside [esp]/esp-idf/components folder

## How to use
Import dht11.h inside your program, initialize the device with DHT11_init(gpio_num) and then call DHT11_read() whenever you need to read from the DHT11 sensor.<br/>
Import dht.h inside your program, initialize the device with DHT11_init(gpio_num) or DHT22_init(gpio_num) and then call DHT_read() whenever you need to read from the DHT11/22 sensor.<br/>
DHT11_read() returns a struct with temperature and humidity and a status code of the operation for error checking.<br/>

Check the examples folder for more information.

<b>WARNING</b>: DHT11_read() is a blocking function.
<b>WARNING</b>: DHT_read() is a blocking function.
80 changes: 56 additions & 24 deletions dht11.c → dht.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,48 @@
*/

#include "esp_timer.h"
#include <esp_log.h>
#include "driver/gpio.h"
#include "rom/ets_sys.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "dht11.h"
#include "dht.h"

typedef enum{
MODE_DHT11,
MODE_DHT22
}dht_mode_t;

#define TAG "dht-drv"

static gpio_num_t dht_gpio;
static dht_mode_t dht_mode;
static int64_t last_read_time = -2000000;
static struct dht11_reading last_read;
static struct dht_reading last_read;

static int _waitOrTimeout(uint16_t microSeconds, int level) {
int micros_ticks = 0;
while(gpio_get_level(dht_gpio) == level) {
if(micros_ticks++ > microSeconds)
return DHT11_TIMEOUT_ERROR;
return DHT_TIMEOUT_ERROR;
ets_delay_us(1);
}
return micros_ticks;
}

static int _checkCRC(uint8_t data[]) {
if(data[4] == (data[0] + data[1] + data[2] + data[3]))
return DHT11_OK;
if(data[4] == ((data[0] + data[1] + data[2] + data[3])&0xFF))
return DHT_OK;
else
return DHT11_CRC_ERROR;
ESP_LOGD(TAG,"Crc error: %02x != %02x+%02x+%02x+%02x"
,data[4]
,data[0]
,data[1]
,data[2]
,data[3]
);
return DHT_CRC_ERROR;
}

static void _sendStartSignal() {
Expand All @@ -62,34 +78,44 @@ static void _sendStartSignal() {

static int _checkResponse() {
/* Wait for next step ~80us*/
if(_waitOrTimeout(80, 0) == DHT11_TIMEOUT_ERROR)
return DHT11_TIMEOUT_ERROR;
if(_waitOrTimeout(80, 0) == DHT_TIMEOUT_ERROR)
return DHT_TIMEOUT_ERROR;

/* Wait for next step ~80us*/
if(_waitOrTimeout(80, 1) == DHT11_TIMEOUT_ERROR)
return DHT11_TIMEOUT_ERROR;
if(_waitOrTimeout(80, 1) == DHT_TIMEOUT_ERROR)
return DHT_TIMEOUT_ERROR;

return DHT11_OK;
return DHT_OK;
}

static struct dht11_reading _timeoutError() {
struct dht11_reading timeoutError = {DHT11_TIMEOUT_ERROR, -1, -1};
static struct dht_reading _timeoutError() {
struct dht_reading timeoutError = {DHT_TIMEOUT_ERROR, -1, -1};
return timeoutError;
}

static struct dht11_reading _crcError() {
struct dht11_reading crcError = {DHT11_CRC_ERROR, -1, -1};
static struct dht_reading _crcError() {
struct dht_reading crcError = {DHT_CRC_ERROR, -1, -1};
return crcError;
}

void DHT11_init(gpio_num_t gpio_num) {
static void DHT_init(gpio_num_t gpio_num, dht_mode_t _dht_mode){
/* Wait 1 seconds to make the device pass its initial unstable status */
vTaskDelay(1000 / portTICK_PERIOD_MS);
dht_gpio = gpio_num;
dht_mode = _dht_mode;
}

struct dht11_reading DHT11_read() {
/* Tried to sense too son since last read (dht11 needs ~2 seconds to make a new read) */
void DHT11_init(gpio_num_t gpio_num) {
DHT_init(gpio_num, MODE_DHT11);
}

void DHT22_init(gpio_num_t gpio_num) {
DHT_init(gpio_num, MODE_DHT22);
}


struct dht_reading DHT_read() {
/* Tried to sense too son since last read (dht needs ~2 seconds to make a new read) */
if(esp_timer_get_time() - 2000000 < last_read_time) {
return last_read;
}
Expand All @@ -100,13 +126,13 @@ struct dht11_reading DHT11_read() {

_sendStartSignal();

if(_checkResponse() == DHT11_TIMEOUT_ERROR)
if(_checkResponse() == DHT_TIMEOUT_ERROR)
return last_read = _timeoutError();

/* Read response */
for(int i = 0; i < 40; i++) {
/* Initial data */
if(_waitOrTimeout(50, 0) == DHT11_TIMEOUT_ERROR)
if(_waitOrTimeout(50, 0) == DHT_TIMEOUT_ERROR)
return last_read = _timeoutError();

if(_waitOrTimeout(70, 1) > 28) {
Expand All @@ -115,10 +141,16 @@ struct dht11_reading DHT11_read() {
}
}

if(_checkCRC(data) != DHT11_CRC_ERROR) {
last_read.status = DHT11_OK;
last_read.temperature = data[2];
last_read.humidity = data[0];
if(_checkCRC(data) != DHT_CRC_ERROR) {
last_read.status = DHT_OK;
if (dht_mode == MODE_DHT11){
last_read.temperature = data[2];
last_read.humidity = data[0];
} else if (dht_mode == MODE_DHT22 ) {
last_read.temperature = ((data[2]<<8) + data[3])/10;
last_read.humidity = ((data[0]<<8) + data[1])/10;
} else {
};
return last_read;
} else {
return last_read = _crcError();
Expand Down
24 changes: 13 additions & 11 deletions include/dht11.h → include/dht.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,27 @@
* SOFTWARE.
*/

#ifndef DHT11_H_
#define DHT11_H_
#ifndef DHT_H_
#define DHT_H_

#include "driver/gpio.h"

enum dht11_status {
DHT11_CRC_ERROR = -2,
DHT11_TIMEOUT_ERROR,
DHT11_OK
enum dht_status {
DHT_CRC_ERROR = -2,
DHT_TIMEOUT_ERROR,
DHT_OK
};

struct dht11_reading {
struct dht_reading {
int status;
int temperature;
int humidity;
float temperature;
float humidity;
};

void DHT11_init(gpio_num_t);

struct dht11_reading DHT11_read();
void DHT22_init(gpio_num_t);

#endif
struct dht_reading DHT_read();

#endif