BLYNK
BLYNK.IO       📲 GETTING STARTED       đź“— DOCS       âť“HELP CENTER       👉 SKETCH BUILDER

How do I use more than one DHT sensor with Blynk?

This code works (one sensor)

#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <DHT.h>

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

#define W5100_CS  10
#define SDCARD_CS 4
#define DHTPIN1 53          // What digital pin we're connected to
#define DHTPIN2 51          // What digital pin we're connected to
#define DHT1TYPE DHT22   // DHT 22, AM2302, AM2321
#define DHT2TYPE DHT22

DHT dht1(DHTPIN1, DHT1TYPE);
DHT dht2(DHTPIN2, DHT2TYPE);
BlynkTimer timer;

void sendSensor()
{
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature(); 
 // float h2 = dht2.readHumidity();
 // float t2 = dht2.readTemperature(); 

  Blynk.virtualWrite(V5, h1);
  Blynk.virtualWrite(V6, t1);
  //Blynk.virtualWrite(V7, h2);
  //Blynk.virtualWrite(V8, t2);
}

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

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  dht1.begin();
  //dht2.begin();
  Blynk.begin(auth);
 
  timer.setInterval(1000L, sendSensor);
}

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

While this code does not (two sensors)

#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <DHT.h>

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

#define W5100_CS  10
#define SDCARD_CS 4
#define DHTPIN1 53          // What digital pin we're connected to
#define DHTPIN2 51          // What digital pin we're connected to
#define DHT1TYPE DHT22   // DHT 22, AM2302, AM2321
#define DHT2TYPE DHT22

DHT dht1(DHTPIN1, DHT1TYPE);
DHT dht2(DHTPIN2, DHT2TYPE);
BlynkTimer timer;

void sendSensor()
{
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature(); 
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature(); 

  Blynk.virtualWrite(V5, h1);
  Blynk.virtualWrite(V6, t1);
  Blynk.virtualWrite(V7, h2);
  Blynk.virtualWrite(V8, t2);
}

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

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  dht1.begin();
  dht2.begin();
  Blynk.begin(auth);
 
  timer.setInterval(1000L, sendSensor);
}

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

When adding the 2nd sensor, i get disconnected almost directly from blynk. no data is sent and device offline. works perfect with on sensor with code above. Can someone help me?

Presumably the sketch has links to libraries on GitHub.

no urls at all, must be some of the code that “looks like” links in thier script…

Paste your full formatted sketch. Search the site for backticks if you don’t know how to post a sketch.
I have edited your thread title to reflect the problem.

thanks that worked :slight_smile: please solve my real problem to lol
not sure what to do now, open a new thread or not

1 Like

No. give me a minute I’ll change the thread title again.

Done.

I have also scattered more backticks through your sketch post to make it more presentable.

1 Like

Use 2 timers. one at 1333L and one at 1239L with separate code for each DHT.

Alternatively find code for pushing the DHT’s along faster. Normally very slow but I believe you can get them down to 6ms.

I gave this an attempt

#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <DHT.h>


char auth[] = "xxx";

#define W5100_CS  10
#define SDCARD_CS 4
#define DHTPIN1 53          // What digital pin we're connected to
#define DHTPIN2 51          // What digital pin we're connected to
#define DHT1TYPE DHT22   // DHT 22, AM2302, AM2321
#define DHT2TYPE DHT22

DHT dht1(DHTPIN1, DHT1TYPE);
DHT dht2(DHTPIN2, DHT2TYPE);
BlynkTimer timer;

void sendSensor1()
{
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature(); 

  Blynk.virtualWrite(V5, h1);
  Blynk.virtualWrite(V6, t1);
}

void sendSensor2()
{
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature(); 

  Blynk.virtualWrite(V7, h2);
  Blynk.virtualWrite(V8, t2);
}

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

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  dht1.begin(); 
  dht2.begin();
  Blynk.begin(auth);
  timer.setInterval(1333L, sendSensor1);
  timer.setInterval(1239L, sendSensor2);
 }

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

Almost same result in serial monitor

[0] Getting IP...
[5525] IP:192.168.10.63
[5526] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.10 on Arduino Mega

[5640] Connecting to blynk-cloud.com:8442
[10640] Connecting to blynk-cloud.com:8442
[10761] Ready (ping: 30ms).
[17277] Connecting to blynk-cloud.com:8442

Disconnected from blynk, no data sent (recived)

That’s a connection problem not an issue with DHT.

Go back to 1 sensor, run for 20 minutes and paste the full Serial Monitor output (less any token references).

[0] Getting IP...
[5525] IP:192.168.10.63
[5525] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.10 on Arduino Mega

[5639] Connecting to blynk-cloud.com:8442
[5806] Ready (ping: 30ms).

All OK with one sensor, data sent and received in “app” connection stable

OK I would suggest you ask Google the following question “Arduino run two DHT sensors” as your problem is probably not Blynk related.

:slight_smile: perhaps, but i have no problems running two DHT22 sensors “locally”. Se code example and output example below.


#include "DHT.h"
 
#define DHT1PIN 53     // what pin we're connected to
#define DHT2PIN 51
 
// Uncomment whatever type you're using!
#define DHT1TYPE DHT22   // DHT 22
#define DHT2TYPE DHT22   // DHT 22
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

 
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
 
void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");
 
  dht1.begin();
  dht2.begin();
}
 
void loop() {

  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();

  if (isnan(t1) || isnan(h1)) {
    Serial.println("Failed to read from DHT #1");
  } else {
    Serial.print("Humidity 1: "); 
    Serial.print(h1);
    Serial.print(" %\t");
    Serial.print("Temperature 1: "); 
    Serial.print(t1);
    Serial.println(" *C");
  }
  if (isnan(t2) || isnan(h2)) {
    Serial.println("Failed to read from DHT #2");
  } else {
    Serial.print("Humidity 2: "); 
    Serial.print(h2);
    Serial.print(" %\t");
    Serial.print("Temperature 2: "); 
    Serial.print(t2);
    Serial.println(" *C");
  }
  Serial.println();
}

Works like a charm:

Humidity 2: 38.90 %	Temperature 2: 25.30 *C

Humidity 1: 38.20 %	Temperature 1: 25.20 *C
Humidity 2: 38.90 %	Temperature 2: 25.30 *C

Humidity 1: 38.20 %	Temperature 1: 25.20 *C
Humidity 2: 38.90 %	Temperature 2: 25.30 *C

Humidity 1: 38.20 %	Temperature 1: 25.20 *C
Humidity 2: 38.90 %	Temperature 2: 25.30 *C

Humidity 1: 38.20 %	Temperature 1: 25.20 *C
Humidity 2: 38.90 %	Temperature 2: 25.30 *C

Humidity 1: 38.20 %	Temperature 1: 25.20 *C
Humidity 2: 38.90 %	Temperature 2: 25.30 *C

Humidity 1: 38.20 %	Temperature 1: 25.20 *C
Humidity 2: 38.90 %	Temperature 2: 25.30 *C

Humidity 1: 38.20 %	Temperature 1: 25.20 *C
Humidity 2: 38.90 %	Temperature 2: 25.30 *C

Humidity 1: 38.20 %	Temperature 1: 25.20 *C
Humidity 2: 38.90 %	Temperature 2: 25.30 *C

Humidity 1: 38.20 %	Temperature 1: 25.20 *C
Humidity 2: 38.90 %	Temperature 2: 25.30 *C

Humidity 1: 38.20 %	Temperature 1: 25.20 *C
Humidity 2: 38.90 %	Temperature 2: 25.30 *C

Ah, OK I have an idea.

What are V5, V6, V7 and V8 widgets?

If they have a default setting of 1s update frequencies change them all to PUSH.

You either pull from the app or push from the MCU, but not both.

It has to be something in my code, find it strange that i can not find example code on google… i can not be the first one trying this (btw i need about 15 sensors for my project…) but i figure if i can make 2 work then i can get 15 to work. thanks for the help… perhaps some one else have an idea?

problem is persisting

???

they are all on push
only other thing i have in app is D9 to turn on and of a LED to verify communication

but what are they, gauges, display widgets etc?

You have the correct hardware selected in the app?

Are you sure this is set up correctly?

I have four “identical” gauges one per virtual pin 5,6,7,8 - all set to push
digital pin 9 is a button on/off

i am fairly sure that the problem is not in how the app is setup, they failure happens before, it is surly something in the code when i add additional sensor that makes delays/holds or interrupts…

I re-wrote the code, just in case. Still identical issue. Works fine with one sensor but not with two.

new code:

#define BLYNK_PRINT Serial
 
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

char auth[] = "xxx";

#define DHT1PIN 53   
#define DHT2PIN 51
#define DHT1TYPE DHT22   // probobly not needed to do twice, clean later
#define DHT2TYPE DHT22   
#define W5100_CS  10
#define SDCARD_CS 4
 
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);

BlynkTimer timer;

void sendsensor1() {
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  Blynk.virtualWrite(V5, h1);
  Blynk.virtualWrite(V6, t1);
  }

void sendsensor2() {      // probobly not needed to do two void sendsensor, clean later
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
  Blynk.virtualWrite(V7, h2);
  Blynk.virtualWrite(V8, t2);
  }

void setup() {
  Serial.begin(9600); 
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); 
  Blynk.begin(auth);
  dht1.begin();
  dht2.begin(); // this seems to fail in combination with blynk, works just fine "offline" doesnt seam to read sensor 2 with blynk part of the code.. 

  timer.setInterval(1333L, sendsensor1);
  timer.setInterval(1243L, sendsensor2); // probobly not needed twice, timers are wierd things... wiki.. 
}

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

You should add in the regular isnan stuff. Blynk is powerful but it doesn’t have magical control of the DHT’s.

I don’t think it will fix your problem though.

Maybe try changing the 1243L to 7243L.