Skip to content

Commit

Permalink
2020-06-10 Add driver for dht11
Browse files Browse the repository at this point in the history
  • Loading branch information
xuhongv committed Jun 10, 2020
1 parent c787ae6 commit 37c91e3
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 18_dht11/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(app-template)
9 changes: 9 additions & 0 deletions 18_dht11/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := app-template

include $(IDF_PATH)/make/project.mk

33 changes: 33 additions & 0 deletions 18_dht11/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
- esp-idf:


wired up:
=========

```
DHT11 ESP32
VCC ---> VIN
DATA ---> D4
GND ---> GND
```

DHT11 with input 3~5.5v

origin dev by samrjohnston
https://github.com/samrjohnston/ESP32Projects

but his project is full of mistake;

Output:
=======

SerialPort:with 15200

stdout:

Temperature reading 25
F temperature is -2
Humidity reading 77



4 changes: 4 additions & 0 deletions 18_dht11/components/dht11/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(COMPONENT_SRCS "dht11.c")
set(COMPONENT_ADD_INCLUDEDIRS "include")

register_component()
5 changes: 5 additions & 0 deletions 18_dht11/components/dht11/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

141 changes: 141 additions & 0 deletions 18_dht11/components/dht11/dht11.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@

#include "dht11.h"

int temp_x10 = 123;
int humidity = 60;
const int channel = 0;

void dht11_init()
{
const int RMT_CLK_DIV = 80; /*!< RMT counter clock divider */
const int RMT_TICK_10_US = (80000000 / RMT_CLK_DIV / 100000); /*!< RMT counter value for 10 us.(Source clock is APB clock) */
const int rmt_item32_tIMEOUT_US = 1000; /*!< RMT receiver timeout value(us) */

rmt_config_t rmt_rx;
rmt_rx.gpio_num = DHT11_GPIO;
rmt_rx.channel = channel;
rmt_rx.clk_div = RMT_CLK_DIV;
rmt_rx.mem_block_num = 1;
rmt_rx.rmt_mode = RMT_MODE_RX;
rmt_rx.rx_config.filter_en = false;
rmt_rx.rx_config.filter_ticks_thresh = 100;
rmt_rx.rx_config.idle_threshold = rmt_item32_tIMEOUT_US / 10 * (RMT_TICK_10_US);
rmt_config(&rmt_rx);
rmt_driver_install(rmt_rx.channel, 1000, 0);
}

/*********************************************************************************
* Processing the pulse data into temp and humidity
*********************************************************************************/
static int parse_items(rmt_item32_t *item, int item_num, int *humidity, int *temp_x10)
{
int i = 0;
unsigned rh = 0, temp = 0, checksum = 0;

///////////////////////////////
// Check we have enough pulses
///////////////////////////////
if (item_num < 42)
return 0;

///////////////////////////////////////
// Skip the start of transmission pulse
///////////////////////////////////////
item++;

///////////////////////////////
// Extract the humidity data
///////////////////////////////
for (i = 0; i < 16; i++, item++)
rh = (rh << 1) + (item->duration1 < 35 ? 0 : 1);

///////////////////////////////
// Extract the temperature data
///////////////////////////////
for (i = 0; i < 16; i++, item++)
temp = (temp << 1) + (item->duration1 < 35 ? 0 : 1);

///////////////////////////////
// Extract the checksum
///////////////////////////////
for (i = 0; i < 8; i++, item++)
checksum = (checksum << 1) + (item->duration1 < 35 ? 0 : 1);

///////////////////////////////
// Check the checksum
///////////////////////////////
if ((((temp >> 8) + temp + (rh >> 8) + rh) & 0xFF) != checksum)
{
printf("Checksum failure %4X %4X %2X\n", temp, rh, checksum);
return 0;
}

///////////////////////////////
// Store into return values
///////////////////////////////
*humidity = rh >> 8;
*temp_x10 = (temp >> 8) * 10 + (temp & 0xFF);
return 1;
}

/*********************************************************************************
* Use the RMT receiver to get the DHT11 data
*********************************************************************************/
int dht11_start_get(int *temp_x10, int *humidity)
{
RingbufHandle_t rb = NULL;
size_t rx_size = 0;
rmt_item32_t *item;
int rtn = 0;

//get RMT RX ringbuffer
rmt_get_ringbuf_handle(channel, &rb);
if (!rb)
return 0;

//////////////////////////////////////////////////
// Send the 20ms pulse to kick the DHT11 into life
//////////////////////////////////////////////////
gpio_set_level(DHT11_GPIO, 1);
gpio_set_direction(DHT11_GPIO, GPIO_MODE_OUTPUT);
ets_delay_us(1000);
gpio_set_level(DHT11_GPIO, 0);
ets_delay_us(20000);

////////////////////////////////////////////////
// Bring rmt_rx_start & rmt_rx_stop into cache
////////////////////////////////////////////////
rmt_rx_start(channel, 1);
rmt_rx_stop(channel);

//////////////////////////////////////////////////
// Now get the sensor to send the data
//////////////////////////////////////////////////
gpio_set_level(DHT11_GPIO, 1);
gpio_set_direction(DHT11_GPIO, GPIO_MODE_INPUT);

////////////////////////////////////////////////
// Start the RMT receiver for the data this time
////////////////////////////////////////////////
rmt_rx_start(channel, 1);

/////////////////////////////////////////////////
// Pull the data from the ring buffer
/////////////////////////////////////////////////
item = (rmt_item32_t *)xRingbufferReceive(rb, &rx_size, 2);
if (item != NULL)
{
int n;
n = rx_size / 4 - 0;
//parse data value from ringbuffer.
rtn = parse_items(item, n, humidity, temp_x10);
//after parsing the data, return spaces to ringbuffer.
vRingbufferReturnItem(rb, (void *)item);
}
/////////////////////////////////////////////////
// Stop the RMT Receiver
/////////////////////////////////////////////////
rmt_rx_stop(channel);

return rtn;
}
34 changes: 34 additions & 0 deletions 18_dht11/components/dht11/include/dht11.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* @Author: xuhongv
* @Date: 2020-06-06 10:43:04
* @LastEditTime: 2020-06-10 10:48:30
* @LastEditors: Please set LastEditors
* @Description: origin from https://github.com/hamsternz/thingspeak-esp32-dht11
* @FilePath: https://github.com/xuhongv
*/

#ifndef DHT11_H_
#define DHT11_H_

#include <driver/rmt.h>
#include <soc/rmt_reg.h>
#include "driver/gpio.h"

// 定义的GPIO口
#define DHT11_GPIO 4

/**
* @description: 初始化
* @param {type}
* @return:
*/
void dht11_init();

/**
* @description: 获取温湿度
* @param {type} temperature温度,humidity湿度
* @return:
*/
int dht11_start_get(int *temperature, int *humidity);

#endif
3 changes: 3 additions & 0 deletions 18_dht11/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set(COMPONENT_SRCS "main.c")

register_component()
14 changes: 14 additions & 0 deletions 18_dht11/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# put here your custom config value
menu "Example Configuration"
config ESP_WIFI_SSID
string "WiFi SSID"
default "myssid"
help
SSID (network name) for the example to connect to.

config ESP_WIFI_PASSWORD
string "WiFi Password"
default "mypassword"
help
WiFi password (WPA or WPA2) for the example to use.
endmenu
8 changes: 8 additions & 0 deletions 18_dht11/main/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
# Main component makefile.
#
# This Makefile can be left empty. By default, it will take the sources in the
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#
40 changes: 40 additions & 0 deletions 18_dht11/main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

#include <string.h>
#include <openssl/ssl.h>

#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/event_groups.h>
#include <esp_log.h>
#include <nvs_flash.h>

#include "dht11.h"

const static char *TAG = "demo_dht11";

/**
* @description: 程序入口
* @param {type}
* @return:
*/
void app_main(void)
{

ESP_ERROR_CHECK(nvs_flash_init());
vTaskDelay(1000 / portTICK_PERIOD_MS);

int temp = 0;
int hum = 0;

//init
dht11_init();

while (1)
{
if (dht11_start_get(&temp, &hum))
{
ESP_LOGI(TAG, "[%lld] 温度 %i.%i℃ , 湿度 %i%%", esp_timer_get_time(), temp / 10, temp % 10, hum);
}
vTaskDelay(3000 / portTICK_PERIOD_MS);
}
}

0 comments on commit 37c91e3

Please sign in to comment.