Sampling rate for sensor when combined



  • I have combined Relay and MQ+DHT

    What i would like to do is set a sampling period for a sensor at say 5 seconds.
    Is this supposed to be done with millis? Can somebody help with the code sample?

    Let's presume for MQ as an exxample

    float mq_reading = analogRead(MQ_Pin);
      if (isnan(mq_reading)) 
      {
          Serial.println("Failed mq_reading");
      } 
      else if (mq_reading != last_mq_reading) 
      {
        last_mq_reading = mq_reading;
        gw.send(msgMQ.set(mq_reading, 1));
        Serial.print("MQ: ");
        Serial.println(mq_reading);  
      }
    }```

  • Hardware Contributor

    Hi!

    Yes - im using millis() if i dont sleep the node (which seems overkill if you only wants 5 seconds).
    What you need to find out if you sensors have the possibility to read that fast. For the dht i think thats about when you just run the normal reading without delay in the loop.



  • Can you give a code example? Currently it reports every second almost


  • Hardware Contributor

    long previousMillis = 0;        // will store last time something happened
    long interval = 5000;           // interval at which to blink (milliseconds)
    
    void loop()
    {
     unsigned long currentMillis = millis();
     
      if(currentMillis - previousMillis > interval) {
        // save the last time something happened
        previousMillis = currentMillis;   
     
    Insert your code here...
    
      }
    }
    

    Note millis() does not work if you are using gw.sleep()


Log in to reply
 

Suggested Topics

  • 4
  • 2
  • 1
  • 933
  • 9
  • 14

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts