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. General Discussion
  3. Sampling rate for sensor when combined

Sampling rate for sensor when combined

Scheduled Pinned Locked Moved General Discussion
4 Posts 2 Posters 911 Views 2 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.
  • M Offline
    M Offline
    moskovskiy82
    wrote on last edited by
    #1

    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);  
      }
    }```
    1 Reply Last reply
    0
    • sundberg84S Offline
      sundberg84S Offline
      sundberg84
      Hardware Contributor
      wrote on last edited by
      #2

      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.

      Controller: Proxmox VM - Home Assistant
      MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
      MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
      RFLink GW - Arduino Mega + RFLink Shield, 433mhz

      1 Reply Last reply
      0
      • M Offline
        M Offline
        moskovskiy82
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • sundberg84S Offline
          sundberg84S Offline
          sundberg84
          Hardware Contributor
          wrote on last edited by sundberg84
          #4
          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()

          Controller: Proxmox VM - Home Assistant
          MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
          MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
          RFLink GW - Arduino Mega + RFLink Shield, 433mhz

          1 Reply Last reply
          1
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          23

          Online

          11.7k

          Users

          11.2k

          Topics

          113.1k

          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