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!

Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.