Multiple DHT22 with nodeMCU to Thingspeak

Hi there!
Could somebody help me to modify this code to work with 4 DHT22 sensors?
Thanks a lot!

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

// replace with your channel’s thingspeak API key,
String apiKey = "xxxxxxxxxx";
const char* ssid = "xxxx";
const char* password = "xxxxxxxx";

const char* server = "api.thingspeak.com";
#define DHTPIN 2 // SO CONNECT THE DHT11/22 SENSOR TO PIN D4 OF THE NODEMCU

DHT dht(DHTPIN, DHT22,15); //CHANGE DHT11 TO DHT22 IF YOU ARE USING DHT22
WiFiClient client;

void setup() {
Serial.begin(115200);
delay(10);
dht.begin();

WiFi.begin(ssid, password);

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

}

void loop() {

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

if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);

Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius Humidity: ");
Serial.print(h);
Serial.println("% send to Thingspeak");
}
client.stop();

Serial.println("Waiting…");
// thingspeak needs minimum 15 sec delay between updates
delay(20000);
}

where you declare the object

#define DHTPIN 2 // SO CONNECT THE DHT11/22 SENSOR TO PIN D4 OF THE NODEMCU

DHT dht(DHTPIN, DHT22,15); //CHANGE DHT11 TO DHT22 IF YOU ARE USING DHT22

declare 3 more with how you connected themDHT dht2(DHT2PIN, DHT22,15); // etc and read the values from those objects

float h = dht2.readHumidity();
float t = dht2.readTemperature();

if you want the readings in a loop, you'd probably be best of declaring an array of pointers to DHT (or an array of objects but i'm never really sure how that works, an array of pointers stretches my to my limits as it is) Basically declare 4 objects, read from 4 objects, print out the results from 4 objects, and Bob's your uncle..

Thank a lot!

I finally make it work. There is the code for 4 DHT22:

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

// replace with your channel’s thingspeak API key,
String apiKey = "xxxxxxxxxx";
const char* ssid = "xxxxxxxxxx";
const char* password = "xxxxxxxxxx";
const char* server = "api.thingspeak.com";

#define DHT1PIN 5 // SO CONNECT THE DHT11/22 SENSOR TO PIN D1 OF THE NODEMCU
#define DHT2PIN 4 // SO CONNECT THE DHT11/22 SENSOR TO PIN D2 OF THE NODEMCU
#define DHT3PIN 0 // SO CONNECT THE DHT11/22 SENSOR TO PIN D3 OF THE NODEMCU
#define DHT4PIN 2 // SO CONNECT THE DHT11/22 SENSOR TO PIN D4 OF THE NODEMCU

DHT dht1(DHT1PIN, DHT22,15); //CHANGE DHT11 TO DHT22 IF YOU ARE USING DHT22
DHT dht2(DHT2PIN, DHT22,15); //CHANGE DHT11 TO DHT22 IF YOU ARE USING DHT22
DHT dht3(DHT3PIN, DHT22,15); //CHANGE DHT11 TO DHT22 IF YOU ARE USING DHT22
DHT dht4(DHT4PIN, DHT22,15); //CHANGE DHT11 TO DHT22 IF YOU ARE USING DHT22

WiFiClient client;

void setup() {

Serial.begin(115200);
delay(10);
dht1.begin();
dht2.begin();
dht3.begin();
dht4.begin();

WiFi.begin(ssid, password);

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

}

void loop() {

float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();
float h3 = dht3.readHumidity();
float t3 = dht3.readTemperature();
float h4 = dht4.readHumidity();
float t4 = dht4.readTemperature();

if (isnan(h1) || isnan(t1)) {
Serial.println("Failed to read from DHT1 sensor!");
delay(10000);
return;
}



if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t1);
postStr +="&field2=";
postStr += String(h1);
postStr +="&field3=";
postStr += String(t2);
postStr +="&field4=";
postStr += String(h2);
postStr +="&field5=";
postStr += String(t3);
postStr +="&field6=";
postStr += String(h3);
postStr +="&field7=";
postStr += String(t4);
postStr +="&field8=";
postStr += String(h4);
postStr += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);

Serial.print("Temperature 1: ");
Serial.print(t1);
Serial.print(" Humidity 1: ");
Serial.println(h1);
Serial.print("Temperature 2: ");
Serial.print(t2);
Serial.print(" Humidity 2: ");
Serial.println(h2);
Serial.print("Temperature 3: ");
Serial.print(t3);
Serial.print(" Humidity 3: ");
Serial.println(h3);
Serial.print("Temperature 4: ");
Serial.print(t4);
Serial.print(" Humidity 4: ");
Serial.println(h4);
Serial.println("% send to Thingspeak");
}
client.stop();

Serial.println("Waiting…");
// thingspeak needs minimum 15 sec delay between updates
delay(20000);
}

yeah that’s great !