具体工程已上传至优快云我的资源里,可直接免费下载
先创建一个组件命名为DHT11,会生成一个文件夹,操作如下:
文件目录如下:
在components文件的CMakeLists.txt里写如下的代码:
记得把main文件下的CMake的txt文件下工程名改一下:
接着复制粘贴DHT11.h文件的代码如下:
#ifndef __DHT11_H__
#define __DHT11_H__
#define uchar unsigned char
#define uint8 unsigned char
#define uint16 unsigned short
extern void DHT11(void); //温湿传感启动
extern uchar shidu,wendu;
#endif
DHT11.c文件代码如下:
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "soc/gpio_reg.h"
#include "soc/io_mux_reg.h"
#include "driver/gpio.h"
#include "DHT11.h"
//引脚定义
#define DHT11_PIN 4 //定义DHT11的引脚
//温湿度定义
uchar ucharFLAG,uchartemp;
uchar shidu,wendu;
uchar ucharT_data_H,ucharT_data_L,ucharRH_data_H,ucharRH_data_L,ucharcheckdata;
uchar ucharT_data_H_temp,ucharT_data_L_temp,ucharRH_data_H_temp,ucharRH_data_L_temp,ucharcheckdata_temp;
uchar ucharcomdata;
static void InputInitial(void)//设置端口为输入
{
esp_rom_gpio_pad_select_gpio(DHT11_PIN);
gpio_set_direction(DHT11_PIN, GPIO_MODE_INPUT);
}
static void OutputHigh(void)//输出1
{
esp_rom_gpio_pad_select_gpio(DHT11_PIN);
gpio_set_direction(DHT11_PIN, GPIO_MODE_OUTPUT);
gpio_set_level(DHT11_PIN, 1);
}
static void OutputLow(void)//输出0
{
esp_rom_gpio_pad_select_gpio(DHT11_PIN);
gpio_set_direction(DHT11_PIN, GPIO_MODE_OUTPUT);
gpio_set_level(DHT11_PIN, 0);
}
static uint8 getData()//读取状态
{
return gpio_get_level(DHT11_PIN);
}
//读取一个字节数据
static void COM(void) // 温湿写入
{
uchar i;
for(i=0;i<8;i++)
{
ucharFLAG=2;
//等待IO口变低,变低后,通过延时去判断是0还是1
while((getData()==0)&&ucharFLAG++) esp_rom_delay_us(10);
esp_rom_delay_us(35);//延时35us
uchartemp=0;
//如果这个位是1,35us后,还是1,否则为0
if(getData()==1) uchartemp=1;
ucharFLAG=2;
//等待IO口变高,变高后,表示可以读取下一位
while((getData()==1)&&ucharFLAG++) esp_rom_delay_us(10);
if(ucharFLAG==1)break;
ucharcomdata<<=1;
ucharcomdata|=uchartemp;
}
}
void Delay_ms(uint16 ms)
{
int i=0;
for(i=0; i<ms; i++){
esp_rom_delay_us(1000);
}
}
void DHT11(void) //温湿传感启动
{
OutputLow();
Delay_ms(19); //>18MS
OutputHigh();
InputInitial(); //输入
esp_rom_delay_us(30);
if(!getData())//表示传感器拉低总线
{
ucharFLAG=2;
//等待总线被传感器拉高
while((!getData())&&ucharFLAG++) esp_rom_delay_us(10);
ucharFLAG=2;
//等待总线被传感器拉低
while((getData())&&ucharFLAG++) esp_rom_delay_us(10);
COM();//读取第1字节,
ucharRH_data_H_temp=ucharcomdata;
COM();//读取第2字节,
ucharRH_data_L_temp=ucharcomdata;
COM();//读取第3字节,
ucharT_data_H_temp=ucharcomdata;
COM();//读取第4字节,
ucharT_data_L_temp=ucharcomdata;
COM();//读取第5字节,
ucharcheckdata_temp=ucharcomdata;
OutputHigh();
//判断校验和是否一致
uchartemp=(ucharT_data_H_temp+ucharT_data_L_temp+ucharRH_data_H_temp+ucharRH_data_L_temp);
if(uchartemp==ucharcheckdata_temp)
{
//校验和一致,
ucharRH_data_H=ucharRH_data_H_temp;
ucharRH_data_L=ucharRH_data_L_temp;
ucharT_data_H=ucharT_data_H_temp;
ucharT_data_L=ucharT_data_L_temp;
ucharcheckdata=ucharcheckdata_temp;
//保存温度和湿度
shidu=ucharRH_data_H;
wendu=ucharT_data_H;
}
else
{
shidu=100;
wendu=100;
}
}
else //没用成功读取,返回0
{
shidu=0,
wendu=0;
}
OutputHigh(); //输出
}
接着是主函数app_main.c文件代码如下:
#include <stdio.h>
#include "esp_system.h"
#include "spi_flash_mmap.h"
#include "esp_wifi.h"
#include "esp_event.h "
#include "esp_log.h"
#include "esp_err.h"
#include "nvs_flash.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/ledc.h"
#include <stdio.h>
#include "driver/uart.h"
#include "driver/gpio.h"
#include "string.h"
#include "dht11.h"
#define TAG "dht11"
//用户函数入口,相当于main函数
void app_main()
{
while(1)
{
DHT11(); //读取温湿度
ESP_LOGI(TAG,"T=%d,H=%d %%.\r\n", wendu, shidu);
vTaskDelay(300);
}
}
然后接线,DATA接GPIO4,,接着接VCC和GND
运行效果如图:
实物图: