DHT and LDR separately working, but together not, what's wrong?



  • Hi all,

    I have a simple sketch for humidity and temperature reading and I want to add a LDR for light level readings. But, separately the sketches for the humidity and temp is working. when putting it together only the LightReading is working.

    What's wrong? hardware is the same for both sketches.

    Thanks, Danny

    Hum/Temp code:

    /**
     * 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
     * 
     * DESCRIPTION
     * This sketch provides an example how to implement a humidity/temperature
     * sensor using DHT11/DHT-22 
     * http://www.mysensors.org/build/humidity
     */
     
    #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
    unsigned long SLEEP_TIME = 30000; // 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
    }
    
    
    

    Code: HUM/TMP/LDR:

    /**
    * 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
    * 
    * DESCRIPTION
    * This sketch provides an example how to implement a humidity/temperature
    * sensor using DHT11/DHT-22 
    * http://www.mysensors.org/build/humidity
    */
    
    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>  
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_LIGHT 2
    #define HUMIDITY_SENSOR_DIGITAL_PIN 4
    #define LIGHT_SENSOR_ANALOG_PIN A1
    
    #define MY_DEBUG
    
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    
    MySensor gw;
    DHT dht;
    float lastTemp;
    float lastHum;
    boolean metric = true; 
    int lastLightLevel;
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgLight(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    
    void setup()  
    { 
     gw.begin();
     dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
     dht.setup(LIGHT_SENSOR_ANALOG_PIN); 
    
     // Send the Sketch Version Information to the Gateway
     gw.sendSketchInfo("HumidityAndLight", "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);
     gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
       
     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);
     }
         
     int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
     Serial.print("L: "); 
     Serial.println(lightLevel);
     if (lightLevel != lastLightLevel) {
         gw.send(msgLight.set(lightLevel));
         lastLightLevel = lightLevel;
     }
    
     gw.sleep(SLEEP_TIME); //sleep a bit
    }
    
    

  • Contest Winner

    Hello @DannyM,

    Could you be a bit more specific. Like what's the serial ouput what does work and what doesn't work. A bit hard for us to help you without those details



  • Okay,

    I understand, here they are:

    TempHum only:

    send: 1-1-0-0 s=255,c=0,t=17,pt=0,l=3,sg=0,st=fail:1.5
    send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=fail:0
    sensor started, id=1, parent=0, distance=1
    send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=8,sg=0,st=fail:Humidity
    send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=fail:1.0
    send: 1-1-0-0 s=0,c=0,t=7,pt=0,l=0,sg=0,st=fail:
    send: 1-1-0-0 s=1,c=0,t=6,pt=0,l=0,sg=0,st=fail:
    find parent
    send: 1-1-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,st=bc:
    send: 1-1-0-0 s=1,c=1,t=0,pt=7,l=5,sg=0,st=fail:24.0
    T: 24.00
    send: 1-1-0-0 s=0,c=1,t=1,pt=7,l=5,sg=0,st=fail:38.0
    H: 38.00
    
    

    With LDR added:

    send: 1-1-0-0 s=255,c=0,t=17,pt=0,l=3,sg=0,st=fail:1.5
    send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=fail:0
    sensor started, id=1, parent=0, distance=1
    send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=16,sg=0,st=fail:HumidityAndLight
    send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=fail:1.0
    send: 1-1-0-0 s=0,c=0,t=7,pt=0,l=0,sg=0,st=fail:
    send: 1-1-0-0 s=1,c=0,t=6,pt=0,l=0,sg=0,st=fail:
    find parent
    send: 1-1-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,st=bc:
    send: 1-1-0-0 s=2,c=0,t=16,pt=0,l=0,sg=0,st=fail:
    Failed reading temperature from DHT
    Failed reading humidity from DHT
    send: 1-1-0-0 s=2,c=1,t=23,pt=2,l=2,sg=0,st=fail:62
    L: 62
    Failed reading temperature from DHT
    Failed reading humidity from DHT
    
    

  • Hardware Contributor

    @DannyM st=fail indicate you have some range or power issues with your radio. A wild guess since the sensors dont work together is power issues with your node. Try adding wait(50); between readings and when radio transmits so it has the chance to "recharge" before sending again. Also check your capacitor on your radio.



  • Hi @sundberg84 I made a higher delay before of the reading of the lightLevel, [delay(1000);] But that didn't change any of the outcome.
    What about the capacitator? do I have to use another one?


  • Hardware Contributor

    @DannyM - If you use a capacitor on the radio, you are fine.

    Is the node far way from your gateway?
    How do you power the sensors?



  • @sundberg84 My node is approx 3 meters away from the gateway, and now I am powering it from the computer, it's an Arduino Nano with USB cable.



  • I have now rebooted my GW so sending will be OK:

    send: 1-1-0-0 s=255,c=0,t=17,pt=0,l=3,sg=0,st=fail:1.5
    send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=fail:0
    sensor started, id=1, parent=0, distance=1
    send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=16,sg=0,st=fail:HumidityAndLight
    send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=fail:0.2
    send: 1-1-0-0 s=0,c=0,t=7,pt=0,l=0,sg=0,st=fail:
    send: 1-1-0-0 s=1,c=0,t=6,pt=0,l=0,sg=0,st=fail:
    find parent
    send: 1-1-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,st=bc:
    send: 1-1-0-0 s=2,c=0,t=16,pt=0,l=0,sg=0,st=fail:
    Failed reading temperature from DHT
    Failed reading humidity from DHT
    send: 1-1-0-0 s=2,c=1,t=23,pt=7,l=5,sg=0,st=fail:61.2
    L: 61.2
    Failed reading temperature from DHT
    Failed reading humidity from DHT
    send: 1-1-0-0 s=2,c=1,t=23,pt=7,l=5,sg=0,st=ok:6.9
    L: 6.9
    Failed reading temperature from DHT
    Failed reading humidity from DHT
    send: 1-1-0-0 s=2,c=1,t=23,pt=7,l=5,sg=0,st=ok:7.1
    L: 7.1
    

  • Contest Winner

    @DannyM You still have some failures. I noticed that they're present in the Humidity only sketch as well.

    In my experience it's better to power nothing from the Arduino. The radio only works always, but using a phone charger will give you 5v and using a buck boost converter (80 euro cents on aliexpress) can provide 3.3V. As an alternative for the buck converter a low drop LE33 works perfectly for me.

    But please attache capacitor to all radio's (gateway and nodes). And try to power as less as you can from the arduino. The arduino just wasn't designed to deliver a lot of current.



  • Maybe This has something to do with it:

     dht.setup(LIGHT_SENSOR_ANALOG_PIN); 
    

    The Lightsensor isn't belonging to the DHT sensor.. I think 😕


  • Contest Winner

    @DannyM Could be. But I doubt it. Look here.

    TempHum only:
    
    send: 1-1-0-0 s=255,c=0,t=17,pt=0,l=3,sg=0,st=fail:1.5
    send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=fail:0
    

    st=fails means that the radio can't deliver the message to the gateway. If a message could be delivered st will be ok

    Failed reading temperature from DHT
    Failed reading humidity from DHT
    send: 1-1-0-0 s=2,c=1,t=23,pt=7,l=5,sg=0,st=ok:6.9
    

    The failed reading is certainly caused by initializing the DHT with the wrong pin.

    Anyway I hope that you're able to get this to work with the help of others. I wouldn't be much online this weekend. But I'm hoping to see a positive post from you in this thread (meaning you got it working ;-)).

    Might be a good idea to start with the humidity temp sensor only. And see if you can get to work without any st=fail's in your log. Then you can go to the next step.



  • Yes, that did it,

    Thanks @sundberg84 and @TheoL for your time..



  • @TheoL I use a telephone charger most of the time, I'm a beginner with all.
    Hope to make this a battery oparated sensor, will search for the components you mentioned.

    thanks



  • Hi all,

    Now I have bought a stepdown regulator to regulate the power for the radio.
    The readings of the sensor are the same as not using the stepdown regulator. So what can be wrong?
    Or is it not possible to use a LDR and a DHT11 together?



  • @DannyM

    Yes you can use LDR and a DHT11 together.

     * 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
     * 
     * DESCRIPTION
     * Example sketch showing how to measue light level using a LM393 photo-resistor 
     * http://www.mysensors.org/build/light
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // NodId
    #define MY_NODE_ID 99
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    //Including nescessary libraries
    #include <SPI.h>
    #include <MySensors.h> 
    #include <DHT.h>  
    
    // 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;
    
    //Defining child ID
    #define CHILD_ID_LIGHT 0
    #define CHILD_ID_HUM 1
    #define CHILD_ID_TEMP 2
    
    //Defining Pin
    #define DHT_DATA_PIN 3
    #define LDR_PIN A0
    
    //Defining values to store sensor information
    float lastTemp;
    float lastHum;
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    bool metric = true;
    int lastLightLevel;
    
    //Defining sleep and wait times
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)(30 seconds)
    #define MESSAGEWAIT 500
    
    MyMessage LightMsg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    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 and Controller
      sendSketchInfo("Temp Fukt Ljus", "1.0"); // 2017 02 05
    //  wait(MESSAGEWAIT);
    
      // Register all sensors to gateway (they will be created as child devices)
      present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    //  wait(MESSAGEWAIT);
      present(CHILD_ID_HUM, S_HUM);
    //  wait(MESSAGEWAIT);
      present(CHILD_ID_TEMP, S_TEMP);
      
        metric = getControllerConfig().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++;
      }
    
      
      int lightLevel = (1023-analogRead(LDR_PIN))/10.23; 
      Serial.println(lightLevel);
      if (lightLevel != lastLightLevel) {
          send(LightMsg.set(lightLevel));
          lastLightLevel = lightLevel;
      }
      sleep(SLEEP_TIME);
    }
    

    LDR pin A0

    DHT pin 3

    And i use pro mini.

    //Mattias



  • @MLs

    Hi, thanks for your input, whit setup of the pins you used I have no more failed to read from DHT sensors in my serial monitor.

    Now I need to revise my sketch, so it will send correct values.

    Thanks again!



  • @DannyM

    //Mattias

    I use a 10k resistor to LDR.



  • Hi all,

    All works fine now! After the reshuffle of the connection pins it does what I want it to do.
    Only thing I need to change is the sleeptime, it's now set to 6000 and when no change send a value to the GW after ten readings. But it is to often now..


Log in to reply
 

Suggested Topics

11
Online

11.4k
Users

11.1k
Topics

112.7k
Posts