Skip to content

Instantly share code, notes, and snippets.

@arduinothai
Last active May 17, 2018 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arduinothai/8d048fbbb74b301e3c57bd0623fd4bf3 to your computer and use it in GitHub Desktop.
Save arduinothai/8d048fbbb74b301e3c57bd0623fd4bf3 to your computer and use it in GitHub Desktop.
// Do not remove the include below
#include "Arduino.h"
#include <DHT12.h>
// Set dht12 i2c comunication on default Wire pin
DHT12 dht12;
void setup()
{
Serial.begin(112560);
// Start sensor handshake
dht12.begin();
}
int timeSinceLastRead = 0;
void loop()
{
// Report every 2 seconds.
if(timeSinceLastRead > 2000) {
// Reading temperature or humidity takes about 250 milliseconds!
// Read temperature as Celsius (the default)
float t12 = dht12.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f12 = dht12.readTemperature(true);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h12 = dht12.readHumidity();
bool dht12Read = true;
// Check if any reads failed and exit early (to try again).
if (isnan(h12) || isnan(t12) || isnan(f12)) {
Serial.println("Failed to read from DHT12 sensor!");
dht12Read = false;
}
if (dht12Read){
// Compute heat index in Fahrenheit (the default)
float hif12 = dht12.computeHeatIndex(f12, h12);
// Compute heat index in Celsius (isFahreheit = false)
float hic12 = dht12.computeHeatIndex(t12, h12, false);
// Compute dew point in Fahrenheit (the default)
float dpf12 = dht12.dewPoint(f12, h12);
// Compute dew point in Celsius (isFahreheit = false)
float dpc12 = dht12.dewPoint(t12, h12, false);
Serial.print("DHT12=> Humidity: ");
Serial.print(h12);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t12);
Serial.print(" *C ");
Serial.print(f12);
Serial.print(" *F\t");
Serial.print(" Heat index: ");
Serial.print(hic12);
Serial.print(" *C ");
Serial.print(hif12);
Serial.print(" *F");
Serial.print(" Dew point: ");
Serial.print(dpc12);
Serial.print(" *C ");
Serial.print(dpf12);
Serial.println(" *F");
}
timeSinceLastRead = 0;
}
delay(100);
timeSinceLastRead += 100;
}
// ที่มา https://github.com/xreef/DHT12_sensor_library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment