前边的文章中我们以开发快”小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
- #else
- #include
- #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[5];
- uint8_t cnt = 7;
- uint8_t idx = 0;
- // EMPTY BUFFER
- for (int i=0; i< 5; i++) bits[i] = 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[idx] |= (1 << cnt);
- if (cnt == 0) // next byte?
- {
- cnt = 7; // restart at MSB
- idx++; // next byte!
- }
- else cnt--;
- }
- // WRITE TO RIGHT VARS
- // as bits[1] and bits[3] are allways zero they are omitted in formulas.
- humidity = bits[0];
- temperature = bits[2];
- uint8_t sum = bits[0] + bits[2];
- if (bits[4] != 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上用喽。
谨以本文抛砖引玉,希望大家折腾出更好玩的东西。