├── .gitignore ├── LICENSE ├── README.md ├── component.mk ├── dht11.c ├── examples └── simple_read.c ├── include └── dht11.h └── library.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Michele Biondi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32-DHT11 2 | Esp-idf driver for DHT11 temperature and humidity sensor 3 | 4 | ## Install 5 | Use Platformio to install this [link](https://platformio.org/lib/show/5817/ESP32-DHT11)
6 | or
7 | Clone this repo inside [esp]/esp-idf/components folder 8 | 9 | ## How to use 10 | 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.
11 | DHT11_read() returns a struct with temperature and humidity and a status code of the operation for error checking.
12 | 13 | Check the examples folder for more information. 14 | 15 | WARNING: DHT11_read() is a blocking function. 16 | -------------------------------------------------------------------------------- /component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Main Makefile. This is basically the same as a component makefile. 3 | # -------------------------------------------------------------------------------- /dht11.c: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Michele Biondi 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "esp_timer.h" 26 | #include "driver/gpio.h" 27 | #include "rom/ets_sys.h" 28 | #include "freertos/FreeRTOS.h" 29 | #include "freertos/task.h" 30 | 31 | #include "dht11.h" 32 | 33 | static gpio_num_t dht_gpio; 34 | static int64_t last_read_time = -2000000; 35 | static struct dht11_reading last_read; 36 | 37 | static int _waitOrTimeout(uint16_t microSeconds, int level) { 38 | int micros_ticks = 0; 39 | while(gpio_get_level(dht_gpio) == level) { 40 | if(micros_ticks++ > microSeconds) 41 | return DHT11_TIMEOUT_ERROR; 42 | ets_delay_us(1); 43 | } 44 | return micros_ticks; 45 | } 46 | 47 | static int _checkCRC(uint8_t data[]) { 48 | if(data[4] == (data[0] + data[1] + data[2] + data[3])) 49 | return DHT11_OK; 50 | else 51 | return DHT11_CRC_ERROR; 52 | } 53 | 54 | static void _sendStartSignal() { 55 | gpio_set_direction(dht_gpio, GPIO_MODE_OUTPUT); 56 | gpio_set_level(dht_gpio, 0); 57 | ets_delay_us(20 * 1000); 58 | gpio_set_level(dht_gpio, 1); 59 | ets_delay_us(40); 60 | gpio_set_direction(dht_gpio, GPIO_MODE_INPUT); 61 | } 62 | 63 | static int _checkResponse() { 64 | /* Wait for next step ~80us*/ 65 | if(_waitOrTimeout(80, 0) == DHT11_TIMEOUT_ERROR) 66 | return DHT11_TIMEOUT_ERROR; 67 | 68 | /* Wait for next step ~80us*/ 69 | if(_waitOrTimeout(80, 1) == DHT11_TIMEOUT_ERROR) 70 | return DHT11_TIMEOUT_ERROR; 71 | 72 | return DHT11_OK; 73 | } 74 | 75 | static struct dht11_reading _timeoutError() { 76 | struct dht11_reading timeoutError = {DHT11_TIMEOUT_ERROR, -1, -1}; 77 | return timeoutError; 78 | } 79 | 80 | static struct dht11_reading _crcError() { 81 | struct dht11_reading crcError = {DHT11_CRC_ERROR, -1, -1}; 82 | return crcError; 83 | } 84 | 85 | void DHT11_init(gpio_num_t gpio_num) { 86 | /* Wait 1 seconds to make the device pass its initial unstable status */ 87 | vTaskDelay(1000 / portTICK_PERIOD_MS); 88 | dht_gpio = gpio_num; 89 | } 90 | 91 | struct dht11_reading DHT11_read() { 92 | /* Tried to sense too son since last read (dht11 needs ~2 seconds to make a new read) */ 93 | if(esp_timer_get_time() - 2000000 < last_read_time) { 94 | return last_read; 95 | } 96 | 97 | last_read_time = esp_timer_get_time(); 98 | 99 | uint8_t data[5] = {0,0,0,0,0}; 100 | 101 | _sendStartSignal(); 102 | 103 | if(_checkResponse() == DHT11_TIMEOUT_ERROR) 104 | return last_read = _timeoutError(); 105 | 106 | /* Read response */ 107 | for(int i = 0; i < 40; i++) { 108 | /* Initial data */ 109 | if(_waitOrTimeout(50, 0) == DHT11_TIMEOUT_ERROR) 110 | return last_read = _timeoutError(); 111 | 112 | if(_waitOrTimeout(70, 1) > 28) { 113 | /* Bit received was a 1 */ 114 | data[i/8] |= (1 << (7-(i%8))); 115 | } 116 | } 117 | 118 | if(_checkCRC(data) != DHT11_CRC_ERROR) { 119 | last_read.status = DHT11_OK; 120 | last_read.temperature = data[2]; 121 | last_read.humidity = data[0]; 122 | return last_read; 123 | } else { 124 | return last_read = _crcError(); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /examples/simple_read.c: -------------------------------------------------------------------------------- 1 | /* Simple Read Example 2 | 3 | This example code is in the Public Domain (or CC0 licensed, at your option.) 4 | 5 | Unless required by applicable law or agreed to in writing, this 6 | software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 7 | CONDITIONS OF ANY KIND, either express or implied. 8 | */ 9 | #include "freertos/FreeRTOS.h" 10 | #include "freertos/task.h" 11 | #include "driver/gpio.h" 12 | #include "sdkconfig.h" 13 | 14 | #include "waiter.h" 15 | #include "dht11.h" 16 | 17 | void app_main() 18 | { 19 | DHT11_init(GPIO_NUM_4); 20 | 21 | while(1) { 22 | printf("Temperature is %d \n", DHT11_read().temperature); 23 | printf("Humidity is %d\n", DHT11_read().humidity); 24 | printf("Status code is %d\n", DHT11_read().status); 25 | waitSeconds(1); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /include/dht11.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Michele Biondi 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #ifndef DHT11_H_ 26 | #define DHT11_H_ 27 | 28 | #include "driver/gpio.h" 29 | 30 | enum dht11_status { 31 | DHT11_CRC_ERROR = -2, 32 | DHT11_TIMEOUT_ERROR, 33 | DHT11_OK 34 | }; 35 | 36 | struct dht11_reading { 37 | int status; 38 | int temperature; 39 | int humidity; 40 | }; 41 | 42 | void DHT11_init(gpio_num_t); 43 | 44 | struct dht11_reading DHT11_read(); 45 | 46 | #endif -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ESP32-DHT11", 3 | "keywords": "DHT11, esp32, esp-idf, espressif, temperature, humidity", 4 | "description": "Full featured driver to use DHT11 sensor on esp32 with esp-idf", 5 | "authors": { 6 | "name": "Michele Biondi", 7 | "email": "michelebiondi01@gmail.com", 8 | "maintainer": true 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/an4cr0n/esp32-DHT11.git" 13 | }, 14 | "version": "1.0.1", 15 | "frameworks": "espidf", 16 | "platforms": "espressif32" 17 | } 18 | --------------------------------------------------------------------------------