[SOLVED] Too frequent updates form DHT



  • Hi.
    I just managed to convert one of my ESP nodes to mysensors, but I got some interesting problems- Temp sensor sends updates very quickly, in my controllers I can see temperature changing back and forth all the time.

    Here is my sketch. It is combination from humidity sensor example from Mysensors examples and relays with switches from Openhardware.io

    My code (you probably will notice right away that I'm no good with this, so any advice is appreciated)

    #include <ArduinoOTA.h>
    
    // Enable debug prints to serial monitor
    //#define MY_DEBUG
    
    // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
    #define MY_BAUD_RATE 9600
    
    #define MY_GATEWAY_ESP8266
    
    #define MY_ESP8266_SSID "Paltes"
    #define MY_ESP8266_PASSWORD "virus.exe"
    
    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    #define MY_IP_ADDRESS 192,168,1,53
    
    // If using static ip you need to define Gateway and Subnet address as well
    #define MY_IP_GATEWAY_ADDRESS 192,168,1,1
    #define MY_IP_SUBNET_ADDRESS 255,255,255,0
    
    // The port to keep open on node server mode
    #define MY_PORT 5003
    
    // How many clients should be able to connect to this gateway (default 1)
    #define MY_GATEWAY_MAX_CLIENTS 3
    
    #if defined(MY_USE_UDP)
    #include <WiFiUDP.h>
    #else
    #include <ESP8266WiFi.h>
    #endif
    
    #include <DHT.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define MY_NODE_ID                  3
    #define MY_TRANSPORT_WAIT_READY_MS  200
    
    #define RELAY_PIN1    D1  // Arduino Digital I/O pin number for relay K1
    #define RELAY_PIN2    D2  // Arduino Digital I/O pin number for relay K2
    //#define RELAY_PIN3   // Arduino Digital I/O pin number for relay K3
    
    #define BUTTON_PIN1   D3  // Arduino Digital I/O pin for temp sensor
    #define BUTTON_PIN2   D5  // Arduino Digital I/O pin number for button relay K3
    //#define BUTTON_PIN3   // Arduino Digital I/O pin for PIR
    
    #define DHT_PIN       D4
    
    #define CHILD_ID1     5   // Id of the sensor child
    #define CHILD_ID2     6   // Id of the sensor child
    //#define CHILD_ID3   3   // Id of the sensor child
    #define CHILD_ID_TEMP 7
    #define CHILD_ID_HUM  8
    
    #define RELAY_ON      1
    #define RELAY_OFF     0
    
    #define SENSOR_TEMP_OFFSET 0
    static const uint64_t UPDATE_INTERVAL = 60000;
    static const uint8_t FORCE_UPDATE_N_READS = 100;
    
    float lastTemp;
    float lastHum;
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    boolean metric = true; 
    
    Bounce debouncer1 = Bounce();
    Bounce debouncer2 = Bounce();
    //Bounce debouncer3 = Bounce();
    
    int oldValue1 = 0;
    int oldValue2 = 0;
    //int oldValue3 = 0;
    
    int state;
    bool initial_state_sent;
    
    MyMessage msg1(CHILD_ID1, V_STATUS);
    MyMessage msg2(CHILD_ID2, V_STATUS);
    //MyMessage msg3(CHILD_ID3, V_STATUS);
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    DHT dht;
    
    void changeState(int chiled, int newState) {
      //state = newState;
      //digitalWrite(pin, newState);
    
      saveState(chiled, newState);
      switch(chiled){
        case CHILD_ID1:
          digitalWrite(RELAY_PIN1, newState);
          send(msg1.set(newState));
          break;
        case CHILD_ID2:
          digitalWrite(RELAY_PIN2, newState);
          send(msg2.set(newState));
          break; /*
        case CHILD_ID3:
          digitalWrite(RELAY_PIN3, newState);
          send(msg3.set(newState));
          break;*/
        default:
          break;
      }
      
    }
    
    void before() {
      // Setup the button
      pinMode(BUTTON_PIN1, INPUT_PULLUP);
      pinMode(BUTTON_PIN2, INPUT_PULLUP);
      //pinMode(BUTTON_PIN3, INPUT_PULLUP);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN1, HIGH);
      digitalWrite(BUTTON_PIN2, HIGH);
      //digitalWrite(BUTTON_PIN3, HIGH);
    
      // After setting up the button, setup debouncer
      debouncer1.attach(BUTTON_PIN1);
      debouncer1.interval(5);
      debouncer2.attach(BUTTON_PIN2);
      debouncer2.interval(5);
      //debouncer3.attach(BUTTON_PIN3);
      //debouncer3.interval(5);
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN1, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN1, OUTPUT);
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN2, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN2, OUTPUT);
       // Make sure relays are off when starting up
      //digitalWrite(RELAY_PIN3, RELAY_OFF);
      // Then set relay pins in output mode
      //pinMode(RELAY_PIN3, OUTPUT);
      
      // digitalWrite(LED_PIN, RELAY_OFF);
      // // Then set relay pins in output mode
      // pinMode(LED_PIN, OUTPUT);
    }
    
    void setup()  {
      
      ArduinoOTA.onStart([]() {
        Serial.println("ArduinoOTA start");
      });
      ArduinoOTA.onEnd([]() {
        Serial.println("\nArduinoOTA end");
      });
      ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
        Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
      });
      ArduinoOTA.onError([](ota_error_t error) {
        Serial.printf("Error[%u]: ", error);
        if (error == OTA_AUTH_ERROR) {
          Serial.println("Auth Failed");
        } else if (error == OTA_BEGIN_ERROR) {
          Serial.println("Begin Failed");
        } else if (error == OTA_CONNECT_ERROR) {
          Serial.println("Connect Failed");
        } else if (error == OTA_RECEIVE_ERROR) {
          Serial.println("Receive Failed");
        } else if (error == OTA_END_ERROR) {
          Serial.println("End Failed");
        }
      });
      ArduinoOTA.begin();
      Serial.println("Ready");
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
    
      dht.setup(DHT_PIN);
      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()); 
        
      // Set relay to last known state (using eeprom storage) 
      int newState = loadState(CHILD_ID1);
      if (newState == 0Xff) {//if eeprom is empty
        newState = RELAY_OFF;
      }
      changeState(CHILD_ID1, newState);
      
      newState = loadState(CHILD_ID2);
      if (newState == 0Xff) {//if eeprom is empty
        newState = RELAY_OFF;
      }
      changeState(CHILD_ID2, newState);
    
      //newState = loadState(CHILD_ID3);
      //if (newState == 0Xff) {//if eeprom is empty
      //  newState = RELAY_OFF;
      //}
      //changeState(CHILD_ID3, newState);
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Koridors", "2.0");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID1, S_BINARY);
      present(CHILD_ID2, S_BINARY);
      //present(CHILD_ID3, S_BINARY);
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
    
      metric = getControllerConfig().isMetric;
    }
    
    void loop() {
    
      ArduinoOTA.handle();
      
      if (!initial_state_sent) {
        Serial.println("Sending initial value");
        send(msg1.set(loadState(CHILD_ID1) ? true : false));
        send(msg2.set(loadState(CHILD_ID2) ? true : false));
        //send(msg3.set(loadState(CHILD_ID3) ? true : false));
        
        initial_state_sent = true;
      }
    
      debouncer1.update();
      // Get the update value
      int value = debouncer1.read();
      if (value != oldValue1 && value==0) {
    	  //toggle switch state
    	  changeState(CHILD_ID1, loadState(CHILD_ID1) ? RELAY_OFF : RELAY_ON);
          //digitalWrite(RELAY_PIN, state ? RELAY_OFF : RELAY_ON);
          //send(msg.set(state?false:true)); // Send new state and request ack back
      }
      oldValue1 = value;
    
      debouncer2.update();
      // Get the update value
      value = debouncer2.read();
      if (value != oldValue2 && value==0) {
    	  //toggle switch state
    	  changeState(CHILD_ID2, loadState(CHILD_ID2) ? RELAY_OFF : RELAY_ON);
          //digitalWrite(RELAY_PIN, state ? RELAY_OFF : RELAY_ON);
          //send(msg.set(state?false:true)); // Send new state and request ack back
      }
      oldValue2 = value;
    /*
      debouncer3.update();
      // Get the update value
      value = debouncer3.read();
      if (value != oldValue3 && value==0) {
    	  //toggle switch state
    	  changeState(CHILD_ID3, loadState(CHILD_ID3) ? RELAY_OFF : RELAY_ON);
        //state = loadState(CHILD_ID3);
        //digitalWrite(RELAY_PIN3, state ? RELAY_OFF : RELAY_ON);
        //send(msg.set(state?false:true)); // Send new state and request ack back
      }
      oldValue3 = value; */
      
      // Force reading sensor, so it works also after sleep()
      dht.readSensor(true);
    
      // Fetch temperatures from DHT sensor
      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++;
      }
    
       // Fetch humidity from DHT sensor
      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); 
    } 
    
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
    #ifdef MY_DEBUG
         Serial.println("This is an ack from gateway");
    #endif
      }
    
      if (message.type == V_LIGHT) {
    #ifdef MY_DEBUG
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
    #endif
        // chnage the state of the related sensor
        changeState(message.sensor, message.getBool() ? RELAY_ON : RELAY_OFF);
       
         // Write some debug info
        
        
       } 
    }
    

    Here is a link to galery with some screensoots and videos thats kind of describe problem Google photos, hope you can see galery


  • Mod

    @archiijs if you look at the debug output of the node (which should always be the first step when troubleshooting) you'll probably see that the node does not sleep.

    That happens because sleep is not supported on esp8266. Try using wait instead.



  • @mfalkvidd said in Too frequent updates form DHT:

    because sleep is not supported on esp8266. Try using wait instead.

    Hi, and thanks for the response.
    Well, I'm not really interested in sleep, all my nodes are AC powered. Can you guide me to some example using wait, thanks in advance.


  • Mod

    @archiijs just replace the letters sleep with wait in your code.



  • @mfalkvidd said in Too frequent updates form DHT:

    wait

    That was easier than expected, thanks for your advice, seems to do the trick. Will apply this to all my nodes now and test.

    Thanks for your fast replays.


  • Mod

    @archiijs great! Thanks for reporting back.



  • @mfalkvidd
    Hi again.
    I got small problem that emerged after this change. When I try to use my wall switches nothing happens, I still can turn on/off all lights from controller without problems, only localy attached switches do not work and that is not an option.

    so the problem lies in this line, if I understand correctly

      // Sleep for a while to save energy
      sleep(UPDATE_INTERVAL); 
    

    if I change sleep to wait, so I'm back to the beginning now. Is there any way to fix this?


  • Mod

    @archiijs I would remove the wait/sleep and change

    else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS)
    

    to

    else if (temperature != lastTemp)
    

    That will cause the temperature to be sent whenever it has changed.

    There are several other ways to get a similar result. What is best depends on how you want the node to behave.



  • @mfalkvidd Thanks again for your response.
    I just changed else if functions as you suggested and uncommented wait/sleep(update_interval);
    Actually, I think that I had code like this couple days before...

    The result is kind of what is needed, I get new values as they change, BUT they change all the time. Like my temperature just changes true 21.7 to 21.6 to 21.7 to 21.8 an ower and ower again nonstop, same with humidity.

    EDIT: Just switched out DHT sensor for anorther and nothing has changed.


  • Mod

    @archiijs you either need to add som hysteresis (only send update if the temperature has changed with X amount, for example 1 degree) or a time limit (never send updates unless X time has passed since last send, for example 5 minutes)



  • @mfalkvidd That sounds about right. But sadly I can not find any examples of such sensors and I'm not sure if I could figure this out by myself 😕


  • Mod



  • @mfalkvidd Superb, I will study your suggestions and report back. Thanks again for your patience with me.



  • Hi @mfalkvidd and others.

    Yesterday after work I tried to get my head around code suggestions but somehow I cold not make them work so I did some google search and after some time I find something that I implemented in my nodes for testing.
    Interestingly it works as expected for two of my nodes, but one still keeps jumping around, not that often, but that's still strange, I will compare code to other two to see what I've done wrong there.
    So I used Hysteresis approach by doing this.

    #define HYSTERISIS    0.5        // Hysteresis for sensor 
    
    ...
    
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
        Serial.println("Failed reading temperature from DHT!");
      } else if (abs(temperature - lastTemp ) > HYSTERISIS) {
        // 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);
        }
    
    

    And the same for humidity.

    Still not sure that this will work as expected in long run, but will see.
    But now it is way better than before. Thanks.


Log in to reply
 

Suggested Topics

  • 4
  • 2
  • 15
  • 3
  • 8
  • 20

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts