Two DHT22 sensors with nodemcu (v2) and blynk app via wifi

Hi everybody! My name its david i´m from Spain and English is not my native language so apologize in advance.
In looking for some help with my project for monitoring temp and humidity fron two dht22 sensors on nodemcu (v2) via wifi with blynk app, in order to log those 4 data in superchart or 4 gauges (sensor1 T&H and sensor2 T&H) .
i can get the two sensors work just fine on serial monitor (local), but only one over blynk (internet).
I´ve changed my code so many times that now not sure whats work and what wasnt.
my code looks like this:

#include <SPI.h>
#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[] = "*********************************"; //Enter the Auth code which was send by Blink

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "******";  //Enter your WIFI Name
char pass[] = "******************";  //Enter your WIFI Password


//#define DHTPIN 4          // 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);
DHT sensor1(4, DHT22); // Change the pin D2 to suit your needs and sensor type to match yours e.g. DHT11, or DHT 21 or DHT22
DHT sensor2(5, DHT22); // Change the pin D1 to suit your needs and sensor type to match yours e.g. DHT11, or DHT 21 or DHT22
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 h1 = sensor1.readHumidity();
  float t1 = sensor1.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  float h2 = sensor2.readHumidity();
  float t2 = sensor2.readTemperature();
  if (isnan(h1) || isnan(t1)) {
    Serial.println("Failed to read from DHT sensor1!");
    if (isnan(h2) || isnan(t2)) {
      Serial.println("Failed to read from DHT sensor2!");
      return;
    }
    // You can send any value at any time.
    // Please don't send more that 10 values per second.
    Blynk.virtualWrite(V5, h1);
    Blynk.virtualWrite(V6, t1);
    Blynk.virtualWrite(V7, h2);
    Blynk.virtualWrite(V8, t2);
  }}

  void setup()
  {
    

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

 void loop() {
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  delay(100);
  }

´´´

Are you sure you can see the 2 sensors on serial monitor?
I don’t see any serial.print t1 & t2 :thinking:

timer.setInterval(1000L, sendSensor); // <---- need more time to acquire data
delay(100);//<---- delay not allowed in blynk loop

Right, I deleted those parts when porting the skech to blynk.
But it’s possible to run those identical sensors on different digital pins on serial monitor. Some people have do it before (and myself yesterday).
I’m noob programming, so I usually copy / edit code from previous projects. So thanks everybody who share their projects to community

First of all, add serial.print and let us see your serial monitor

So add those lines back in, and share your serial output, and also your Blynk app setup information.

Pete.

1 Like

This one works for me on serial monitor:

Humidity 1: 69.20 % Temperature 1: 30.30 *C Humidity 2: 66.70 % Temperature 2: 30.70 *C Humidity 1: 69.30 % Temperature 1: 30.30 *C Humidity 2: 66.70 % Temperature 2: 30.70 *C Humidity 1: 70.00 % Temperature 1: 30.30 *C Humidity 2: 67.30 % Temperature 2: 30.70 *C …



#include "DHT.h"

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)


const int DHTPin = 5;     // D1 on node mcu
const int DHTPin2 = 4;    // D2 on node mcu

DHT dht(DHTPin, DHTTYPE);
DHT dht2(DHTPin2, DHTTYPE);
void setup() {
   Serial.begin(9600);
   Serial.println("DHTxx test!");

   dht.begin();
   dht2.begin();
}

void loop() {
   // Wait a few seconds between measurements.
   delay(2000);

   // Reading temperature or humidity takes about 250 milliseconds!
   float h = dht.readHumidity();
   float t = dht.readTemperature();
   float h2 = dht2.readHumidity();
   float t2 = dht2.readTemperature();

   if (isnan(h) || isnan(t)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
   }


   Serial.print("Humidity 1: ");
   Serial.print(h);
   Serial.print(" %\t");
   Serial.print("Temperature 1: ");
   Serial.print(t);
   Serial.print(" *C ");
   Serial.print("Humidity 2: ");
   Serial.print(h2);
   Serial.print(" %\t");
   Serial.print("Temperature 2: ");
   Serial.print(t2);
   Serial.print(" *C ");

}

But this is a totally different sketch.
We need to see the serial prints immediately before or after the Blynk.virtualWrites

Pete.

Yes, different sketch.
I’ll try to adapt this one to blynk and share my results in a moment

We told you to provide your serial monitor with the first sketch without any changes.
Else we can’t help you.

well, after some tries, i´m unable to make it work (no conection with blink, and no serial.print working with first skecht)

Here´s another try.
It´s a single DHT22 sketch working OK with my nodemcu+blynk.
Hope you can help me to add Serial.print for those values first (i think is what you are asking for) and then adding a second sensor support:

 //Temperature sensor
/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 * This example shows how value can be pushed from Arduino to
 * the Blynk App.
 *
 * WARNING :
 * For this example you'll need SimpleTimer library:
 *   https://github.com/jfturcot/SimpleTimer
 * and Adafruit DHT sensor library:
 *   https://github.com/adafruit/DHT-sensor-library
 *
 * App project setup:
 *   Value Display widget attached to V5
 *   Value Display widget attached to V6
 *
 **************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "**********************************"; //Enter the Auth code which was send by Blink

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "****";  //Enter your WIFI Name
char pass[] = "**********************";  //Enter your WIFI Password


#define DHTPIN 2          // Digital pin 4

// 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);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature
}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

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

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

We wanted that because of your assertion that the correct values appeared in the serial monitor, but not in Blynk.

It turns out that this wasn’t actually the case, because you were using different sketches to produce the serial output and the Blynk output.

I’d suggest that you go back to your original code, remove the delay, change the timer from 1000ms to 10000ms, add-in serial prints for h1, t1, h2 and t2 and change these nan tests to that they are two separate if statements with the correct number of closing brackets…

I’d also remove the comments that are irrelevant (such as the unused types of DHT sensors, and how to obtain an auth token etc).

Post your new sketch, the serial output, a Blynk screenshot in run mode, snd one in edit mode showing which pins are in use.

Pete.

2 Likes
timer.setInterval(10000L, sendSensor);//<----  10 000 is enough

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);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature

   Serial.print("Humidity 1: ");
   Serial.print(h);
   Serial.println(" %\t");
   Serial.print("Temperature 1: ");
   Serial.print(t);
   Serial.println(" °C ");
}
1 Like

Thanks you guys, really apreaciate your patience. :sweat_smile:

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h>
#include <DHT.h>


char auth[] = "******************************";
char ssid[] = "*****";
char pass[] = "***********";


#define DHTPIN 2
#define DHTTYPE DHT22


DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;


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);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature

   Serial.print("Humidity 1: ");
   Serial.print(h);
   Serial.println(" %\t");
   Serial.print("Temperature 1: ");
   Serial.print(t);
   Serial.println(" °C ");
}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

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

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
  
}

This is the first time i can connect blynk and both, serial print and app values fron 1 sensor.
But with a extranger behaviour: when module connect, only get nan on both serial and app. then i need to remove the dht22 module fron protoboard and reconnect and then values are displayed OK on serial and app. So module seens not booting properly and a hot swap solve the problem.

When booting:

[7799] IP: 192.168.43.250
[7799] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.1 on ESP8266

[7877] Connecting to blynk-cloud.com:80
[8262] Ready (ping: 77ms).
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!

When swapping sensor:

Failed to read from DHT sensor!
Humidity 1: 86.60 %	
Temperature 1: 26.80 °C 
Humidity 1: 86.60 %	
Temperature 1: 26.80 °C 
Humidity 1: 85.50 %	
Temperature 1: 26.90 °C 
Humidity 1: 85.50 %	
Temperature 1: 26.90 °C 
Humidity 1: 77.20 %	
Temperature 1: 26.90 °C 
Humidity 1: 77.20 %	
Temperature 1: 26.90 °C 
Humidity 1: 72.00 %	

So i think i need to solve this thing first and then go with sensor #2 .
Any ideas?

1 Like

Well, powering from a dc power supply (so arduino ide closed, and no pc conection), works fine with the app without removing sensor to be found.

Yeah, I’m very close to getting it!


#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h>
#include <DHT.h>


char auth[] = "***********************";
char ssid[] = "*****";
char pass[] = "************";


#define DHTPIN 2
#define DHTPIN2 5
#define DHTTYPE DHT22


DHT dht(DHTPIN, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
BlynkTimer timer;


void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();

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);  //V5 is for Humidity
  Blynk.virtualWrite(V6, t);  //V6 is for Temperature
  Blynk.virtualWrite(V7, h2);  //V7 is for Humidity
  Blynk.virtualWrite(V8, t2);  //V8 is for Temperature
   Serial.print("Humidity 1: ");
   Serial.print(h);
   Serial.println(" %\t");
   Serial.print("Temperature 1: ");
   Serial.print(t);
   Serial.println(" °C ");
   Serial.print("Humidity 2: ");
   Serial.print(h2);
   Serial.println(" %\t");
   Serial.print("Temperature 2: ");
   Serial.print(t2);
   Serial.println(" °C ");
}

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();
  dht2.begin();

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

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
  
}

Now i can get values from 2 sensors in app and in serial monitor:
Sensor 2 boot just fine.
Sensor 1 have the issue of swapping to get values when connected to pc, so i´m pretty sure now that the problem is in this funtion, that is not defined for sensor 2:
if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; }
i think first sensors readings is missing when booting and this “return” command makes a dead loop maybe?
I can´t comment out the “isnan” funtion because makes me an error during compillation.

I said 10000 not a 1000

true, and i test with both and makes no difference.
Do you know if i can set timer interval for each sensor alternating? something like “5 sec - sensor 1 read - 5 sec - sensor 2 read - 5 sec …”
How can i remove the isnan thing without crashing the sketch?

You don’t need to know temperature every 10 seconds
Temperature only change every 2 hours

Try to test with two individuals calls
With 1 minute interval