2 or more DHT11 - Sensors on one Arduino NANO MYSENSOR



  • Hello I'm a newby with programming and arduino but i like the things i can do with mysensors and the fhem on my raspberry... so i want to use a Humidity-MYSENSOR but i want to plut 2 or more of the DHT11 on one sensor...
    i tried to create a code based on the humidity-mysensor code but i failed...
    maybe someone can help me?
    is it possible to use more than one dht11 on one arduino???

    thanks for help
    mike


  • Hero Member

    @michlb1982 Hi Mike, yes it's possible. Why don't you post the code you have created so that feedback can be provided for it.

    Cheers
    Al



  • hello
    well, i tried to do it by my self but, as i said, I'm a programming newbee...
    here my code

    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>  
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    const int HUMIDITY_SENSOR_DIGITAL_PIN[] = {8, 7};
    #define NUMBER_OF_SENSORS 2
    unsigned long SLEEP_TIME = 15000; // 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[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
    }
    
    
    

    i found a relaycode and i thought that could work for the DHT11 too... ?!

    // Example sketch showing how to control physical relays. 
    // This example will remember relay state even after power failure.
    
    #include <MySensor.h>
    #include <SPI.h>
    
    const int RELAY[] = {A0, A1, A2, A3};// Arduio Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 4 // Total number of attached relays
    #define RELAY_ON 0  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
    
    MySensor gw;
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay", "1.4");
    
      // Fetch relay status
      for (int sensor=1, pin=0; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(RELAY[pin], OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(RELAY[pin], gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    }
    
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
       if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(RELAY[message.sensor-1], message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    
    

    the problem i have, is that i don't know how to "call" one pin after the other to readout the data and send them each to the gateway....

    maybe you have an idea....

    thank you !


  • Contest Winner

    @michlb1982 said:

    DHT dht;

    DHT dht;
    

    that line creates (constructs) an instance of the DHT object. If you want two DHT devices, you will need to construct two DHT objects in the program:

    DHT dht0;
    DHT dht1;
    

    then you need to do something like this:

    dht0.setup(HUMIDITY_SENSOR_DIGITAL_PIN[0]); 
    dht1.setup(HUMIDITY_SENSOR_DIGITAL_PIN[1]); 
    

    then in your program, you access each of them accordingly:

    float humidity0 = dht0.getHumidity();
    

    I hope that helps...


  • Hero Member

    To add to what BulldogLowell posted, you also need to duplicate the MySensors calls. The code below is untested and is not a real elegant way to do it, but should get you going in the right direction.

    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>  
    
    #define CHILD_ID1_HUM 0
    #define CHILD_ID1_TEMP 1
    #define CHILD_ID2_HUM 2
    #define CHILD_ID2_TEMP 3
    const int HUMIDITY_SENSOR1_DIGITAL_PIN = 8;
    const int HUMIDITY_SENSOR2_DIGITAL_PIN = 9;
    
    unsigned long SLEEP_TIME = 15000; // Sleep time between reads (in milliseconds)
    
    MySensor gw;
    DHT dht1;
    DHT dht2;
    float lastTemp1;
    float lastHum1;
    float lastTemp2;
    float lastHum2;
    boolean metric = true; 
    MyMessage msgHum1(CHILD1_ID_HUM, V_HUM);
    MyMessage msgTemp1(CHILD1_ID_TEMP, V_TEMP);
    MyMessage msgHum2(CHILD2_ID_HUM, V_HUM);
    MyMessage msgTemp2(CHILD2_ID_TEMP, V_TEMP);
    
    void setup()  
    { 
      gw.begin();
      dht1.setup(HUMIDITY_SENSOR1_DIGITAL_PIN); 
      dht2.setup(HUMIDITY_SENSOR2_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_ID1_HUM, S_HUM);
      gw.present(CHILD_ID1_TEMP, S_TEMP);
      gw.present(CHILD_ID2_HUM, S_HUM);
      gw.present(CHILD_ID2_TEMP, S_TEMP);
      
      metric = gw.getConfig().isMetric;
    }
    
    void loop()      
    {  
      delay(dht1.getMinimumSamplingPeriod());
    
      float temperature = dht1.getTemperature();
      if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature != lastTemp1) {
        lastTemp1 = temperature;
        if (!metric) {
          temperature = dht1.toFahrenheit(temperature);
        }
        gw.send(msgTemp1.set(temperature, 1));
        Serial.print("T1: ");
        Serial.println(temperature);
      }
      
      float humidity = dht1.getHumidity();
      if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum1) {
          lastHum1 = humidity;
          gw.send(msgHum1.set(humidity, 1));
          Serial.print("H1: ");
          Serial.println(humidity);
      }
    
      delay(dht2.getMinimumSamplingPeriod());
    
      float temperature = dht2.getTemperature();
      if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature != lastTemp2) {
        lastTemp = temperature;
        if (!metric) {
          temperature = dht2.toFahrenheit(temperature);
        }
        gw.send(msgTemp2.set(temperature, 1));
        Serial.print("T2: ");
        Serial.println(temperature);
      }
      
      float humidity = dht2.getHumidity();
      if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum2) {
          lastHum = humidity;
          gw.send(msgHum2.set(humidity, 1));
          Serial.print("H2: ");
          Serial.println(humidity);
      }
    
      gw.sleep(SLEEP_TIME); //sleep a bit
    }
    


  • But still, even when you duplicate them - they won't properly work in Domoticz, unfortunately.


  • Hero Member

    @andriej said:

    But still, even when you duplicate them - they won't properly work in Domoticz, unfortunately.

    Good thing the OP is using fhem then...



  • @Sparkman said:

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

    #define CHILD_ID1_HUM 0
    #define CHILD_ID1_TEMP 1
    #define CHILD_ID2_HUM 2
    #define CHILD_ID2_TEMP 3
    const int HUMIDITY_SENSOR1_DIGITAL_PIN = 8;
    const int HUMIDITY_SENSOR2_DIGITAL_PIN = 9;

    unsigned long SLEEP_TIME = 15000; // Sleep time between reads (in milliseconds)

    MySensor gw;
    DHT dht1;
    DHT dht2;
    float lastTemp1;
    float lastHum1;
    float lastTemp2;
    float lastHum2;
    boolean metric = true;
    MyMessage msgHum1(CHILD1_ID_HUM, V_HUM);
    MyMessage msgTemp1(CHILD1_ID_TEMP, V_TEMP);
    MyMessage msgHum2(CHILD2_ID_HUM, V_HUM);
    MyMessage msgTemp2(CHILD2_ID_TEMP, V_TEMP);

    void setup()
    {
    gw.begin();
    dht1.setup(HUMIDITY_SENSOR1_DIGITAL_PIN);
    dht2.setup(HUMIDITY_SENSOR2_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_ID1_HUM, S_HUM);
    gw.present(CHILD_ID1_TEMP, S_TEMP);
    gw.present(CHILD_ID2_HUM, S_HUM);
    gw.present(CHILD_ID2_TEMP, S_TEMP);

    metric = gw.getConfig().isMetric;
    }

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

    float temperature = dht1.getTemperature();
    if (isnan(temperature)) {
    Serial.println("Failed reading temperature from DHT");
    } else if (temperature != lastTemp1) {
    lastTemp1 = temperature;
    if (!metric) {
    temperature = dht1.toFahrenheit(temperature);
    }
    gw.send(msgTemp1.set(temperature, 1));
    Serial.print("T1: ");
    Serial.println(temperature);
    }

    float humidity = dht1.getHumidity();
    if (isnan(humidity)) {
    Serial.println("Failed reading humidity from DHT");
    } else if (humidity != lastHum1) {
    lastHum1 = humidity;
    gw.send(msgHum1.set(humidity, 1));
    Serial.print("H1: ");
    Serial.println(humidity);
    }

    delay(dht2.getMinimumSamplingPeriod());

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

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

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

    thanks for the help, you messed up a little with the number and the names of the nodes but after 5 min i could compile it... now the test starts...


  • Hero Member

    @michlb1982 Glad you got it working. Do you mind posting the fixed code for reference?

    Cheers
    Al



  • well it works but not 100% and it needs a lot of time that the relay switches....
    is there a way to speed up the time between giving the order and the switching of the relay?...

    here the code

    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>  
    
    //Definition der Sensoren
    #define CHILD_ID1_HUM 1
    #define CHILD_ID1_TEMP 1
    #define CHILD_ID2_HUM 2
    #define CHILD_ID2_TEMP 2
    #define CHILD_ID3_HUM 3
    #define CHILD_ID3_TEMP 3
    const int HUMIDITY_SENSOR1_DIGITAL_PIN = 8;
    const int HUMIDITY_SENSOR2_DIGITAL_PIN = 7;
    const int HUMIDITY_SENSOR3_DIGITAL_PIN = 6;
    
    //Definition der Relays
    const int RELAY[] = {A0, A1, A2, A3};// Arduio Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 4 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    unsigned long SLEEP_TIME = 15*1000; // Sleep time between reads (in milliseconds)
    
    MySensor gw;
    DHT dht1;
    DHT dht2;
    DHT dht3;
    float lastTemp1;
    float lastHum1;
    float lastTemp2;
    float lastHum2;
    float lastTemp3;
    float lastHum3;
    boolean metric = true; 
    MyMessage msgHum1(CHILD_ID1_HUM, V_HUM);
    MyMessage msgTemp1(CHILD_ID1_TEMP, V_TEMP);
    MyMessage msgHum2(CHILD_ID2_HUM, V_HUM);
    MyMessage msgTemp2(CHILD_ID2_TEMP, V_TEMP);
    MyMessage msgHum3(CHILD_ID3_HUM, V_HUM);
    MyMessage msgTemp3(CHILD_ID3_TEMP, V_TEMP);
    
    void setup()  
    { 
      gw.begin();
      dht1.setup(HUMIDITY_SENSOR1_DIGITAL_PIN); 
      dht2.setup(HUMIDITY_SENSOR2_DIGITAL_PIN); 
      dht3.setup(HUMIDITY_SENSOR3_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_ID1_HUM, S_HUM);
      gw.present(CHILD_ID1_TEMP, S_TEMP);
      gw.present(CHILD_ID2_HUM, S_HUM);
      gw.present(CHILD_ID2_TEMP, S_TEMP);
      gw.present(CHILD_ID3_HUM, S_HUM);
      gw.present(CHILD_ID3_TEMP, S_TEMP);
      
      metric = gw.getConfig().isMetric;
        // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay", "1.4");
    
      // Fetch relay status
      for (int sensor=1, pin=0; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(RELAY[pin], OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(RELAY[pin], gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
        //digitalWrite(RELAY[pin], 0);  //schaltet standardmaessig aus
    }
    
    }
    
    void loop()      
    
    {
      // Alway process incoming messages whenever possible
      gw.process();
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
       if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(RELAY[message.sensor-1], message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       }  
      
    //DHT 11 Sensor 1
      delay(dht1.getMinimumSamplingPeriod());
    
      float temperature1 = dht1.getTemperature();
      if (isnan(temperature1)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature1 != lastTemp1) {
        lastTemp1 = temperature1;
        if (!metric) {
          temperature1 = dht1.toFahrenheit(temperature1);
        }
        gw.send(msgTemp1.set(temperature1, 1));
        Serial.print("T1: ");
        Serial.println(temperature1);
      }
      
      float humidity1 = dht1.getHumidity();
      if (isnan(humidity1)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity1 != lastHum1) {
          lastHum1 = humidity1;
          gw.send(msgHum1.set(humidity1, 1));
          Serial.print("H1: ");
          Serial.println(humidity1);
      }
    
    // DHT11 Sensor 2
      delay(dht2.getMinimumSamplingPeriod());
    
      float temperature2 = dht2.getTemperature();
      if (isnan(temperature2)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature2 != lastTemp2) {
        lastTemp2 = temperature2;
        if (!metric) {
          temperature2 = dht2.toFahrenheit(temperature2);
        }
        gw.send(msgTemp2.set(temperature2, 1));
        Serial.print("T2: ");
        Serial.println(temperature2);
      }
      
      float humidity2 = dht2.getHumidity();
      if (isnan(humidity2)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity2 != lastHum2) {
          lastHum2 = humidity2;
          gw.send(msgHum2.set(humidity2, 1));
          Serial.print("H2: ");
          Serial.println(humidity2);
      }
      
    //DHT11 Sensor 3 
      delay(dht3.getMinimumSamplingPeriod());
    
      float temperature3 = dht3.getTemperature();
      if (isnan(temperature3)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature3 != lastTemp3) {
        lastTemp3 = temperature3;
        if (!metric) {
          temperature3 = dht3.toFahrenheit(temperature3);
        }
        gw.send(msgTemp3.set(temperature3, 1));
        Serial.print("T3: ");
        Serial.println(temperature3);
      }
      
      float humidity3 = dht3.getHumidity();
      if (isnan(humidity3)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity3 != lastHum3) {
          lastHum3 = humidity3;
          gw.send(msgHum3.set(humidity3, 1));
          Serial.print("H3: ");
          Serial.println(humidity3);
      }
    
      gw.sleep(SLEEP_TIME); //sleep a bit
    }
    

    maybe someone has an idea?

    the next problem, when i unplug the power and replug it, the relay gets into the last status, i want the relay always off only when i give the order... in case of "off-plowering " it shall be off after replug...
    did i explained understandable??

    short: relay = off , order to switch on => relay on , cut off power and replug Relay => still off...


  • Hero Member

    @michlb1982

    A couple of things, you put the device to sleep so while it's asleep, it won't respond to the commands to change the relays. Since you are combining relays and sensors, you need to keep the unit awake and find a different way to send the sensor data occasionally. For the relay status, read the comments in the sketch. You are saving the relay state and then reading that on startup. You can eliminate all of that code and just set the relay to off in the setup section.

    Cheers
    Al


Log in to reply
 

Suggested Topics

61
Online

11.4k
Users

11.1k
Topics

112.6k
Posts