Multiple DHT22 in one sensor for greenhouse (newbee question)



  • I try to find out how to create a sensor with 2 DHT22 and, because I am a beginner I do something wrong. Can anybody help me so I can continue with my fisrst MYSENSORS steps.
    Here the script:

    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>  
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    #define CHILD_ID_HUM 10
    #define CHILD_ID_TEMP 11
    #define HUMIDITY_SENSOR_DIGITAL_PIN 4
    unsigned long SLEEP_TIME = 3000; // Sleep time between reads (in milliseconds)
    
    MySensor gw;
    DHT dht;
    float lastTemp;
    float lastHum;
    boolean metric = true; 
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    
    
    void setup()  
    { 
      gw.begin();
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
      // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo("Humidity", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_HUM, S_HUM);
      gw.present(CHILD_ID_TEMP, S_TEMP);
      
      metric = gw.getConfig().isMetric;
    }
    
    void loop()      
    {  
      delay(dht.getMinimumSamplingPeriod());
    
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature != lastTemp) {
        lastTemp = temperature;
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        gw.send(msgTemp.set(temperature, 1));
        Serial.print("T: ");
        Serial.println(temperature);
      }
      
      float humidity = dht.getHumidity();
      if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum) {
          lastHum = humidity;
          gw.send(msgHum.set(humidity, 1));
          Serial.print("H: ");
          Serial.println(humidity);
      }
    
      gw.sleep(SLEEP_TIME); //sleep a bit
    }
    


  • @Dick said:

    I try to find out how to create a sensor with 2 DHT22 and, because I am a beginner I do something wrong. Can anybody help me so I can continue with my fisrst MYSENSORS steps.
    the problem is that I can only see one temp and one humid.
    it is something in the code .

    Here the script:

    #include <SPI.h>
    #include <MySensor.h>
    #include <DHT.h>

    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    #define CHILD_ID_HUM 10
    #define CHILD_ID_TEMP 11
    #define HUMIDITY_SENSOR_DIGITAL_PIN 4
    unsigned long SLEEP_TIME = 3000; // Sleep time between reads (in milliseconds)

    MySensor gw;
    DHT dht;
    float lastTemp;
    float lastHum;
    boolean metric = true;
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);

    void setup()
    {
    gw.begin();
    dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);

    // Send the Sketch Version Information to the Gateway
    gw.sendSketchInfo("Humidity", "1.0");

    // Register all sensors to gw (they will be created as child devices)
    gw.present(CHILD_ID_HUM, S_HUM);
    gw.present(CHILD_ID_TEMP, S_TEMP);

    metric = gw.getConfig().isMetric;
    }

    void loop()
    {
    delay(dht.getMinimumSamplingPeriod());

    float temperature = dht.getTemperature();
    if (isnan(temperature)) {
    Serial.println("Failed reading temperature from DHT");
    } else if (temperature != lastTemp) {
    lastTemp = temperature;
    if (!metric) {
    temperature = dht.toFahrenheit(temperature);
    }
    gw.send(msgTemp.set(temperature, 1));
    Serial.print("T: ");
    Serial.println(temperature);
    }

    float humidity = dht.getHumidity();
    if (isnan(humidity)) {
    Serial.println("Failed reading humidity from DHT");
    } else if (humidity != lastHum) {
    lastHum = humidity;
    gw.send(msgHum.set(humidity, 1));
    Serial.print("H: ");
    Serial.println(humidity);
    }

    gw.sleep(SLEEP_TIME); //sleep a bit
    }


  • Contest Winner

    @Dick

    This (untested) should work to check the temp and humidity:

    #include <SPI.h>
    #include <MySensor.h>
    #include <DHT.h>
    
    #define CHILD_ID_HUM1 0
    #define CHILD_ID_HUM2 1
    #define CHILD_ID_TEMP1 3
    #define CHILD_ID_TEMP2 4
    
    unsigned long SLEEP_TIME = 3000; // Sleep time between reads (in milliseconds)
    
    MySensor gw;
    DHT*  dht[2];
    byte sensorPin[2] = {3, 4};
    float lastTemp[2] = {0.0, 0.0};
    float lastHum[2] = {0.0, 0.0};
    
    boolean metric = true;
    //MyMessage msgHum(CHILD_ID_HUM1, V_HUM);
    //MyMessage msgTemp(CHILD_ID_TEMP1, V_TEMP);
    
    void setup()
    {
      gw.begin();
      for (int i = 0; i < 2; i++)
      {
        dht[i] = new DHT;
        dht[i]->setup(sensorPin[i]);
      }
    
      gw.sendSketchInfo("Humidity", "1.0");
    
      gw.present(CHILD_ID_HUM1, S_HUM);
      gw.present(CHILD_ID_HUM2, S_HUM);
      gw.present(CHILD_ID_TEMP1, S_TEMP);
      gw.present(CHILD_ID_TEMP1, S_TEMP);
    
      metric = gw.getConfig().isMetric;
    }
    
    void loop()
    {
      for (int i = 0; i < 2; i++)
      {
        delay(dht[i]->getMinimumSamplingPeriod());
        float temperature = dht[i]->getTemperature();
        if (isnan(temperature))
        {
          Serial.print(F("Failed reading temperature from DHT"));
          Serial.println(i);
        }
        else if (temperature != lastTemp[i])
        {
          lastTemp[i] = temperature;
          if (!metric)
          {
            temperature = dht[i]->toFahrenheit(temperature);
          }
          //gw.send(msgTemp.set(temperature, i));
          Serial.print(F("T"));
          Serial.print(i);
          Serial.print(F("= "));
          Serial.println(temperature);
        }
        float humidity = dht[i]->getHumidity();
        if (isnan(humidity)) 
        {
          Serial.print("Failed reading humidity from DHT");
          Serial.println(i);
        } 
        else if (humidity != lastHum[i]) 
        {
          lastHum[i] = humidity;
          //gw.send(msgHum.set(humidity, 1));
          Serial.print(F("H"));
          Serial.print(i);
          Serial.print(F("= "));
          Serial.println(humidity);
        }
      }
      gw.sleep(SLEEP_TIME); //sleep a bit
    }
    

    if you can test it using the Serial Monitor, we can then get the radio commands working (not complete yet)



  • @BulldogLowell
    thats fast, I have a lot to learn when I read your code. I tested the code and on the serial it looks fine. As you mentioned already it cannot connect to the gateway. Can you also show me how that goos? I also want to know why you use [i] in your script? can you also advise me where to start to learn the programming language you used? hoop th hear from you soon.languagerhere.


  • Contest Winner

    @Dick

    Sorry for the late response...

    I created two instances of the DHT object, using pointers, so that's a little beyond a beginner (but also not hard to do). The [i] is the for-loop index, I put the two sensors into an array to simplify the code (but a little harder for new person to understand).

    My advice is to go through the Arduino learning sections on their websites and search for C++ tutorials on the web, there are many good ones.

    Try this and see if it 1) sets up the two temperature and two humidity devices on your controller and 2) does it transmit the temperatures?

    #include <SPI.h>
    #include <MySensor.h>
    #include <DHT.h>
    
    #define NUM_SENSORS 2
    
    unsigned long SLEEP_TIME = 3000UL; // Sleep time between reads (in milliseconds)
    
    MySensor gw;
    MyMessage tempMssg(0, V_TEMP);
    MyMessage humMssg(NUM_SENSORS, V_HUM);
    
    byte tempID[NUM_SENSORS] = {0,1};
    byte humID[NUM_SENSORS] = {2,3};
    
    
    DHT* dht[NUM_SENSORS];
    byte sensorPin[NUM_SENSORS] = {3, 4};
    float lastTemp[NUM_SENSORS] = {0.0, 0.0};
    float lastHum[NUM_SENSORS] = {0.0, 0.0};
    
    boolean metric = true;
    
    void setup()
    {
      Serial.begin(9600);
      gw.begin();
      for (int i = 0; i < NUM_SENSORS; i++)
      {
        dht[i] = new DHT;
        dht[i]->setup(sensorPin[i]);
      }
    
      gw.sendSketchInfo("Humidity", "1.0");
      
      for (int i = 0; i < NUM_SENSORS; i++)
      {
        gw.present(tempID[i], S_TEMP);
        gw.present(humID[i], S_HUM);
      }
      
      metric = gw.getConfig().isMetric;
      Serial.println(F("Setup Complete."));
    }
    
    void loop()
    {
      for (int i = 0; i < NUM_SENSORS; i++)
      {
        delay(dht[i]->getMinimumSamplingPeriod());
        float temperature = dht[i]->getTemperature();
        if (isnan(temperature))
        {
          Serial.print(F("Failed reading temperature from DHT"));
          Serial.println(i);
        }
        else if (temperature != lastTemp[i])
        {
          lastTemp[i] = temperature;
          if (!metric)
          {
            temperature = dht[i]->toFahrenheit(temperature);
          }
          gw.send(tempMssg.setSensor(i).set(temperature, false));  // no ack
          Serial.print(F("T"));
          Serial.print(i);
          Serial.print(F("= "));
          Serial.println(temperature);
        }
        float humidity = dht[i]->getHumidity();
        if (isnan(humidity)) 
        {
          Serial.print("Failed reading humidity from DHT");
          Serial.println(i);
        } 
        else if (humidity != lastHum[i]) 
        {
          lastHum[i] = humidity;
          gw.send(humMssg.setSensor(i).set(humidity, false));  // no ack
          Serial.print(F("H"));
          Serial.print(i);
          Serial.print(F("= "));
          Serial.println(humidity);
        }
      }
      gw.sleep(SLEEP_TIME); //sleep a bit
    }
    

    I cannot test it but hopefully we can get it working for you.



  • @BulldogLowell
    Pls do not sorry for the late response. I found it fast! I am glad that somebody wants to help others. At the moment I am not in the position to test it but believe me Tomorrow evening, after work, it is the first thing I gonna do, ha ha.
    Also thanks for the tips where to start. I have knowlegse of SQL and SQLplus, not of C++ but enough info can be found on the internet. First of all I start with the basics of Arduino. Good begin.
    I let you know about the test results tomorrow.
    Thans again.
    to be continued. Have a nice evening.
    BR.
    Dick



  • At least I was able to test it. The solution worked perfect. Thanks for the support and I continue to finsh my project by implementing it at home. have a nice day. Bests
    Dick



  • @BulldogLowell Can this sketch be converted to 2.0.0 ?


  • Contest Winner

    @turborick

    Sorry, I've yet to upgrade to latest version of MySensors libraries.



  • This post is deleted!


  • Is there a way i could display the temp and hum for each sensor on an lcd screen?
    I know how to do this for data that is not using pointers.
    below is my sketch that flips between 2 sensors on the LCD

    /* test modified from a v1 basic mutliple multi dht22 sketch
    
        Gives correct temp and humidity readings for each dht22 in domoticz (ie not crossed between 1 and 2)
    
        Not displaying properly on the LCD yet
    
        Domoticz ver 4.11558 beta
        Gateway 3.2.1
        node 3.2.1
        09/12/2019
    
         Arduino Mega
         1.8 inch TFT LCD Display 128x160 8/16Bit ST7735S
         x2 DHT22
         NRF24
    */
    
    #define MY_DEBUG
    #define MY_RADIO_NRF24    //Mega Pins 49 CE, 53 CSN/CS, 52 SCK, 51  MOSI, 50  MISO
    #define MY_RF24_CE_PIN 49
    #define MY_RF24_CS_PIN 53
    #define MY_RF24_PA_LEVEL (RF24_PA_MIN)
    #define MY_NODE_ID 103
    
    
    #include <UTFT.h>          //LCD 1.8
    #include <SPI.h>
    #include <MySensors.h>
    #include <DHT.h>
    
    #define RELAY1 44
    #define RELAY2 45
    
    
    //static const uint8_t FORCE_UPDATE_N_READS = 10;
    extern uint8_t BigFont[];
    extern uint8_t SmallFont[];
    
    #define NUM_SENSORS 2
    
    unsigned long SLEEP_TIME = 6000; // Sleep time between reads (in milliseconds)
    
    //MySensor gw;
    MyMessage tempMssg(0, V_TEMP);
    MyMessage humMssg(NUM_SENSORS, V_HUM);
    
    byte tempID[NUM_SENSORS] = {0, 1};
    byte humID[NUM_SENSORS] = {2, 3};
    
    DHT* dht[NUM_SENSORS];
    byte sensorPin[NUM_SENSORS] = {40, 42};
    float lastTemp[NUM_SENSORS] = {0.0, 0.0};
    float lastHum[NUM_SENSORS] = {0.0, 0.0};
    
    boolean metric = true;
    
    
    //LCD setup
    UTFT myGLCD(ITDB18SP, 11, 12, 8, 9, 10); // lcd pins 3,2,6,5,4
    
    
    
    void setup()
    {
    
      myGLCD.InitLCD(LANDSCAPE);// or PORTRAIT
      myGLCD.clrScr();
      myGLCD.setColor(200, 255, 255);
      myGLCD.setBackColor(0, 0, 0);
      myGLCD.setFont(SmallFont);
      myGLCD.print("Version & Sketch", LEFT, 3);
      myGLCD.print("V.2 TEST5", LEFT, 16);
      delay(1000);
      myGLCD.clrScr();
      myGLCD.setFont(SmallFont);
      myGLCD.setColor(255, 255, 255);
    
      myGLCD.print("TH1 OUTSIDE", 2, 2);
      myGLCD.print("o", 67, 12);
      myGLCD.print("C", 74, 15);
      myGLCD.print("%", 150, 15);
    
      myGLCD.print("TH2 INSIDE", 2, 36);
      myGLCD.print("o", 67, 46);
      myGLCD.print("C", 74, 49);
      myGLCD.print("%", 150, 49);
    
    
    
      Serial.begin(9600);
      // begin();
      for (int i = 0; i < NUM_SENSORS; i++)
      {
        dht[i] = new DHT;
        dht[i]->setup(sensorPin[i]);
      }
    
      sendSketchInfo("DHT22 Humidity test5", "2.0");
    
      for (int i = 0; i < NUM_SENSORS; i++)
      {
        present(tempID[i], S_TEMP);
        present(humID[i], S_HUM);
      }
      metric = getControllerConfig().isMetric;
    
      Serial.println(F("Setup Complete."));
    }
    
    void loop()
    {
      for (int i = 0; i < NUM_SENSORS; i++)
      {
        delay(dht[i]->getMinimumSamplingPeriod());
        float temperature = dht[i]->getTemperature();
        if (isnan(temperature))
        {
          Serial.print(F("Failed reading temperature from DHT"));
          Serial.println(i);
        }
        else if (temperature != lastTemp[i])
        {
          lastTemp[i] = temperature;
          if (!metric)
          {
            temperature = dht[i]->toFahrenheit(temperature);
          }
          send(tempMssg.setSensor(i).set(temperature, false));  // no ack
          Serial.print(F("T"));
          Serial.print(i);
          Serial.print(F("= "));
          Serial.println(temperature);
        }
        myGLCD.setFont(BigFont);
        myGLCD.setColor(100, 255, 255);
        myGLCD.printNumF(temperature, 1, 1, 15);  // this where something needs to be changed to get it to display each temp separately
    
    
    
        float humidity = dht[i]->getHumidity();
        if (isnan(humidity))
        {
          Serial.print("Failed reading humidity from DHT");
          Serial.println(i);
        }
        else if (humidity != lastHum[i])
        {
          lastHum[i] = humidity;
          send(humMssg.setSensor(i).set(humidity, false));  // no ack
          Serial.print(F("H"));
          Serial.print(i);
          Serial.print(F("= "));
          Serial.println(humidity); // this where something needs to be changed to get it to display each hum separately
    
          myGLCD.setFont(BigFont);
          myGLCD.setColor(100, 255, 255);
          myGLCD.printNumF(humidity, 1, 85, 15);
    
        }
      }
      sleep(SLEEP_TIME); //sleep a bit
    }```

Log in to reply
 

Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts