multiple DHT11 on a ESP8266 over MQTT --Code issue

To break this down:
I am running node red network on a raspberry pi 3,
I want to place a DHT11 in each room and wire them all to a single ESP8266 and this will transmit over MQTT to the PI3 and be ready by node red.
I found this code and it works when uploaded. Issue is when i try to scale it to 8 DHT11.
The code below loads to the ESP8266 but when I run the serial port check it only reads the first “DHT1” and repeats.

I tried to reach out to the creators with no luck. This is a cut down version, last few blocks of code were cut to fit the 9000 character limit.

ALSO I am new to the coding. I am handy with building things but i am trying to learn to code.

/*****
 
 All the resources for this project:
 https://randomnerdtutorials.com/
 
*****/

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

#define DHTTYPE DHT11   // DHT 11

const char* ssid = "FiOS-33CXZ";
const char* password = "elks2543ton5142cue";
const char* mqtt_server = "192.168.1.248";

WiFiClient espClient;
PubSubClient client(espClient);

const int DHT1Pin = 5;    // DHT Sensor - GPIO 5 = D1 on ESP-12E NodeMCU board
const int DHT2Pin = 4;    // DHT Sensor - GPIO 4 = D2 on ESP-12E NodeMCU board
const int DHT3Pin = 0;    // DHT Sensor - GPIO 0 = D3 on ESP-12E NodeMCU board
const int DHT4Pin = 2;    // DHT Sensor - GPIO 2 = D4 on ESP-12E NodeMCU board
const int DHT5Pin = 14;    // DHT Sensor - GPIO 14 = D5 on ESP-12E NodeMCU board
const int DHT6Pin = 12;   // DHT Sensor - GPIO 12 = D6 on ESP-12E NodeMCU board
const int DHT7Pin = 13;    // DHT Sensor - GPIO 13 = D7 on ESP-12E NodeMCU board
//const int DHT8Pin = 15;    // DHT Sensor - GPIO 15 = D8 on ESP-12E NodeMCU board  //LED LAMP in use

const int lamp = 15;     // Lamp - LED - GPIO 15 = D8 on ESP-12E NodeMCU board


DHT dht1(DHT2Pin, DHTTYPE);
DHT dht2(DHT2Pin, DHTTYPE);
DHT dht3(DHT3Pin, DHTTYPE);
DHT dht4(DHT4Pin, DHTTYPE);
DHT dht5(DHT5Pin, DHTTYPE);
DHT dht6(DHT6Pin, DHTTYPE);
DHT dht7(DHT7Pin, DHTTYPE);
//DHT dht8(DHT8Pin, DHTTYPE); //LED LAMP in use


long now = millis();
long lastMeasure = 0;

void setup_wifi() {
  delay(10);
  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.print("WiFi connected - ESP IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  if(topic=="room/lamp"){
      Serial.print("Changing Room lamp to ");
      if(messageTemp == "on"){
        digitalWrite(lamp, HIGH);
        Serial.print("On");
      }
      else if(messageTemp == "off"){
        digitalWrite(lamp, LOW);
        Serial.print("Off");
      }
  }
  Serial.println();
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");

    if (client.connect("ESP8266Client2")) {
      Serial.println("connected");  
      client.subscribe("room/lamp");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void setup() {
  pinMode(lamp, OUTPUT);
  
  dht1.begin();
  dht2.begin();
  dht3.begin();
  dht4.begin();
  dht5.begin();
  dht6.begin();
  dht7.begin();
//  dht8.begin();
      
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("ESP8266Client2");

  now = millis();
  if (now - lastMeasure > 30000) {
    lastMeasure = now;
    float h = dht1.readHumidity(); float t = dht1.readTemperature(); float f = dht1.readTemperature(true);
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT1 sensor!");
      return;  
  }
  
    float hic = dht1.computeHeatIndex(t, h, false);
    static char temperatureTemp[7];
    dtostrf(hic, 6, 2, temperatureTemp);

    static char humidityTemp[7];
    dtostrf(h, 6, 2, humidityTemp);

    client.publish("room1/temperature", temperatureTemp);
    client.publish("room1/humidity", humidityTemp);

    Serial.print("DHT1 ");
    Serial.print("Humidity: "); Serial.print(h);
    Serial.print(" %\t Temperature: "); Serial.print(t);
    Serial.print(" *C "); Serial.print(f);
    Serial.print(" *F\t Heat index: "); Serial.print(hic);
    Serial.println(" *C ");
    // Serial.print(hif);
    // Serial.println(" *F");
  }
}


  if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("ESP8266Client2");
  now = millis();
  if (now - lastMeasure > 31000) {
    lastMeasure = now;
    float h = dht2.readHumidity(); float t = dht2.readTemperature(); float f = dht2.readTemperature(true);
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT2 sensor!");
      return;
  }     
    float hic = dht2.computeHeatIndex(t, h, false);
    static char temperatureTemp[7];
    dtostrf(hic, 6, 2, temperatureTemp);

    static char humidityTemp[7];
    dtostrf(h, 6, 2, humidityTemp);

    client.publish("room2/temperature", temperatureTemp);
    client.publish("room2/humidity", humidityTemp);

    Serial.print("DHT2 ");
    Serial.print("Humidity: "); Serial.print(h);
    Serial.print(" %\t Temperature: "); Serial.print(t);
    Serial.print(" *C "); Serial.print(f);
    Serial.print(" *F\t Heat index: "); Serial.print(hic);
    Serial.println(" *C ");
    // Serial.print(hif);
    // Serial.println(" *F");
  }
  

  if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("ESP8266Client2");
  now = millis();
  if (now - lastMeasure > 32000) {
    lastMeasure = now;
    float h = dht3.readHumidity(); float t = dht3.readTemperature(); float f = dht3.readTemperature(true);
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT3 sensor!");
      return;
  }     
    float hic = dht3.computeHeatIndex(t, h, false);
    static char temperatureTemp[7];
    dtostrf(hic, 6, 2, temperatureTemp);

    static char humidityTemp[7];
    dtostrf(h, 6, 2, humidityTemp);

    client.publish("room3/temperature", temperatureTemp);
    client.publish("room3/humidity", humidityTemp);

    Serial.print("DHT3 ");
    Serial.print("Humidity: "); Serial.print(h);
    Serial.print(" %\t Temperature: "); Serial.print(t);
    Serial.print(" *C "); Serial.print(f);
    Serial.print(" *F\t Heat index: "); Serial.print(hic);
    Serial.println(" *C ");
    // Serial.print(hif);
    // Serial.println(" *F");
  }
  

  if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("ESP8266Client2");
  now = millis();
  if (now - lastMeasure > 33000) {
    lastMeasure = now;
    float h = dht4.readHumidity(); float t = dht4.readTemperature(); float f = dht4.readTemperature(true);
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT4 sensor!");
      return;
  }     
    float hic = dht4.computeHeatIndex(t, h, false);
    static char temperatureTemp[7];
    dtostrf(hic, 6, 2, temperatureTemp);

    static char humidityTemp[7];
    dtostrf(h, 6, 2, humidityTemp);

    client.publish("room4/temperature", temperatureTemp);
    client.publish("room4/humidity", humidityTemp);

    Serial.print("DHT4 ");
    Serial.print("Humidity: "); Serial.print(h);
    Serial.print(" %\t Temperature: "); Serial.print(t);
    Serial.print(" *C "); Serial.print(f);
    Serial.print(" *F\t Heat index: "); Serial.print(hic);
    Serial.println(" *C ");
    // Serial.print(hif);
    // Serial.println(" *F");
  }

}

First: edit your post and insert code tags! Read the sticky post at the top of the topic to know how to do that.

The code below loads to the ESP8266 but when I run the serial port check it only reads the first “DHT1” and repeats.

I’m quite sure it read the value of the second DHT11 as that code is reached first. It enters that code if the time since the last read is more than 20s. Once it enters that if block, lastMeasure is set to the current millis() value. Because of this the code for the first DHT11 will never be reached as that if checks if lastMeasure was set 30sec ago. As that value is reset every 20 secs, 30 secs will never be reached.

Hint: learn to use arrays!

pylon:
Hint: learn to use arrays!

Hint: Learn to use Code Tags as described under Item #6 in this very cleverly titled thread: Read this before posting a programming question …

Hint: I wouldn’t post my WiFi password on a public forum.

pylon:
First: edit your post and insert code tags! Read the sticky post at the top of the topic to know how to do that.

I'm quite sure it read the value of the second DHT11 as that code is reached first. It enters that code if the time since the last read is more than 20s. Once it enters that if block, lastMeasure is set to the current millis() value. Because of this the code for the first DHT11 will never be reached as that if checks if lastMeasure was set 30sec ago. As that value is reset every 20 secs, 30 secs will never be reached.

Hint: learn to use arrays!

Thank you for reply, obviously I am lost and I am making beginner mistakes. Is the fix to set both times to 20 seconds? Can all of the sensors be placed on the same time?

Or is there a better fix for this?

ideally what I would like to see if all 8 read and transmit at the same interval say every 20 seconds. So is there a way to group them all into one package that is being time checked and send it all together?
IF not then how can i change it that DHT1 reads then 10 secons later DHT2 ETC...

Thank you for reply, obviously I am lost and I am making beginner mistakes. Is the fix to set both times to 20 seconds? Can all of the sensors be placed on the same time?

No, but have separate variable for lastMeasure, one for each sensor.

pylon:
No, but have separate variable for lastMeasure, one for each sensor.

This is taken from the code:
now = millis();
if (now - lastMeasure > 27000) {
lastMeasure = now;

Are you saying I should change it too:
now = millis();
if (now - 30000 > 27000) {
lastMeasure = now;

Are you saying I should change it too:

No, I wrote to have a separate lastMeasure for each sensor:

  if (now - lastMeasure1 > 30000) {
    lastMeasure1 = now;
    float h = dht1.readHumidity(); float t = dht1.readTemperature(); float f = dht1.readTemperature(true);

and

  if (now - lastMeasure2 > 31000) {
    lastMeasure2 = now;
    float h = dht2.readHumidity(); float t = dht2.readTemperature(); float f = dht2.readTemperature(true);

That’s just for illustrating the concept. I also wrote that you should use arrays. It will make your sketch much more compact and less complex.