Below is the code for NodeMCU + DHT11. DHT11 is a temperature and humidity sensor.
#include <DHT.h>
#define DHTPIN D1
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(115200);
dht.begin();
}
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // Read temperature as Celsius (the default)
if (isnan(h) || isnan(t)) // Check if any reads failed and exit early (to try again).
{
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
delay(1000);
}
Explanation of the circuit and code in below video:
You can also download the code from the below repository:
0 Comments