Multiple DH22 sensors



  • Hi Guys,

    Recently I've made my first sensor based on a Pro mini with DH22. (And ethernet gateway) I can read the data with Domoticz.

    Now I wanted to go one step further and connect multiple (3) DH22 sensors to one Pro mini. (Fore use in more intelligent home ventilation system) Electrical seems not to be much more difficult took input pin 3, 4, and 5, but progamming is (for me).

    I managed to recieve date for three sensors. The problem is the data send for al three sensors is identical. Serial plotter shows sending / recieving is ok, and Domoticz shows the three sensors. (With identical values.)
    I guess it may has something to do with this part:

    MyMessage msgHum1(CHILD_ID_HUM1, V_HUM);
    MyMessage msgTemp1(CHILD_ID_TEMP1, V_TEMP);
    MyMessage msgHum2(CHILD_ID_HUM2, V_HUM);
    MyMessage msgTemp2(CHILD_ID_TEMP2, V_TEMP);
    MyMessage msgHum3(CHILD_ID_HUM3, V_HUM);
    MyMessage msgTemp3(CHILD_ID_TEMP3, V_TEMP);

    Or with the part:
    float temperature1 = dht.getTemperature();
    (because how does the progamm knows what pin)

    Could anyone be so kind to give me some pointers

    Thank you

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
    
     */
     
    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>  
    
    #define CHILD_ID_HUM1 0
    #define CHILD_ID_TEMP1 1
    #define HUMIDITY_SENSOR_1_DIGITAL_PIN 3
    
    #define CHILD_ID_HUM2 3
    #define CHILD_ID_TEMP2 4
    #define HUMIDITY_SENSOR_2_DIGITAL_PIN 4
    
    #define CHILD_ID_HUM3 5
    #define CHILD_ID_TEMP3 6
    #define HUMIDITY_SENSOR_3_DIGITAL_PIN 5
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    
    MySensor gw;
    DHT dht;
    float lastTemp1;
    float lastHum1;
    float lastTemp2;
    float lastHum2;
    float lastTemp3;
    float lastHum3;
    boolean metric = true;
     
    MyMessage msgHum1(CHILD_ID_HUM1, V_HUM);
    MyMessage msgTemp1(CHILD_ID_TEMP1, V_TEMP);
    MyMessage msgHum2(CHILD_ID_HUM2, V_HUM);
    MyMessage msgTemp2(CHILD_ID_TEMP2, V_TEMP);
    MyMessage msgHum3(CHILD_ID_HUM3, V_HUM);
    MyMessage msgTemp3(CHILD_ID_TEMP3, V_TEMP);
    
    void setup()  
    { 
      gw.begin();
      dht.setup(HUMIDITY_SENSOR_1_DIGITAL_PIN); 
      dht.setup(HUMIDITY_SENSOR_2_DIGITAL_PIN);
      dht.setup(HUMIDITY_SENSOR_3_DIGITAL_PIN);
    
      // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo("HumiditySensorDrieineen", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_HUM1, S_HUM);
      gw.present(CHILD_ID_TEMP1, S_TEMP);
      gw.present(CHILD_ID_HUM2, S_HUM);
      gw.present(CHILD_ID_TEMP2, S_TEMP);
      gw.present(CHILD_ID_HUM3, S_HUM);
      gw.present(CHILD_ID_TEMP3, S_TEMP);
    
      metric = gw.getConfig().isMetric;
    }
    
    void loop()      
    {  
      delay(dht.getMinimumSamplingPeriod());
    
      float temperature1 = dht.getTemperature();
      if (isnan(temperature1)) {
          Serial.println("Failed reading temperature from DHT1");
      } else if (temperature1 != lastTemp1) {
        lastTemp1 = temperature1;
        if (!metric) {
          temperature1 = dht.toFahrenheit(temperature1);
        }
        gw.send(msgTemp1.set(temperature1, 1));
        Serial.print("T: ");
        Serial.println(temperature1);
      }
      
      float humidity1 = dht.getHumidity();
      if (isnan(humidity1)) {
          Serial.println("Failed reading humidity from DHT1");
      } else if (humidity1 != lastHum1) {
          lastHum1 = humidity1;
          gw.send(msgHum1.set(humidity1, 1));
          Serial.print("H: ");
          Serial.println(humidity1);
      }
    
      float temperature2 = dht.getTemperature();
      if (isnan(temperature2)) {
          Serial.println("Failed reading temperature from DHT2");
      } else if (temperature2 != lastTemp2) {
        lastTemp2 = temperature2;
        if (!metric) {
          temperature2 = dht.toFahrenheit(temperature2);
        }
        gw.send(msgTemp2.set(temperature2, 1));
        Serial.print("T: ");
        Serial.println(temperature2);
      }
      
      float humidity2 = dht.getHumidity();
      if (isnan(humidity2)) {
          Serial.println("Failed reading humidity from DHT2");
      } else if (humidity2 != lastHum2) {
          lastHum2 = humidity2;
          gw.send(msgHum2.set(humidity2, 1));
          Serial.print("H: ");
          Serial.println(humidity2);
      }
    
       float temperature3 = dht.getTemperature();
      if (isnan(temperature3)) {
          Serial.println("Failed reading temperature from DHT3");
      } else if (temperature3 != lastTemp3) {
        lastTemp3 = temperature3;
        if (!metric) {
          temperature3 = dht.toFahrenheit(temperature3);
        }
        gw.send(msgTemp3.set(temperature3, 1));
        Serial.print("T: ");
        Serial.println(temperature3);
      }
      
      float humidity3 = dht.getHumidity();
      if (isnan(humidity3)) {
          Serial.println("Failed reading humidity from DHT3");
      } else if (humidity3 != lastHum3) {
          lastHum3 = humidity3;
          gw.send(msgHum3.set(humidity3, 1));
          Serial.print("H: ");
          Serial.println(humidity3);
      }
    
      gw.sleep(SLEEP_TIME); //sleep a bit
    }```
    

  • Contest Winner

    @Jasper-van-Winden It can't know that. You'll need three instances of DHT. I changed your sketch so that it should work. At least it compiles I don't have three D22 that I can use to test.

    I've appended // <--- by Theo to each line I've changed.

    /**
       The MySensors Arduino library handles the wireless radio link and protocol
       between your home built sensors/actuators and HA controller of choice.
       The sensors forms a self healing radio network with optional repeaters. Each
       repeater and gateway builds a routing tables in EEPROM which keeps track of the
       network topology allowing messages to be routed to nodes.
    
    */
    
    #include <SPI.h>
    #include <MySensor.h>
    #include <DHT.h>
    
    #define CHILD_ID_HUM1 0
    #define CHILD_ID_TEMP1 1
    #define HUMIDITY_SENSOR_1_DIGITAL_PIN 3
    
    #define CHILD_ID_HUM2 3
    #define CHILD_ID_TEMP2 4
    #define HUMIDITY_SENSOR_2_DIGITAL_PIN 4
    
    #define CHILD_ID_HUM3 5
    #define CHILD_ID_TEMP3 6
    #define HUMIDITY_SENSOR_3_DIGITAL_PIN 5
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    
    MySensor gw;
    
    DHT dht1; // <--- by Theo
    DHT dht2; // <--- by Theo
    DHT dht3; // <--- by Theo
    
    
    float lastTemp1;
    float lastHum1;
    float lastTemp2;
    float lastHum2;
    float lastTemp3;
    float lastHum3;
    boolean metric = true;
    
    MyMessage msgHum1(CHILD_ID_HUM1, V_HUM);
    MyMessage msgTemp1(CHILD_ID_TEMP1, V_TEMP);
    MyMessage msgHum2(CHILD_ID_HUM2, V_HUM);
    MyMessage msgTemp2(CHILD_ID_TEMP2, V_TEMP);
    MyMessage msgHum3(CHILD_ID_HUM3, V_HUM);
    MyMessage msgTemp3(CHILD_ID_TEMP3, V_TEMP);
    
    void setup()
    {
      gw.begin();
      dht1.setup(HUMIDITY_SENSOR_1_DIGITAL_PIN); // <--- by Theo
      dht2.setup(HUMIDITY_SENSOR_2_DIGITAL_PIN); // <--- by Theo
      dht3.setup(HUMIDITY_SENSOR_3_DIGITAL_PIN); // <--- by Theo
    
      // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo("HumiditySensorDrieineen", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_HUM1, S_HUM);
      gw.wait( 55 ); // <--- by Theo
      gw.present(CHILD_ID_TEMP1, S_TEMP);
      gw.wait( 55 ); // <--- by Theo
      gw.present(CHILD_ID_HUM2, S_HUM);
      gw.wait( 55 ); // <--- by Theo
      gw.present(CHILD_ID_TEMP2, S_TEMP);
      gw.wait( 55 ); // <--- by Theo
      gw.present(CHILD_ID_HUM3, S_HUM);
      gw.wait( 55 ); // <--- by Theo
      gw.present(CHILD_ID_TEMP3, S_TEMP);
      gw.wait( 55 ); // <--- by Theo
    
      metric = gw.getConfig().isMetric;
    }
    
    void loop()
    {
      delay(dht1.getMinimumSamplingPeriod());
    
      float temperature1 = dht1.getTemperature(); // <--- by Theo
      if (isnan(temperature1)) {
        Serial.println("Failed reading temperature from DHT1");
      } else if (temperature1 != lastTemp1) {
        lastTemp1 = temperature1;
        if (!metric) {
          temperature1 = dht1.toFahrenheit(temperature1); // <--- by Theo
        }
        gw.send(msgTemp1.set(temperature1, 1));
        Serial.print("T: ");
        Serial.println(temperature1);
      }
    
      float humidity1 = dht1.getHumidity(); // <--- by Theo
      if (isnan(humidity1)) {
        Serial.println("Failed reading humidity from DHT1");
      } else if (humidity1 != lastHum1) {
        lastHum1 = humidity1;
        gw.send(msgHum1.set(humidity1, 1));
        Serial.print("H: ");
        Serial.println(humidity1);
      }
    
      float temperature2 = dht2.getTemperature(); // <--- by Theo
      if (isnan(temperature2)) {
        Serial.println("Failed reading temperature from DHT2");
      } else if (temperature2 != lastTemp2) {
        lastTemp2 = temperature2;
        if (!metric) {
          temperature2 = dht2.toFahrenheit(temperature2); // <--- by Theo
        }
        gw.send(msgTemp2.set(temperature2, 1));
        Serial.print("T: ");
        Serial.println(temperature2);
      }
    
      float humidity2 = dht2.getHumidity(); // <--- by Theo
      if (isnan(humidity2)) {
        Serial.println("Failed reading humidity from DHT2");
      } else if (humidity2 != lastHum2) {
        lastHum2 = humidity2;
        gw.send(msgHum2.set(humidity2, 1));
        Serial.print("H: ");
        Serial.println(humidity2);
      }
    
      float temperature3 = dht3.getTemperature(); // <--- by Theo
      if (isnan(temperature3)) {
        Serial.println("Failed reading temperature from DHT3");
      } else if (temperature3 != lastTemp3) {
        lastTemp3 = temperature3;
        if (!metric) {
          temperature3 = dht3.toFahrenheit(temperature3); // <--- by Theo
        }
        gw.send(msgTemp3.set(temperature3, 1));
        Serial.print("T: ");
        Serial.println(temperature3);
      }
    
      float humidity3 = dht3.getHumidity(); // <--- by Theo
      if (isnan(humidity3)) {
        Serial.println("Failed reading humidity from DHT3");
      } else if (humidity3 != lastHum3) {
        lastHum3 = humidity3;
        gw.send(msgHum3.set(humidity3, 1));
        Serial.print("H: ");
        Serial.println(humidity3);
      }
    
      //  gw.sleep(SLEEP_TIME); //sleep a bit  // // <--- by Theo
      gw.wait(SLEEP_TIME); //sleep a bit  // // <--- by Theo
    }
    

  • Contest Winner

    The code could be made much more efficient by using Arrays for instance. Or using a threshold for sending values I have a library for that on my gitgub. And posted an article about on this forum.

    But this should give you an idea of what you'd need to achieve what you want. And I guessed that it's easier for you to see what you needed to do if I just altered your code.



  • @TheoL

    Thx for your changes, I now understand were I went wrong. I gave it a quick try and it seems to work!

    I understand comments about arrays. Will try to improve the code now I have some understanding of it.

    Thank you verry much for your qick reply

    Thank you erry



  • Sorry one question. Why the waiting time in register all sensors to gateway?


  • Contest Winner

    @Jasper-van-Winden In my experience the radio sometimes can't handle the speed of the Arduino. Especially in MySensors 1.5, haven't tested it with 2.0.

    This results in missing messages, and not all of your sensors will be shown in your controller.


  • Mod

    @Jasper-van-Winden the wait has helped in many cases. Nobody is 100% sure why. Maybe it gives the capacitors and the power supply time to recover from the power used when sending. Maybe it is somethong else. But it often helps 🙂


Log in to reply
 

Suggested Topics

  • 1
  • 2
  • 2
  • 2
  • 5
  • 1

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts