初始化提交
This commit is contained in:
112
arduino-cli/libraries/esp_dht/esp_dht.cpp
Normal file
112
arduino-cli/libraries/esp_dht/esp_dht.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "esp_dht.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
DHT11 :: DHT11(uint8 p)
|
||||
{
|
||||
pin=p;
|
||||
}
|
||||
DHT11::~DHT11()
|
||||
{
|
||||
|
||||
}
|
||||
void DHT11:: PortIN()
|
||||
{
|
||||
pinMode(pin,INPUT);
|
||||
}
|
||||
|
||||
void DHT11:: PortOUT()
|
||||
{
|
||||
pinMode(pin,OUTPUT);
|
||||
}
|
||||
|
||||
uint8 DHT11:: Start()
|
||||
{
|
||||
uint8 result = 0;
|
||||
PortOUT();
|
||||
DHT11_Pin_Low;
|
||||
delay(20);
|
||||
DHT11_Pin_Hig;
|
||||
delayMicroseconds(45);
|
||||
PortIN();
|
||||
delayMicroseconds(5);
|
||||
while(DHT11_Pin_In==0)
|
||||
{
|
||||
result++;
|
||||
delayMicroseconds(10);
|
||||
}
|
||||
while(DHT11_Pin_In&&result);
|
||||
if(result>3)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8 DHT11::ReadByte()
|
||||
{
|
||||
uint8 reader;
|
||||
uint8 bitsum;
|
||||
reader = 0;
|
||||
PortIN();
|
||||
for(bitsum=0;bitsum<8;bitsum++)
|
||||
{
|
||||
reader = reader << 1;
|
||||
while(DHT11_Pin_In==0);
|
||||
delayMicroseconds(50);
|
||||
if(DHT11_Pin_In)
|
||||
reader |= 0x01;
|
||||
while(DHT11_Pin_In);
|
||||
}
|
||||
return reader;
|
||||
|
||||
|
||||
}
|
||||
uint8 DHT11::Read_Value(uint8 *dht)
|
||||
{
|
||||
uint8 sum;
|
||||
uint8 checksum=0;
|
||||
if(Start())
|
||||
{
|
||||
dht[0] = ReadByte();
|
||||
dht[1] = ReadByte();
|
||||
dht[2] = ReadByte();
|
||||
dht[3] = ReadByte();
|
||||
checksum = ReadByte();
|
||||
sum = (dht[0]+dht[1]+dht[2]+dht[3]);
|
||||
if(checksum==sum)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
};
|
||||
return false;
|
||||
}
|
||||
void DHT11::NumToString(uint8 dht,uint8 *str)
|
||||
{
|
||||
str[0] = (dht%100)/10 + '0';
|
||||
str[1] = dht%10 + '0';
|
||||
str[2] = '\0';
|
||||
}
|
||||
|
||||
void DHT11::Get_DHT11_Value() //DHT11 数据获取
|
||||
{
|
||||
uint8 dhtData[5];
|
||||
uint8 outstr[10];
|
||||
if(Read_Value(dhtData))
|
||||
{ Serial.print("temperature: ");
|
||||
NumToString(dhtData[2],outstr);Serial.write(outstr,3);
|
||||
NumToString(dhtData[3],outstr);Serial.write(outstr,3);
|
||||
delay(10);
|
||||
Serial.print("\r\n");
|
||||
Serial.print("humidity: ");
|
||||
NumToString(dhtData[0],outstr);Serial.write(outstr,3);
|
||||
NumToString(dhtData[1],outstr);Serial.write(outstr,3);
|
||||
delay(10);
|
||||
Serial.print("\r\n");
|
||||
//Serial.write(dhtData,4);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
30
arduino-cli/libraries/esp_dht/esp_dht.h
Normal file
30
arduino-cli/libraries/esp_dht/esp_dht.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/**********************************************************
|
||||
Make in goouuu.com
|
||||
Maker:MDC
|
||||
***********************************************************/
|
||||
#ifndef ESP_DHT11_H
|
||||
#define ESP_DHT11_H
|
||||
#define uint8 unsigned char
|
||||
|
||||
|
||||
class DHT11
|
||||
{
|
||||
private:
|
||||
uint8 pin;
|
||||
public: //公共方法
|
||||
DHT11(uint8 p);//构造函数
|
||||
~DHT11();//析构函数
|
||||
void PortIN();//DHT11 引脚设置为输入模式
|
||||
void PortOUT();//DHT11 引脚设置为输出模式
|
||||
uint8 Start();//开始读取数据
|
||||
uint8 ReadByte();//读取一个字节的数据
|
||||
uint8 Read_Value(uint8 *dht);//读取5个字节,读取一帧温湿度数据
|
||||
void NumToString(uint8 dht,uint8 *str);
|
||||
void Get_DHT11_Value();//获取一帧数据并且打印
|
||||
};
|
||||
#define DHT11_Pin_In digitalRead(pin)
|
||||
#define DHT11_Pin_Low digitalWrite(pin,0)
|
||||
#define DHT11_Pin_Hig digitalWrite(pin,1)
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user