卓泰科技 发表于 2016-6-13 17:46:37

【Joytag 学ESP8266】使用DHT11获取温湿度

原文信息

标题:【Joytag 学ESP8266】使用DHT11获取温湿度
连接:http://forum.godpub.com/thread-126-1-1.html
备注:狗趴论坛首发,转载请注明出处。


简介

前边的文章中我们以开发快”小E"为例,学习了使用Arduino IDE开发ESP8266。
包括安装Arduino core for ESP8266 WiFi chip以及实现板载RGB LED8个颜色的交替闪烁等。
并且在另一篇文章中讲述了如何通过MQTT 控制ESP8266 (开发快”小E")板载 LED。

而开发快”小E"的板载硬件极其丰富,除了板载RGB LED外,还包含但不限于OLED显示屏、DHT11温湿度计等。
此例中我们测试使用DHT11温湿度计。


原理图

开发快”小E"用的是AOSONG的DHT11模块,VDD为电源,可接直流3.3-5.5V。
以下为从炫球大神的帖子中扒到的DHT11连接图,看起来与在Arduino UNO上连线没啥区别。
唯一需要注意的是DATA连接到GPIO5上,我们的程序中要做对应的修改。


代码

既然接线啥的没啥区别,那么在Arduino 开发环境下,代码也应该没啥区别吧。
在Arduino官网上找到一个DHT11库:
http://playground.arduino.cc/Main/DHT11Lib

露点(Dew Point)啥的咱不去关注(话说净网行动不允许露点!!!)
啥开氏温度(kelvin temperature)以及华氏温度(Fahrenheit temperature),咱们也不去管。

所以精简后的代码如下:
ESP8266_DHT11.ino
#include "dht11.h"

dht11 DHT11;
#define DHT11PIN 5

void setup()
{
Serial.begin(115200);
}

void loop()
{
Serial.println("\n");

int chk = DHT11.read(DHT11PIN);

Serial.print("Read sensor: ");
switch (chk)
{
    case DHTLIB_OK:
      Serial.println("OK");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      Serial.println("Checksum error");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      Serial.println("Time out error");
      break;
    default:
      Serial.println("Unknown error");
      break;
}

Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);

Serial.print("Temperature (°C): ");
Serial.println((float)DHT11.temperature, 2);


delay(2000);
}
//
// END OF FILE
//
dht11.h
//
//    FILE: dht11.h
// VERSION: 0.4.1
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
//   URL: http://playground.arduino.cc/Main/DHT11Lib
//
// HISTORY:
// George Hadjikyriacou - Original version
// see dht.cpp file
//

#ifndef dht11_h
#define dht11_h

#if defined(ARDUINO) && (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#define DHT11LIB_VERSION "0.4.1"

#define DHTLIB_OK                              0
#define DHTLIB_ERROR_CHECKSUM      -1
#define DHTLIB_ERROR_TIMEOUT      -2

class dht11
{
public:
    int read(int pin);
      int humidity;
      int temperature;
};
#endif
//
// END OF FILE
//
dht11.cpp
//
//    FILE: dht11.cpp
// VERSION: 0.4.1
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
// HISTORY:
// George Hadjikyriacou - Original version (??)
// Mod by SimKard - Version 0.2 (24/11/2010)
// Mod by Rob Tillaart - Version 0.3 (28/03/2011)
// + added comments
// + removed all non DHT11 specific code
// + added references
// Mod by Rob Tillaart - Version 0.4 (17/03/2012)
// + added 1.0 support
// Mod by Rob Tillaart - Version 0.4.1 (19/05/2012)
// + added error codes
//

#include "dht11.h"

// Return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int dht11::read(int pin)
{
      // BUFFER TO RECEIVE
      uint8_t bits;
      uint8_t cnt = 7;
      uint8_t idx = 0;

      // EMPTY BUFFER
      for (int i=0; i< 5; i++) bits = 0;

      // REQUEST SAMPLE
      pinMode(pin, OUTPUT);
      digitalWrite(pin, LOW);
      delay(18);
      digitalWrite(pin, HIGH);
      delayMicroseconds(40);
      pinMode(pin, INPUT);

      // ACKNOWLEDGE or TIMEOUT
      unsigned int loopCnt = 10000;
      while(digitalRead(pin) == LOW)
                if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

      loopCnt = 10000;
      while(digitalRead(pin) == HIGH)
                if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

      // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
      for (int i=0; i<40; i++)
      {
                loopCnt = 10000;
                while(digitalRead(pin) == LOW)
                        if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

                unsigned long t = micros();

                loopCnt = 10000;
                while(digitalRead(pin) == HIGH)
                        if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

                if ((micros() - t) > 40) bits |= (1 << cnt);
                if (cnt == 0)   // next byte?
                {
                        cnt = 7;    // restart at MSB
                        idx++;      // next byte!
                }
                else cnt--;
      }

      // WRITE TO RIGHT VARS
      // as bits and bits are allways zero they are omitted in formulas.
      humidity    = bits;
      temperature = bits;

      uint8_t sum = bits + bits;

      if (bits != sum) return DHTLIB_ERROR_CHECKSUM;
      return DHTLIB_OK;
}
//
// END OF FILE
//
把这三个文件放到ESP8266_DHT11目录下,编译并上传到开发快”小E"。
然后打开串口监视器,就可以看到每隔两秒输出如下信息:


Read sensor: OK
Humidity (%): 46.00
Temperature (°C): 30.00


总结

好吧,这个例子比较简单,拿来就可以用了。
但是恰恰是这个简单的例子,说明了使用Arduino IDE开发ESP8266是非常简单的事情。
很多库可以不做修改或者做简单的修改就可以在ESP8266上用喽。

谨以本文抛砖引玉,希望大家折腾出更好玩的东西。

后续更多精彩内容,请关注狗趴论坛:http://www.godpub.com

Qubot 发表于 2016-6-14 17:25:34

lihai
页: [1]
查看完整版本: 【Joytag 学ESP8266】使用DHT11获取温湿度