// To use the sensor DHT11,
// you need this libraries.
#include "DHT.h"
#define DHTPIN 5
#define DHTTYPE DHT11
// It initializes an object
// to use the sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// put your setup code here, to run once:
// It initializes the serial port
Serial.begin(9600);
// It initializes the sensor
dht.begin();
}
void loop() {
// put your main code here, to run repeatedly:
// There is a small delay in order
// to give time to the sensor to
// read the next data.
delay(1000);
// It creates 2 floating variables
// to read the data from the sensor
float h = dht.readHumidity();
float t = dht.readTemperature();
// If the sensor is not conected
// properly send a text to the
// serial port
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Send the data through the
// serial port
Serial.print(h);
Serial.print(",");
Serial.println(t);
}