2 or more DHT22 - Sensors on one Arduino pro mini



  • I am a newbie with MySensors And Arduino, I'm looking for a sketch to put 2 or more DHT22 sensors on a pro mini that will run in 2.0. I have the pro mini running with one sensor and the DhtTemperatureAndHumiditySensor sketch. I have no idea where to begin. below is the Sketch

    Thank you in advance Rick

    /**
     * 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.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0: Henrik EKblad
     * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz)
     * 
     * DESCRIPTION
     * This sketch provides an example of how to implement a humidity/temperature
     * sensor using a DHT11/DHT-22.
     *  
     * For more information, please visit:
     * http://www.mysensors.org/build/humidity
     * 
     */
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached 
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    //#define MY_RS485
     
    #include <SPI.h>
    #include <MySensors.h>  
    #include <DHT.h>
    
    // Set this to the pin you connected the DHT's data pin to
    #define DHT_DATA_PIN 3
    
    // Set this offset if the sensor has a permanent small offset to the real temperatures
    #define SENSOR_TEMP_OFFSET 0
    
    // Sleep time between sensor updates (in milliseconds)
    // Must be >1000ms for DHT22 and >2000ms for DHT11
    static const uint64_t UPDATE_INTERVAL = 60000;
    
    // Force sending an update of the temperature after n sensor reads, so a controller showing the
    // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
    // the value didn't change since;
    // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
    static const uint8_t FORCE_UPDATE_N_READS = 10;
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    
    float lastTemp;
    float lastHum;
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    bool metric = true;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    DHT dht;
    
    
    void presentation()  
    { 
      // Send the sketch version information to the gateway
      sendSketchInfo("TemperatureAndHumidity", "1.1");
      
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
      
      metric = getConfig().isMetric;
    }
    
    
    void setup()
    {
      dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
      if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
        Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
      }
      // Sleep for the time of the minimum sampling period to give the sensor time to power up
      // (otherwise, timeout errors might occure for the first reading)
      sleep(dht.getMinimumSamplingPeriod());
    }
    
    
    void loop()      
    {  
      // Force reading sensor, so it works also after sleep()
      dht.readSensor(true);
      
      // Get temperature from DHT library
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
        Serial.println("Failed reading temperature from DHT!");
      } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
        // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
        lastTemp = temperature;
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        // Reset no updates counter
        nNoUpdatesTemp = 0;
        temperature += SENSOR_TEMP_OFFSET;
        send(msgTemp.set(temperature, 1));
    
        #ifdef MY_DEBUG
        Serial.print("T: ");
        Serial.println(temperature);
        #endif
      } else {
        // Increase no update counter if the temperature stayed the same
        nNoUpdatesTemp++;
      }
    
      // Get humidity from DHT library
      float humidity = dht.getHumidity();
      if (isnan(humidity)) {
        Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
        // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
        lastHum = humidity;
        // Reset no updates counter
        nNoUpdatesHum = 0;
        send(msgHum.set(humidity, 1));
        
        #ifdef MY_DEBUG
        Serial.print("H: ");
        Serial.println(humidity);
        #endif
      } else {
        // Increase no update counter if the humidity stayed the same
        nNoUpdatesHum++;
      }
    
      // Sleep for a while to save energy
      sleep(UPDATE_INTERVAL); 
    }
    


  • I found this forum thread but it must be for a version prior to 2.0
    link text

    can this sketch be converted to 2.0?

    #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
    }```

  • Hero Member

    Here are the instructions how to convert the sketch. Please try and if you run in to trouble please let me know and I will walk you through it.

    Converting sketch from 1.5 to to 2.0

    Add the following to activate NRF-radio support

    #define MY_RADIO_NRF24
    

    Remove MySensor constructor

    MySensors gw;
    

    Remove MySensors setup() call

    gw.setup(....);
    

    If node expects incoming messages, add the following function to handle data:
    void receive(const MyMessage &msg) {}

    Move present() and sendSketchInfo() from setup() to a new function called presentation().

    void presentation() {
         sendSketchInfo(...)	
         present(...);
    }
    

    If you’re using static ids , add:

    #define MY_NODE_ID xx
    

    If static parent node is used, add:

    #define MY_PARENT_NODE_ID xx
    

    If you want node to be a repeater, add

    #define MY_REPEATER_FEATURE
    

    If you need to reconfigure your CE/CS Pins (not needed if NRF is connected to default pins)

    #define MY_RF24_CE_PIN 9
    #define MY_RF24_CS_PIN 10
    

    Remove all “gw.” before MySensors library calls.

    If node requests time using requestTime(), remove callback argument define define the following function to handle time response:

    void receiveTime(unsigned long ts) {
    }
    

    Make sure to include MySensors.h after any configuration defines.



  • @korttoma Thanks for the instructions. Can you check and see if I have missed anything in the code ?

    
    #define MY_DEBUG
    #define MY_REPEATER_FEATURE
    #define MY_RADIO_NRF24
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <DHT.h>
    
    #define CHILD_ID_HUM1 1
    #define CHILD_ID_TEMP1 2
    #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)
    
    
    DHT dht1;
    DHT dht2;
    DHT dht3;
    
    
    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 presentation()  
    { 
       // Send the Sketch Version Information to the Gateway
      sendSketchInfo("MultiDHT2", "1.0");
      
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_HUM1, S_HUM);
      wait( 55 );
      present(CHILD_ID_TEMP1, S_TEMP);
      wait( 55 );
      present(CHILD_ID_HUM2, S_HUM);
      wait( 55 );
      present(CHILD_ID_TEMP2, S_TEMP);
      wait( 55 );
      present(CHILD_ID_HUM3, S_HUM);
      wait( 55 );
      present(CHILD_ID_TEMP3, S_TEMP);
      wait( 55 );
    
      metric = getConfig().isMetric;
    }
    
    void setup()
    {
    
      dht1.setup(HUMIDITY_SENSOR_1_DIGITAL_PIN);
      dht2.setup(HUMIDITY_SENSOR_2_DIGITAL_PIN);
      dht3.setup(HUMIDITY_SENSOR_3_DIGITAL_PIN);
    
    
    }
    
    void loop()
    {
      delay(dht1.getMinimumSamplingPeriod());
    
      float temperature1 = dht1.getTemperature();
      if (isnan(temperature1)) {
        Serial.println("Failed reading temperature from DHT1");
      } else if (temperature1 != lastTemp1) {
        lastTemp1 = temperature1;
        if (!metric) {
          temperature1 = dht1.toFahrenheit(temperature1);
        }
        send(msgTemp1.set(temperature1, 1));
        Serial.print("T: ");
        Serial.println(temperature1);
      }
    
      float humidity1 = dht1.getHumidity();
      if (isnan(humidity1)) {
        Serial.println("Failed reading humidity from DHT1");
      } else if (humidity1 != lastHum1) {
        lastHum1 = humidity1;
        send(msgHum1.set(humidity1, 1));
        Serial.print("H: ");
        Serial.println(humidity1);
      }
    
      float temperature2 = dht2.getTemperature();
      if (isnan(temperature2)) {
        Serial.println("Failed reading temperature from DHT2");
      } else if (temperature2 != lastTemp2) {
        lastTemp2 = temperature2;
        if (!metric) {
          temperature2 = dht2.toFahrenheit(temperature2);
        }
        send(msgTemp2.set(temperature2, 1));
        Serial.print("T: ");
        Serial.println(temperature2);
      }
    
      float humidity2 = dht2.getHumidity();
      if (isnan(humidity2)) {
        Serial.println("Failed reading humidity from DHT2");
      } else if (humidity2 != lastHum2) {
        lastHum2 = humidity2;
        send(msgHum2.set(humidity2, 1));
        Serial.print("H: ");
        Serial.println(humidity2);
      }
    
      float temperature3 = dht3.getTemperature();
      if (isnan(temperature3)) {
        Serial.println("Failed reading temperature from DHT3");
      } else if (temperature3 != lastTemp3) {
        lastTemp3 = temperature3;
        if (!metric) {
          temperature3 = dht3.toFahrenheit(temperature3);
        }
        send(msgTemp3.set(temperature3, 1));
        Serial.print("T: ");
        Serial.println(temperature3);
      }
    
      float humidity3 = dht3.getHumidity();
      if (isnan(humidity3)) {
        Serial.println("Failed reading humidity from DHT3");
      } else if (humidity3 != lastHum3) {
        lastHum3 = humidity3;
        send(msgHum3.set(humidity3, 1));
        Serial.print("H: ");
        Serial.println(humidity3);
      }
    
      // sleep(SLEEP_TIME); //sleep a bit  //
      wait(SLEEP_TIME); //sleep a bit  //
    }
    

  • Hero Member

    @turborick looks fine to me, does it compile?



  • @korttoma yes it compiles. here is the output from serial monitor

    Starting repeater (RNNRA-, 2.0.0)
    TSM:INIT
    TSM:RADIO:OK
    TSP:ASSIGNID:OK (ID=2)
    TSM:FPAR
    TSP:MSG:SEND 2-2-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSP:MSG:READ 0-0-2 s=255,c=3,t=8,pt=1,l=1,sg=0:0
    TSP:MSG:FPAR RES (ID=0, dist=0)
    TSP:MSG:PAR OK (ID=0, dist=1)
    TSM:FPAR:OK
    TSM:ID
    TSM:CHKID:OK (ID=2)
    TSM:UPL
    TSP:PING:SEND (dest=0)
    TSP:MSG:SEND 2-2-0-0 s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=ok:1
    TSP:MSG:READ 0-0-2 s=255,c=3,t=25,pt=1,l=1,sg=0:1
    TSP:MSG:PONG RECV (hops=1)
    TSP:CHKUPL:OK
    TSM:UPL:OK
    TSM:READY
    TSP:MSG:SEND 2-2-0-0 s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=ok:0100
    TSP:MSG:SEND 2-2-0-0 s=255,c=0,t=18,pt=0,l=5,sg=0,ft=0,st=ok:2.0.0
    TSP:MSG:SEND 2-2-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=ok:0
    TSP:MSG:SEND 2-2-0-0 s=255,c=3,t=11,pt=0,l=9,sg=0,ft=0,st=ok:MultiDHT2
    TSP:MSG:SEND 2-2-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=ok:1.0
    TSP:MSG:SEND 2-2-0-0 s=1,c=0,t=7,pt=0,l=0,sg=0,ft=0,st=ok:
    TSP:MSG:SEND 2-2-0-0 s=2,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=ok:
    TSP:MSG:SEND 2-2-0-0 s=3,c=0,t=7,pt=0,l=0,sg=0,ft=0,st=ok:
    TSP:MSG:SEND 2-2-0-0 s=4,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=ok:
    TSP:MSG:SEND 2-2-0-0 s=5,c=0,t=7,pt=0,l=0,sg=0,ft=0,st=ok:
    TSP:MSG:SEND 2-2-0-0 s=6,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=ok:
    Request registration...
    TSP:MSG:SEND 2-2-0-0 s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=ok:2
    TSP:MSG:READ 0-0-2 s=255,c=3,t=27,pt=1,l=1,sg=0:1
    Node registration=1
    Init complete, id=2, parent=0, distance=1, registration=1
    TSP:MSG:SEND 2-2-0-0 s=2,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=ok:18.8
    T: 18.80
    TSP:MSG:SEND 2-2-0-0 s=1,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=ok:29.0
    H: 29.00
    TSP:MSG:SEND 2-2-0-0 s=4,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=ok:18.7
    T: 18.70
    TSP:MSG:SEND 2-2-0-0 s=3,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=ok:27.5
    H: 27.50
    TSP:MSG:SEND 2-2-0-0 s=6,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=ok:18.8
    T: 18.80
    TSP:MSG:SEND 2-2-0-0 s=5,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=ok:30.0
    H: 30.00
    TSP:MSG:SEND 2-2-0-0 s=4,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=ok:18.6
    T: 18.60
    TSP:MSG:SEND 2-2-0-0 s=5,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=ok:30.1
    H: 30.10
    TSP:SANCHK:OK
    TSP:MSG:SEND 2-2-0-0 s=1,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=ok:28.9
    H: 28.90
    TSP:MSG:SEND 2-2-0-0 s=3,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=ok:27.4
    H: 27.40
    TSP:MSG:SEND 2-2-0-0 s=4,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=ok:18.7
    T: 18.70
    TSP:MSG:SEND 2-2-0-0 s=3,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=ok:27.5
    H: 27.50
    TSP:MSG:SEND 2-2-0-0 s=6,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=ok:18.7
    T: 18.70
    TSP:MSG:SEND 2-2-0-0 s=5,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=ok:30.2
    H: 30.20
    TSP:SANCHK:OK
    

  • Hero Member

    @turborick seems to be working just fine, don´t you think?



  • @korttoma I'll try and add to Vera tomorrow


Log in to reply
 

Suggested Topics

12
Online

11.4k
Users

11.1k
Topics

112.7k
Posts