Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. dht22 with relay irregular measurement and operation

dht22 with relay irregular measurement and operation

Scheduled Pinned Locked Moved Development
3 Posts 2 Posters 1.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DickD Offline
    DickD Offline
    Dick
    wrote on last edited by
    #1

    Double dht22 with relay is working good but the 2 DHT's give logging irregular
    H1= 50.20
    H0= 75.70
    H0= 75.80
    T1= 23.90
    H1= 50.30
    T0= 21.40
    H0= 75.70
    T0= 21.50
    H0= 75.90
    T0= 21.40
    H0= 75.80

    is it possible to measure in an sequence? Also the relay cannot be used by the button everytime I like, also immediate after the measurement it can be used. is there something wrong in the delay?
    Here the code as it is now

    #include <SPI.h>
    #include <MySensor.h>
    #include <DHT.h>
    #include <Bounce2.h>
    
    #define CHILD_ID_HUM1 0
    #define CHILD_ID_HUM2 1
    #define CHILD_ID_TEMP1 3
    #define CHILD_ID_TEMP2 4
    
    // RelayWithActuator stuff
    #define RELAY_PIN  6  // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN  7  // Arduino Digital I/O pin number for button 
    #define CHILD_ID 5   // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    Bounce debouncer = Bounce(); 
    bool state;
    MySensor gw;
    MyMessage msg(CHILD_ID,V_LIGHT);
    
    #define HEARTBEAT       10
    unsigned int     timer = 0;
    // Sleep time between reads (in milliseconds)
    #define SLEEP_TIME      3000 
    
    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(incomingMessage, AUTO, true);
      for (int i = 0; i < 2; i++)
      {
        dht[i] = new DHT;
        dht[i]->setup(sensorPin[i]);
      }
    
      gw.sendSketchInfo("HumidityAndRelay", "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);
    
       // Setup the button and Activate internal pull-up
      pinMode(BUTTON_PIN,INPUT_PULLUP);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_LIGHT);
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT);   
          
      // Set relay to last known state (using eeprom storage) 
      state = gw.loadState(CHILD_ID);
      digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
     
      timer = 0;
      metric = gw.getConfig().isMetric;
    }
    
    void loop()
    {
      gw.process(); 
      timer++;
      
      if (timer > (SLEEP_TIME / HEARTBEAT))   
      { 
        // Reset timer again and for the next timed loop
        timer = 0;
        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);
          }
        }
      } 
      
      // Get the button update state (true if a change was detected) 
      if (debouncer.update() && debouncer.read() == 0) {
          gw.send(msg.set(state?false:true), true); // Send new state and request ack back
      }
      
      gw.wait(HEARTBEAT); //sleep a bit
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_LIGHT) {
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(CHILD_ID, state);
        
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    
    1 Reply Last reply
    0
    • DickD Offline
      DickD Offline
      Dick
      wrote on last edited by
      #2

      somebody any suggestions?

      1 Reply Last reply
      0
      • gundark2G Offline
        gundark2G Offline
        gundark2
        wrote on last edited by gundark2
        #3

        So I'm new to all this, however anytime I get funky things going on when I have multiple sensors on a node I found that introducing a delay in between the readings helped smooth things out. Hopefully someone will correct me if I'm wrong, but I have had some luck with delays. Also I built one to control my hot tub and I found noise from the pump messed things up, de-coupling capacitors became my friend after that.

        1 Reply Last reply
        1

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        16

        Online

        12.0k

        Users

        11.2k

        Topics

        113.4k

        Posts


        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • MySensors
        • OpenHardware.io
        • Categories
        • Recent
        • Tags
        • Popular