Setting up the DHT11 sensor

The DHT11 sensor is a Digital Temperature and Humidity sensor which can be connected and send environmental data to the NodeMCU.


Here, we 'll create a circuit that will send the data to the Blynk server and through this to our mobile phone. So, create first the circuit following the diagram below:



In this diagram, we notice that we connect the S(ignal) pin of the DHT11 to the D4 pin of the NodeMCU. So the data will be directed to GPIO2.

Now, it is time to create a new project on the Blynk app. So, open the Blynk app, login to the server if not already logged in and make a new project called "DHT11".







Our next job is to add some widgets for the visualization of the data that the Blynk app will receive. Pressing we choose one gauge for the Temperature and another one for the Humidity.




Following all the above, now we have to label the gauges and adjust the pins that the will "listen to". The first gauge will be named Temperature, will listen to Virtual Pin 6 and its limits will be 0 to 50 Celsius degrees.


and the Humidity gauge settings:




Then we have to create the code for all this to work together:


#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "????????????????????????????";

// Your WiFi credentials.
char ssid[] = "XXXXXXXXXXXXXXXXXXXXXXX";
char pass[] = "YYYYYYYYYYYYYYYYYYYYYYYYY";

#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(DHTPIN, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}

Copy the above code, paste it into Arduino IDE, insert the Authentication code that came to your email address before, the WiFi name and the WiFi password, and then press the button . Now, watch the changes of Temperature and Humidity of the environment around DHT11, directly into your mobile phone like the next picture:



You can also add a history graph like below, choosing the Superchart and adjusting the settings according to the current setup!


So experiment with the widgets and.....DON'T FORGET TO HAVE FUN WITH BLYNK!