DHT11 humidity and temperature fluctuations



  • Have set up a mysensors relay node with DHT11.
    Seems the sensor is sending quite erraneous data. It is powered by some phone charger and off the same 5V line as arduino.
    The readings are taken every 60seconds
    Green is humidity, yellow is temp from DHT and blue is outside temperature from weatherunderground.
    There is a 10k pull up resistor in place. As can be seen the values for humidity some time spike into the unknown.
    What can be the reason for that? Power supply? Maybe need to define in the code the sensor type?

    alt text

    #define SN "Vent Klapans"
    #define SV "2.1"
    
    //System settings
    #define MY_NODE_ID 56
    #define MY_RADIO_NRF24
    #define MY_DEBUG
    
    #include <Bounce2.h>
    #include <DHT.h>
    #include <math.h>
    #include <MySensors.h>
    #include <SPI.h>
    #include <Wire.h>
    
    //SETUP PINS
    #define BUT1_PIN 7
    #define BUTTON_PIN2 8 
    #define DHT_PIN 3
    #define RELAY_1  5  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 2 // Total number of attached relays
    
    //Define connections
    #define CHILD_ID_HUM 3
    #define CHILD_ID_TEMP 4
    #define CHILD_BUT1_ID 7
    #define CHILD_BUT2 8
    
    #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
    DHT dht;
    
    //MQ+DHT
    long DHT_Millis = 0;
    long DHT_interval = 60000;
    //Buttons
    Bounce debouncer1 = Bounce(); 
    Bounce debouncer_2 = Bounce();
    int oldValue1=-1;
    int oldValue_2=-1;
    bool state1;
    bool state2;
    
    MyMessage msgHumi(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgBut1(CHILD_BUT1_ID,V_STATUS);
    MyMessage msgbut2(CHILD_BUT2,V_TRIPPED);
    
    void before() 
    {
      dht.setup(DHT_PIN);
      pinMode(BUT1_PIN,INPUT_PULLUP);
      pinMode(BUTTON_PIN2,INPUT);
      digitalWrite(BUTTON_PIN2, HIGH);
      debouncer1.attach(BUT1_PIN);
      debouncer1.interval(5);
      debouncer_2.attach(BUTTON_PIN2);
      debouncer_2.interval(5);
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) // Fetch relay status
        {   
        pinMode(pin, OUTPUT);   // Then set relay pins in output mode
        digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF); // Set relay to last known state (using eeprom storage) 
        }
    } 
    void setup()  
    {  Serial.begin(115200); }
    void presentation()  
    { 
      sendSketchInfo(SN, SV);
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
      present(CHILD_BUT1_ID, S_LIGHT); 
      present(CHILD_BUT2, S_DOOR);
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) // Fetch relay status
        {
        present(sensor, S_LIGHT); // Register all sensors to gw (they will be created as child devices)
        }
    }
    
    
    
    void loop() 
    {
      int value_but_2 = debouncer_2.read();
      unsigned long DHT_Current_Millis = millis();
      if(DHT_Current_Millis - DHT_Millis > DHT_interval)
      {
        DHT_Millis = DHT_Current_Millis; 
        delay(dht.getMinimumSamplingPeriod());
        float temperature = dht.getTemperature();
        float humidity = dht.getHumidity();
        if (isnan(temperature)) 
          {
          Serial.println("Failed reading temperature from DHT");
          } 
          if (isnan(humidity)) 
          {
          Serial.println("Failed reading humidity from DHT");
          }       
        else
          {
          send(msgTemp.set(temperature, 1));
          send(msgHumi.set(humidity, 1));
          Serial.print("T: ");
          Serial.println(temperature);
          Serial.print("H: ");
          Serial.println(humidity);
          }
      }
        debouncer1.update();
          int value = debouncer1.read();
          if (value != oldValue1) 
          {
            // Send in the new value
            send(msgBut1.set(value==HIGH ? 1 : 0));
            oldValue1 = value;
          }
        debouncer_2.update();
        if (value_but_2 != oldValue_2) 
        { 
        if ( value_but_2==0)
          {
          state2 = !state2;
          send(msgbut2.set(state2));
          }
        oldValue_2 = value_but_2;
      }
    }   
    void receive(const MyMessage &message) 
    {
        if (message.type==V_STATUS) // We only expect one type of message from controller. But we better check anyway.
          {
          digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF); // Change relay state
          saveState(message.sensor, message.getBool()); // Store state in eeprom
          Serial.print("Incoming change for sensor:"); // Write some debug info
          Serial.print(message.sensor);
          Serial.print(", New status: ");
          Serial.println(message.getBool());
          } 
    }
    

  • Mod

    @moskovskiy82 the indentation after the first isnan clause is wrong. Not sure what your intention is, but it looks fishy.

    Debug output will probably make it easy to spot what is wrong.



  • @mfalkvidd It's hard to catch. Otherwise usually looks normal



  • If you are having trouble reading the serial prints you could send a msg to controller stating the value as 100 degrees/100% - this would then give peakson the graph to indicate errors instead of trying to get the serial.

    I am not sure (and as I have only been attempting to program arduinos for a month or so, could be very wrong :) ) but I don't think your if/else statement is structured correctly and the else part is only linked to the if(isnan(temparature)). If

    You could try separate if/else for temp and humidity, i.e.

    if(isnan(temparature) {
    ...error msg
    }
    else {
    ...send temp message
    }
    
    If(isnan(humidity) {
    ...error msg
    }
    else {
    ...send humidity msg
    }
    
    

    I'm also not sure if you need this line:
    delay(dht.getMinimumSamplingPeriod());

    As you are only sampling every 60 seconds anyway...


Log in to reply
 

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