这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界» 论坛首页» 综合技术» 物联网技术» 【Joytag 学ESP8266】使用DHT11获取温湿度

共2条 1/1 1 跳转至

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

专家
2016-06-13 17:52:36 打赏
简介

前边的文章中我们以开发快”小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
  1. #include "dht11.h"

  2. dht11 DHT11;
  3. #define DHT11PIN 5

  4. void setup()
  5. {
  6. Serial.begin(115200);
  7. }

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

  11. int chk = DHT11.read(DHT11PIN);

  12. Serial.print("Read sensor: ");
  13. switch (chk)
  14. {
  15. case DHTLIB_OK:
  16. Serial.println("OK");
  17. break;
  18. case DHTLIB_ERROR_CHECKSUM:
  19. Serial.println("Checksum error");
  20. break;
  21. case DHTLIB_ERROR_TIMEOUT:
  22. Serial.println("Time out error");
  23. break;
  24. default:
  25. Serial.println("Unknown error");
  26. break;
  27. }

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

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


  32. delay(2000);
  33. }
  34. //
  35. // END OF FILE
  36. //
复制代码
dht11.h
  1. //
  2. // FILE: dht11.h
  3. // VERSION: 0.4.1
  4. // PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
  5. // LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
  6. //
  7. // DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
  8. //
  9. // URL: http://playground.arduino.cc/Main/DHT11Lib
  10. //
  11. // HISTORY:
  12. // George Hadjikyriacou - Original version
  13. // see dht.cpp file
  14. //

  15. #ifndef dht11_h
  16. #define dht11_h

  17. #if defined(ARDUINO) && (ARDUINO >= 100)
  18. #include
  19. #else
  20. #include
  21. #endif

  22. #define DHT11LIB_VERSION "0.4.1"

  23. #define DHTLIB_OK 0
  24. #define DHTLIB_ERROR_CHECKSUM -1
  25. #define DHTLIB_ERROR_TIMEOUT -2

  26. class dht11
  27. {
  28. public:
  29. int read(int pin);
  30. int humidity;
  31. int temperature;
  32. };
  33. #endif
  34. //
  35. // END OF FILE
  36. //
复制代码
dht11.cpp
  1. //
  2. // FILE: dht11.cpp
  3. // VERSION: 0.4.1
  4. // PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
  5. // LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
  6. //
  7. // DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
  8. //
  9. // HISTORY:
  10. // George Hadjikyriacou - Original version (??)
  11. // Mod by SimKard - Version 0.2 (24/11/2010)
  12. // Mod by Rob Tillaart - Version 0.3 (28/03/2011)
  13. // + added comments
  14. // + removed all non DHT11 specific code
  15. // + added references
  16. // Mod by Rob Tillaart - Version 0.4 (17/03/2012)
  17. // + added 1.0 support
  18. // Mod by Rob Tillaart - Version 0.4.1 (19/05/2012)
  19. // + added error codes
  20. //

  21. #include "dht11.h"

  22. // Return values:
  23. // DHTLIB_OK
  24. // DHTLIB_ERROR_CHECKSUM
  25. // DHTLIB_ERROR_TIMEOUT
  26. int dht11::read(int pin)
  27. {
  28. // BUFFER TO RECEIVE
  29. uint8_t bits[5];
  30. uint8_t cnt = 7;
  31. uint8_t idx = 0;

  32. // EMPTY BUFFER
  33. for (int i=0; i< 5; i++) bits[i] = 0;

  34. // REQUEST SAMPLE
  35. pinMode(pin, OUTPUT);
  36. digitalWrite(pin, LOW);
  37. delay(18);
  38. digitalWrite(pin, HIGH);
  39. delayMicroseconds(40);
  40. pinMode(pin, INPUT);

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

  45. loopCnt = 10000;
  46. while(digitalRead(pin) == HIGH)
  47. if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

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

  54. unsigned long t = micros();

  55. loopCnt = 10000;
  56. while(digitalRead(pin) == HIGH)
  57. if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

  58. if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
  59. if (cnt == 0) // next byte?
  60. {
  61. cnt = 7; // restart at MSB
  62. idx++; // next byte!
  63. }
  64. else cnt--;
  65. }

  66. // WRITE TO RIGHT VARS
  67. // as bits[1] and bits[3] are allways zero they are omitted in formulas.
  68. humidity = bits[0];
  69. temperature = bits[2];

  70. uint8_t sum = bits[0] + bits[2];

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


  1. Read sensor: OK
  2. Humidity (%): 46.00
  3. Temperature (°C): 30.00

复制代码

总结

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

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


专家
2016-07-07 11:09:22 打赏
2楼
坐 台V5

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册]