วันอาทิตย์ที่ 24 เมษายน พ.ศ. 2559

EP1.2 ESP8266 Read DHT11

DHT11


เป็นโมดูลใช้ในการวัดอุณหภูมิ และความชื้น


รูป DHT11



โปรแกรม สามารถเลือกใช้ได้ทั้ง  DHT11  DHT22



#include "DHT.h"
#define DHTPIN 2          // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
DHT *dht;
void setup()
{
  Serial.begin(115200);
  Serial.println("DHT11 test!");
  initDht(&dht, DHTPIN, DHTTYPE);
}
void loop()
{
  readDht();

  delay(2000);
}
void initDht(DHT **dht, uint8_t pin, uint8_t dht_type) {
    *dht = new DHT(pin, dht_type, 30);
    (*dht)->begin();
    Serial.println("DHTxx Begin");
}
void readDht()
{
    float h = dht->readHumidity();
    float t = dht->readTemperature();
    float f = dht->readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  float hif = dht->computeHeatIndex(f, h);
  float hic = dht->computeHeatIndex(t, h, false);
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
}
ขอบคุณบทความ
 http://cmmakerclub.com/esp8266/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%AD%E0%B9%88%E0%B8%B2%E0%B8%99%E0%B8%84%E0%B9%88%E0%B8%B2-dht22-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-esp8266-native-arduino-ide/




ขอบคุณที่ติดตามครับ

ไม่มีความคิดเห็น:

แสดงความคิดเห็น