DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器,它应用专用的数字模块采集技术和温湿度传感技术,确保产品具有极高的可靠性和卓越的长期稳定性。传感器包括一个电阻式感湿元件和一个NTC测温元件,并与一个高性能8位单片机相连接。因此该产品具有品质卓越、超快响应、抗干扰能力强、性价比极高等优点。每个DHT11传感器都在极为精确的湿度校验室中进行校准。校准系数以程序的形式存在OTP内存中,传感器内部在检测型号的处理过程中要调用这些校准系数。单线制串行接口,使系统集成变得简易快捷。超小的体积、极低的功耗,使其成为该类应用中,在苛刻应用场合的最佳选择。
[C] 纯文本查看 复制代码
/*
*NRF24l01针脚连接线
* MISO -> 12
* MOSI -> 11
* SCK -> 13
* Configurable:
* CE -> 8
* CSN -> 7
*/
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include <OneWire.h>
int sid=1;//模块类型
int nid=2;//模块编号
//声名变量
const unsigned long interval = 60000;
unsigned long last_sent;
OneWire ds(A5);
float celsius=0.0;
float fahrenheit=0.0;
int DHT11_PIN=4;
int humdity=0;
int temperature=0;
//NRF24l01
unsigned long nrf24l01nowlast;
char nrf24l01buff[33]={0};
char nrf24l01Data;
int nrf24l01i=0;
void setup()
{
Serial.begin(115200);
char client[10]={0};//client
sprintf(client,"clie%d",sid);
//初始化Mirf,用于NRF24l01收发
Mirf_Init(0,client,sid);
ini_dht11();
Serial.println("zwifi_wengshitu");
}
void loop()
{
unsigned long now = millis();
if ( now - last_sent >= interval )
{
last_sent = now;
celsius=0;
humdity=0;
run_ds18();
if(celsius>0){
run_dht11();
}
if(humdity>0){
char celstr[10]={0};
dtostrf(celsius,4,2,celstr);
char data[10]={0};
sprintf(data,"%d.%s",humdity,celstr);
send_data(data);
}
}
}
//初始化Mirf 0初始化1为接收2为发送
void Mirf_Init(int txrx,char *server,int channel){
//初始化Mirf,用于NRF24l01收发
if(txrx==0) {
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.setRADDR((byte *)server);//设置接收地址
}
if(txrx==1) {
Mirf.setRADDR((byte *)server);//设置接收地址
}
if(txrx==2) {
Mirf.setTADDR((byte *)server);//设置发送地址
}
Mirf.payload = sizeof(char);//收发字节
Mirf.channel = channel;
Mirf.config();
}
//NRF24l01发送函数
void Mirf_Send(int channel,char *server,char *str){
Mirf_Init(2,server,channel);
int bufi=0;
for(bufi=0;bufi<strlen(str);bufi++){//循环发送
char words=str[bufi];//发送的字符
Mirf.send((byte *)&words);//发送命令
while(Mirf.isSending()){//等待发送完闭
}
delay(50);//延时,否则可能出现发送丢失现象
//Serial.print(words);
}
//Serial.println("");
}
void run_ds18()
{
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
//float celsius, fahrenheit;
if ( !ds.search(addr)) {
//Serial.println("No more addresses.");
//Serial.println();
ds.reset_search();
delay(250);
return;
}
//Serial.print("ROM =");
for( i = 0; i < 8; i++) {
//Serial.write(' ');
//Serial.print(addr, HEX);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
//Serial.println();
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
Serial.print("Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
Serial.print("Chip = DS18B20");
type_s = 0;
break;
case 0x22:
Serial.print("Chip = DS1822");
type_s = 0;
break;
default:
Serial.print("Device is not a DS18x20 family device.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
//Serial.print(" Data = ");
//Serial.print(present,HEX);
//Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data = ds.read();
//Serial.print(data, HEX);
//Serial.print(" ");
}
//Serial.print(" CRC=");
//Serial.print(OneWire::crc8(data, 8), HEX);
//Serial.println();
// convert the data to actual temperature
unsigned int raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// count remain gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
Serial.print(fahrenheit);
Serial.println(" Fahrenheit");
}
void ini_dht11()
{
DDRC|=_BV(DHT11_PIN);
PORTC|=_BV(DHT11_PIN);
}
byte read_dht11_dat()
{
byte i = 0;
byte result = 0;
for(i=0;i<8;i++)
{
while(!(PINC&_BV(DHT11_PIN)));
delayMicroseconds(30);
if(PINC&_BV(DHT11_PIN))
result|=(1<<(7-i));
while((PINC&_BV(DHT11_PIN)));
}
return result;
}
void run_dht11()
{
byte dht11_dat[5];
byte dht11_in;
byte i;
PORTC &= ~_BV(DHT11_PIN);
delay(18);
PORTC|=_BV(DHT11_PIN);
delayMicroseconds(40);
DDRC &= ~_BV(DHT11_PIN);
delayMicroseconds(40);
dht11_in = PINC & _BV(DHT11_PIN);
if(dht11_in)
{
Serial.println("dht11 start condition 1 not met");
return;
}
delayMicroseconds(80);
dht11_in=PINC & _BV(DHT11_PIN);
if(!dht11_in)
{
Serial.println("dht11 start condition 2 not met");
return;
}
delayMicroseconds(80);
for(i=0;i<5;i++)
dht11_dat=read_dht11_dat();
DDRC|=_BV(DHT11_PIN);
PORTC|=_BV(DHT11_PIN);
byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
if(dht11_dat[4]!=dht11_check_sum)
{
Serial.println("DHT11 checksum error");
}
Serial.print("Chip = DHT11 Current humdity= ");
Serial.print(dht11_dat[0],DEC);
Serial.print(".");
Serial.print(dht11_dat[1],DEC);
Serial.print("%");
Serial.print("temperature = ");
Serial.print(dht11_dat[2],DEC);
Serial.print(".");
Serial.print(dht11_dat[3],DEC);
Serial.println("C");
humdity=(int)dht11_dat[0];
temperature=(int)dht11_dat[2];
}
//
void send_data(char *data){
char server[10]={0};//server
sprintf(server,"serv%d",1);
//Serial.println(server);
char updateData[33]={0};
char front[10]={0};
//memcpy(front,body,9);
sprintf(front," {ck%03d%03d",sid,nid);
sprintf(updateData,"%s%s}",front,data);
Serial.println(updateData);
Serial.println();
Mirf_Send(1,server,updateData);
char client[10]={0};//client
sprintf(client,"clie%d",sid);
Mirf_Init(1,client,sid);
}