Four DHT22 in Arduino uno

¿Anyone could tell me if it is possible?
When I upload the code, the Arduino remains offline

The code:

#include "DHT.h"
//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneEthernet.h>

#define DHT1PIN 4     // what digital pin we're connected to
#define DHT1TYPE DHT22   // DHT 22  (AM2302), AM2321
#define DHT2PIN 5     // what digital pin we're connected to
#define DHT2TYPE DHT22   // DHT 22  (AM2302), AM2321
#define DHT3PIN 6     // what digital pin we're connected to
#define DHT3TYPE DHT22   // DHT 22  (AM2302), AM2321
#define DHT4PIN 7     // what digital pin we're connected to
#define DHT4TYPE DHT22   // DHT 22  (AM2302), AM2321

#define VIRT_TEMP1 V1
#define VIRT_HUM1 V2
#define VIRT_TEMP2 V3
#define VIRT_HUM2 V4
#define VIRT_TEMP3 V5
#define VIRT_HUM3 V6
#define VIRT_TEMP4 V7
#define VIRT_HUM4 V8

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "x*******p";

DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
DHT dht3(DHT3PIN, DHT3TYPE);
DHT dht4(DHT4PIN, DHT4TYPE);

unsigned long prev_DHT_refresh, interval_DHT_refresh = 1000;


void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token);

  dht1.begin();
  dht2.begin();
  dht3.begin();
  dht4.begin();
}

void loop()
{
  Cayenne.run();

  getDhtValues();

}

void getDhtValues() {
    unsigned long now = millis();

  if (now - prev_DHT_refresh > interval_DHT_refresh) {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h1 = dht1.readHumidity();
float h2 = dht2.readHumidity();
float h3 = dht3.readHumidity();
float h4 = dht4.readHumidity();
// Read temperature as Celsius (the default)
float t1 = dht1.readTemperature();
float t2 = dht2.readTemperature();
float t3 = dht3.readTemperature();
float t4 = dht4.readTemperature();

// Check if any reads failed
if (!isnan(h1) && !isnan(t1)) {
  Cayenne.virtualWrite(VIRT_HUM1, h1);
  Cayenne.celsiusWrite(VIRT_TEMP1, t1);
  }
if (!isnan(h2) && !isnan(t2)) {
  Cayenne.virtualWrite(VIRT_HUM2, h2);
  Cayenne.celsiusWrite(VIRT_TEMP2, t2);
  }
if (!isnan(h3) && !isnan(t3)) {
  Cayenne.virtualWrite(VIRT_HUM3, h3);
  Cayenne.celsiusWrite(VIRT_TEMP3, t3);
  }
if (!isnan(h4) && !isnan(t4)) {
  Cayenne.virtualWrite(VIRT_HUM4, h4);
  Cayenne.celsiusWrite(VIRT_TEMP4, t4);
}
prev_DHT_refresh = now;
}
}

Hi @dpineda.psi,

I don’t suspect anything in the sketch related to the DHT sensors would have an impact on your connectivity here. Can the same Arduino connect OK if you use just the basic connectivity sketch? (with your token in the place of AuthenticationToken of course.) If yes with the basic sketch, and no with the DHT sketch, then maybe there is something there that I’m missing that’s causing an issue. If both sketches can’t connect, it’s likely some other issue than the sketch code.

Either way, the first step toward troubleshooting this would be the output from your Arduino IDE serial monitor after uploading the sketch while it attempts to connect to Cayenne. Share that with us and we’ll offer our thoughts.

edit: just to be sure, this Arduino Uno has a W5100 Ethernet Shield, correct?

1 Like

The basic skecht work fine

Ok, so perhaps it is something in the code that I’m not considering. Could you show me the Arduino IDE serial monitor output from when you try to run the DHT sketch that’s not connecting?