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. Troubleshooting
  3. Multisensor PIR problem [solved]

Multisensor PIR problem [solved]

Scheduled Pinned Locked Moved Troubleshooting
53 Posts 7 Posters 26.3k 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.
  • A Offline
    A Offline
    andriej
    wrote on last edited by
    #2

    " delay(dht.getMinimumSamplingPeriod());" is the problem - it delays whole sketch every run of loop()

    :-)

    N 1 Reply Last reply
    0
    • N Offline
      N Offline
      n3ro
      wrote on last edited by
      #3

      PERFECT!!!!!!!!!!! It works now :):):)

      Big THX!

      pimatic + MySensors + Homeduino + z-way
      https://github.com/n3roGit/MySensors_n3ro

      1 Reply Last reply
      0
      • A andriej

        " delay(dht.getMinimumSamplingPeriod());" is the problem - it delays whole sketch every run of loop()

        N Offline
        N Offline
        n3ro
        wrote on last edited by
        #4

        @andriej hey,

        the PIR works perfect now, but the DHT didnt update... Only the first startup of the arduino works:

        ---------- Battery: 100
        ---------- PIR: 1
        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:1
        send: 10-10-0-0 s=3,c=1,t=0,pt=7,l=5,st=ok:20.5
        ---------- Temp: 20.50
        send: 10-10-0-0 s=2,c=1,t=1,pt=7,l=5,st=ok:34.6
        ---------- Humidity: 34.60
        send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:87
        ---------- Light: 87
        ---------- Battery: 100
        ---------- PIR: 0
        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
        ---------- Battery: 100
        ---------- PIR: 1
        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:1
        send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:84
        ---------- Light: 84
        ---------- Battery: 100
        ---------- PIR: 0
        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
        send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:88
        ---------- Light: 88
        ---------- Battery: 100
        ---------- PIR: 1
        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:1
        send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:83
        ---------- Light: 83
        ---------- Battery: 100
        ---------- PIR: 0
        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
        send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:87
        ---------- Light: 87
        ---------- Battery: 100
        ---------- PIR: 1

        pimatic + MySensors + Homeduino + z-way
        https://github.com/n3roGit/MySensors_n3ro

        S 1 Reply Last reply
        0
        • N n3ro

          @andriej hey,

          the PIR works perfect now, but the DHT didnt update... Only the first startup of the arduino works:

          ---------- Battery: 100
          ---------- PIR: 1
          send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:1
          send: 10-10-0-0 s=3,c=1,t=0,pt=7,l=5,st=ok:20.5
          ---------- Temp: 20.50
          send: 10-10-0-0 s=2,c=1,t=1,pt=7,l=5,st=ok:34.6
          ---------- Humidity: 34.60
          send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:87
          ---------- Light: 87
          ---------- Battery: 100
          ---------- PIR: 0
          send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
          ---------- Battery: 100
          ---------- PIR: 1
          send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:1
          send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:84
          ---------- Light: 84
          ---------- Battery: 100
          ---------- PIR: 0
          send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
          send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:88
          ---------- Light: 88
          ---------- Battery: 100
          ---------- PIR: 1
          send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:1
          send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:83
          ---------- Light: 83
          ---------- Battery: 100
          ---------- PIR: 0
          send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
          send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:87
          ---------- Light: 87
          ---------- Battery: 100
          ---------- PIR: 1

          S Offline
          S Offline
          Sparkman
          Hero Member
          wrote on last edited by
          #5

          @n3ro the DHT needs a delay between polling. You are likely polling it too fast now. Rather than remove the delay command, you need to use a different method to not poll the DHT too frequently. The millis command is one option: http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

          Cheers
          Al

          1 Reply Last reply
          0
          • N Offline
            N Offline
            n3ro
            wrote on last edited by
            #6

            ufff. to build a multisensor sketch is really hard :-/

            thx for your info!

            pimatic + MySensors + Homeduino + z-way
            https://github.com/n3roGit/MySensors_n3ro

            1 Reply Last reply
            0
            • N Offline
              N Offline
              n3ro
              wrote on last edited by
              #7

              can somebody help me with my sketch? i dont understand millis :-/

              pimatic + MySensors + Homeduino + z-way
              https://github.com/n3roGit/MySensors_n3ro

              S 1 Reply Last reply
              0
              • N n3ro

                can somebody help me with my sketch? i dont understand millis :-/

                S Offline
                S Offline
                Sparkman
                Hero Member
                wrote on last edited by Sparkman
                #8

                @n3ro Hard to help you with your sketch since you don't have it posted ;-)

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  n3ro
                  wrote on last edited by
                  #9

                  @n3ro said:

                  https://github.com/n3roGit/MySensors_n3ro/tree/master/MotionSensor_DHT_light_battery_mod

                  ;)

                  pimatic + MySensors + Homeduino + z-way
                  https://github.com/n3roGit/MySensors_n3ro

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    torfinn
                    wrote on last edited by
                    #10

                    Hi ! I had the same problem. Seems like gw.sleep(some_number_of_mills); lets you loose the init of the dht. Just put dht.begin(); in your loop before you read the value and it should work.

                    example.
                    void loop ()
                    {
                    dht.begin();
                    float temperature = dht.readTemperature();
                    ......

                    N 1 Reply Last reply
                    0
                    • T torfinn

                      Hi ! I had the same problem. Seems like gw.sleep(some_number_of_mills); lets you loose the init of the dht. Just put dht.begin(); in your loop before you read the value and it should work.

                      example.
                      void loop ()
                      {
                      dht.begin();
                      float temperature = dht.readTemperature();
                      ......

                      N Offline
                      N Offline
                      n3ro
                      wrote on last edited by
                      #11

                      @torfinn said:

                      dht.begin();

                      Hey torfinn,

                      i get this error when compiling:
                      MotionSensor_DHT_light_battery_mod.ino: In function 'void loop()':
                      MotionSensor_DHT_light_battery_mod.ino:109:7: error: 'class DHT' has no member named 'begin'
                      Fehler beim Kompilieren.

                      pimatic + MySensors + Homeduino + z-way
                      https://github.com/n3roGit/MySensors_n3ro

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        n3ro
                        wrote on last edited by n3ro
                        #12

                        I have a idea,

                        is it possible to build a second loop like this?
                        But now i have one problem... the loop2 never run...

                        #include <MySensor.h>
                        #include <readVcc.h>
                        #include <SPI.h>
                        #include <DHT.h>
                        
                        #include <Scheduler.h>
                        
                        
                        
                        
                        #define NODE_ID 11                       // ID of node
                        unsigned long SLEEP_TIME = 600000;        // Sleep time between reports (in milliseconds)
                        
                        #define CHILD_ID_PIR 1                   // Id of the sensor PIR
                        #define CHILD_ID_HUM 2                   // Id of the sensor HUM
                        #define CHILD_ID_TEMP 3                  // Id of the sensor TEMP
                        #define CHILD_ID_LIGHT 4                 // Id of the sensor LIGHT
                        
                        #define PIR_SENSOR_DIGITAL 3           // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
                        #define INTERRUPT PIR_SENSOR_DIGITAL-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
                        #define HUMIDITY_SENSOR_DIGITAL_PIN 2
                        #define LIGHT_SENSOR_ANALOG_PIN 0
                        
                        MySensor gw;
                        // Initialize Variables
                        MyMessage msgPir(CHILD_ID_PIR, V_TRIPPED);
                        MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                        MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                        MyMessage msgLight(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                        
                        DHT dht;
                        float lastTemp;
                        float lastHum;
                        boolean metric = true;
                        int oldBatteryPcnt;
                        int lastLightLevel;
                        unsigned long time;
                        
                        
                        int MIN_V = 2700; // empty voltage (0%)
                        int MAX_V = 3200; // full voltage (100%)
                        
                        void setup()
                        {
                          gw.begin(NULL, NODE_ID, false);
                        
                          //PIR
                          // Send the sketch version information to the gateway and Controller
                          gw.sendSketchInfo("Motion Sensor", "1.0");
                        
                          pinMode(PIR_SENSOR_DIGITAL, INPUT);      // sets the motion sensor digital pin as input
                          digitalWrite(PIR_SENSOR_DIGITAL, HIGH);
                          // Register all sensors to gw (they will be created as child devices)
                          gw.present(CHILD_ID_PIR, S_MOTION);
                        
                          //DHT
                          dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
                        
                          // Send the Sketch Version Information to the Gateway
                          gw.sendSketchInfo("Humidity", "1.0");
                        
                          // Register all sensors to gw (they will be created as child devices)
                          gw.present(CHILD_ID_HUM, S_HUM);
                          gw.present(CHILD_ID_TEMP, S_TEMP);
                        
                          metric = gw.getConfig().isMetric;
                        
                          //LIGHT
                          // Send the sketch version information to the gateway and Controller
                          gw.sendSketchInfo("Light Sensor", "1.0");
                        
                          // Register all sensors to gateway (they will be created as child devices)
                          gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                          
                          Scheduler.startLoop(loop2);
                        
                        }
                        
                        void loop()
                        {
                          // Read digital motion value
                          boolean tripped = digitalRead(PIR_SENSOR_DIGITAL) == HIGH;
                          Serial.print("---------- PIR: ");
                          Serial.println(tripped);
                          gw.send(msgPir.set(tripped ? "1" : "0")); // Send tripped value to gw
                        
                          // Sleep until interrupt comes in on motion sensor. Send update every two minute.
                          gw.sleep(INTERRUPT, CHANGE, SLEEP_TIME);
                        }
                        void loop2()
                        {
                          //DHT
                          //dht.begin();
                          delay(dht.getMinimumSamplingPeriod());
                          //delay(1000);
                          //millis(dht.getMinimumSamplingPeriod());
                          //Serial.print(dht.getMinimumSamplingPeriod());
                        
                          float temperature = dht.getTemperature();
                          if (isnan(temperature)) {
                            Serial.println("Failed reading temperature from DHT");
                          } else if (temperature != lastTemp) {
                            lastTemp = temperature;
                            if (!metric) {
                              temperature = dht.toFahrenheit(temperature);
                            }
                            gw.send(msgTemp.set(temperature, 1));
                            Serial.print("---------- Temp: ");
                            Serial.println(temperature);
                          }
                        
                          float humidity = dht.getHumidity();
                          if (isnan(humidity)) {
                            Serial.println("Failed reading humidity from DHT");
                          } else if (humidity != lastHum) {
                            lastHum = humidity;
                            gw.send(msgHum.set(humidity, 1));
                            Serial.print("---------- Humidity: ");
                            Serial.println(humidity);
                          }
                        
                          // Light
                          int lightLevel = (1023 - analogRead(LIGHT_SENSOR_ANALOG_PIN)) / 10.23;
                          //Serial.println(lightLevel);
                          if (lightLevel != lastLightLevel) {
                            gw.send(msgLight.set(lightLevel));
                            lastLightLevel = lightLevel;
                            Serial.print("---------- Light: ");
                            Serial.println(lightLevel);
                          }
                        
                          // Measure battery
                          float batteryV = readVcc();
                          int batteryPcnt = (((batteryV - MIN_V) / (MAX_V - MIN_V)) * 100 );
                          if (batteryPcnt > 100) {
                            batteryPcnt = 100;
                          }
                        
                          if (batteryPcnt != oldBatteryPcnt) {
                            gw.sendBatteryLevel(batteryPcnt); // Send battery percentage
                            oldBatteryPcnt = batteryPcnt;
                          }
                          Serial.print("---------- Battery: ");
                          Serial.println(batteryPcnt);
                          gw.sleep(SLEEP_TIME);
                        }
                        

                        i have downloaded THIS

                        But i get this error when compiling:

                        In file included from MotionSensor_DHT_light_battery_mod.ino:6:0:
                        Arduino\libraries\Scheduler/Scheduler.h:33:22: fatal error: WProgram.h: No such file or directory
                        #include <WProgram.h>

                        pimatic + MySensors + Homeduino + z-way
                        https://github.com/n3roGit/MySensors_n3ro

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          rvendrame
                          Hero Member
                          wrote on last edited by
                          #13

                          @n3ro , you can have only one loop().

                          You need to merge the two logics into into a single loop. You might also want to remove the second sleep, otherwise PIR will not trigger there.

                          Home Assistant / Vera Plus UI7
                          ESP8266 GW + mySensors 2.3.2
                          Alexa / Google Home

                          N 1 Reply Last reply
                          0
                          • R rvendrame

                            @n3ro , you can have only one loop().

                            You need to merge the two logics into into a single loop. You might also want to remove the second sleep, otherwise PIR will not trigger there.

                            N Offline
                            N Offline
                            n3ro
                            wrote on last edited by
                            #14

                            @rvendrame do you have an idea how to do it?

                            pimatic + MySensors + Homeduino + z-way
                            https://github.com/n3roGit/MySensors_n3ro

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              rvendrame
                              Hero Member
                              wrote on last edited by
                              #15

                              Maybe you can start by to removing these 3 lines:

                              }
                              void loop2()
                              {
                              
                              

                              ... and move the 1st sleep to the end of loop (where the 2nd sleep is):

                              // Sleep until interrupt comes in on motion sensor. Send update every two minute.
                               gw.sleep(INTERRUPT, CHANGE, SLEEP_TIME);
                              
                              

                              .... and remove the 2nd sleep:

                              gw.sleep(SLEEP_TIME);
                              

                              Home Assistant / Vera Plus UI7
                              ESP8266 GW + mySensors 2.3.2
                              Alexa / Google Home

                              N 1 Reply Last reply
                              0
                              • R rvendrame

                                Maybe you can start by to removing these 3 lines:

                                }
                                void loop2()
                                {
                                
                                

                                ... and move the 1st sleep to the end of loop (where the 2nd sleep is):

                                // Sleep until interrupt comes in on motion sensor. Send update every two minute.
                                 gw.sleep(INTERRUPT, CHANGE, SLEEP_TIME);
                                
                                

                                .... and remove the 2nd sleep:

                                gw.sleep(SLEEP_TIME);
                                
                                N Offline
                                N Offline
                                n3ro
                                wrote on last edited by
                                #16

                                @rvendrame
                                if i use this:

                                #include <MySensor.h>
                                #include <readVcc.h>
                                #include <SPI.h>
                                #include <DHT.h>
                                
                                
                                #define NODE_ID 10                       // ID of node
                                unsigned long SLEEP_TIME = 600000;        // Sleep time between reports (in milliseconds)
                                
                                #define CHILD_ID_PIR 1                   // Id of the sensor PIR
                                #define CHILD_ID_HUM 2                   // Id of the sensor HUM
                                #define CHILD_ID_TEMP 3                  // Id of the sensor TEMP
                                #define CHILD_ID_LIGHT 4                 // Id of the sensor LIGHT
                                
                                #define PIR_SENSOR_DIGITAL 3           // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
                                #define INTERRUPT PIR_SENSOR_DIGITAL-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
                                #define HUMIDITY_SENSOR_DIGITAL_PIN 2
                                #define LIGHT_SENSOR_ANALOG_PIN 0
                                
                                MySensor gw;
                                // Initialize Variables
                                MyMessage msgPir(CHILD_ID_PIR, V_TRIPPED);
                                MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                                MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                                MyMessage msgLight(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                                
                                DHT dht;
                                float lastTemp;
                                float lastHum;
                                boolean metric = true;
                                int oldBatteryPcnt;
                                int lastLightLevel;
                                
                                
                                int MIN_V = 2700; // empty voltage (0%)
                                int MAX_V = 3200; // full voltage (100%)
                                
                                void setup()
                                {
                                  gw.begin(NULL, NODE_ID, false);
                                
                                  //PIR
                                  // Send the sketch version information to the gateway and Controller
                                  gw.sendSketchInfo("Motion Sensor", "1.0");
                                
                                  pinMode(PIR_SENSOR_DIGITAL, INPUT);      // sets the motion sensor digital pin as input
                                  digitalWrite(PIR_SENSOR_DIGITAL, HIGH);
                                  // Register all sensors to gw (they will be created as child devices)
                                  gw.present(CHILD_ID_PIR, S_MOTION);
                                  
                                  //DHT
                                  dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
                                
                                  // Send the Sketch Version Information to the Gateway
                                  gw.sendSketchInfo("Humidity", "1.0");
                                
                                  // Register all sensors to gw (they will be created as child devices)
                                  gw.present(CHILD_ID_HUM, S_HUM);
                                  gw.present(CHILD_ID_TEMP, S_TEMP);
                                
                                  metric = gw.getConfig().isMetric;
                                  
                                  //LIGHT
                                  // Send the sketch version information to the gateway and Controller
                                  gw.sendSketchInfo("Light Sensor", "1.0");
                                
                                  // Register all sensors to gateway (they will be created as child devices)
                                  gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                                
                                }
                                
                                void loop()
                                {
                                
                                  // Measure battery
                                  float batteryV = readVcc();
                                  int batteryPcnt = (((batteryV - MIN_V) / (MAX_V - MIN_V)) * 100 );
                                  if (batteryPcnt > 100) {
                                    batteryPcnt = 100;
                                  }
                                
                                  if (batteryPcnt != oldBatteryPcnt) {
                                    gw.sendBatteryLevel(batteryPcnt); // Send battery percentage
                                    oldBatteryPcnt = batteryPcnt;
                                  }
                                  Serial.print("---------- Battery: ");
                                  Serial.println(batteryPcnt);
                                
                                  // Read digital motion value
                                  boolean tripped = digitalRead(PIR_SENSOR_DIGITAL) == HIGH;
                                  Serial.print("---------- PIR: ");
                                  Serial.println(tripped);
                                  gw.send(msgPir.set(tripped ? "1" : "0")); // Send tripped value to gw
                                
                                
                                  //DHT
                                  //delay(dht.getMinimumSamplingPeriod());
                                
                                  float temperature = dht.getTemperature();
                                  if (isnan(temperature)) {
                                    Serial.println("Failed reading temperature from DHT");
                                  } else if (temperature != lastTemp) {
                                    lastTemp = temperature;
                                    if (!metric) {
                                      temperature = dht.toFahrenheit(temperature);
                                    }
                                    gw.send(msgTemp.set(temperature, 1));
                                    Serial.print("---------- Temp: ");
                                    Serial.println(temperature);
                                  }
                                
                                  float humidity = dht.getHumidity();
                                  if (isnan(humidity)) {
                                    Serial.println("Failed reading humidity from DHT");
                                  } else if (humidity != lastHum) {
                                    lastHum = humidity;
                                    gw.send(msgHum.set(humidity, 1));
                                    Serial.print("---------- Humidity: ");
                                    Serial.println(humidity);
                                  }
                                  // Light
                                  int lightLevel = (1023 - analogRead(LIGHT_SENSOR_ANALOG_PIN)) / 10.23;
                                  //Serial.println(lightLevel);
                                  if (lightLevel != lastLightLevel) {
                                    gw.send(msgLight.set(lightLevel));
                                    lastLightLevel = lightLevel;
                                    Serial.print("---------- Light: ");
                                    Serial.println(lightLevel);
                                  }
                                  // Sleep until interrupt comes in on motion sensor. Send update every two minute.
                                  gw.sleep(INTERRUPT, CHANGE, SLEEP_TIME);
                                }
                                

                                the pir works fine. but the dht doesn't work.

                                when delay(dht.getMinimumSamplingPeriod()); is used the dht works and the pir fails.

                                pimatic + MySensors + Homeduino + z-way
                                https://github.com/n3roGit/MySensors_n3ro

                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  rvendrame
                                  Hero Member
                                  wrote on last edited by
                                  #17

                                  It makes sense ;-) Try this:

                                  create (outside the loop) a variable to count the time:

                                  long mytime = 0; 
                                  

                                  .. and replace the delay by :

                                   if ( milis() > mytime )  { 
                                  
                                     <  Put the dht stuff here > 
                                  
                                    mytime = millis() + dht.getMinimumSamplingPeriod() ; 
                                  
                                  }
                                  

                                  Home Assistant / Vera Plus UI7
                                  ESP8266 GW + mySensors 2.3.2
                                  Alexa / Google Home

                                  N 1 Reply Last reply
                                  0
                                  • R rvendrame

                                    It makes sense ;-) Try this:

                                    create (outside the loop) a variable to count the time:

                                    long mytime = 0; 
                                    

                                    .. and replace the delay by :

                                     if ( milis() > mytime )  { 
                                    
                                       <  Put the dht stuff here > 
                                    
                                      mytime = millis() + dht.getMinimumSamplingPeriod() ; 
                                    
                                    }
                                    
                                    N Offline
                                    N Offline
                                    n3ro
                                    wrote on last edited by
                                    #18

                                    @rvendrame

                                    hmm :)
                                    if is use this:

                                    #include <MySensor.h>
                                    #include <readVcc.h>
                                    #include <SPI.h>
                                    #include <DHT.h>
                                    
                                    
                                    #define NODE_ID 10                       // ID of node
                                    unsigned long SLEEP_TIME = 10000;        // Sleep time between reports (in milliseconds)
                                    
                                    #define CHILD_ID_PIR 1                   // Id of the sensor PIR
                                    #define CHILD_ID_HUM 2                   // Id of the sensor HUM
                                    #define CHILD_ID_TEMP 3                  // Id of the sensor TEMP
                                    #define CHILD_ID_LIGHT 4                 // Id of the sensor LIGHT
                                    
                                    #define PIR_SENSOR_DIGITAL 3           // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
                                    #define INTERRUPT PIR_SENSOR_DIGITAL-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
                                    #define HUMIDITY_SENSOR_DIGITAL_PIN 2
                                    #define LIGHT_SENSOR_ANALOG_PIN 0
                                    
                                    MySensor gw;
                                    // Initialize Variables
                                    MyMessage msgPir(CHILD_ID_PIR, V_TRIPPED);
                                    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                                    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                                    MyMessage msgLight(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                                    
                                    DHT dht;
                                    float lastTemp;
                                    float lastHum;
                                    boolean metric = true;
                                    int oldBatteryPcnt;
                                    int lastLightLevel;
                                    long mytime = 0;
                                    
                                    
                                    int MIN_V = 2700; // empty voltage (0%)
                                    int MAX_V = 3200; // full voltage (100%)
                                    
                                    void setup()
                                    {
                                      gw.begin(NULL, NODE_ID, false);
                                    
                                      //PIR
                                      // Send the sketch version information to the gateway and Controller
                                      gw.sendSketchInfo("Motion Sensor", "1.0");
                                    
                                      pinMode(PIR_SENSOR_DIGITAL, INPUT);      // sets the motion sensor digital pin as input
                                      digitalWrite(PIR_SENSOR_DIGITAL, HIGH);
                                      // Register all sensors to gw (they will be created as child devices)
                                      gw.present(CHILD_ID_PIR, S_MOTION);
                                    
                                      //DHT
                                      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
                                    
                                      // Send the Sketch Version Information to the Gateway
                                      gw.sendSketchInfo("Humidity", "1.0");
                                    
                                      // Register all sensors to gw (they will be created as child devices)
                                      gw.present(CHILD_ID_HUM, S_HUM);
                                      gw.present(CHILD_ID_TEMP, S_TEMP);
                                    
                                      metric = gw.getConfig().isMetric;
                                    
                                      //LIGHT
                                      // Send the sketch version information to the gateway and Controller
                                      gw.sendSketchInfo("Light Sensor", "1.0");
                                    
                                      // Register all sensors to gateway (they will be created as child devices)
                                      gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                                    
                                    }
                                    
                                    void loop()
                                    {
                                    
                                    
                                    
                                      // Read digital motion value
                                      boolean tripped = digitalRead(PIR_SENSOR_DIGITAL) == HIGH;
                                      Serial.print("---------- PIR: ");
                                      Serial.println(tripped);
                                      gw.send(msgPir.set(tripped ? "1" : "0")); // Send tripped value to gw
                                    
                                    
                                      if ( millis() > mytime )  {
                                    
                                        //DHT
                                        delay(dht.getMinimumSamplingPeriod());
                                    
                                        float temperature = dht.getTemperature();
                                        if (isnan(temperature)) {
                                          Serial.println("Failed reading temperature from DHT");
                                        } else if (temperature != lastTemp) {
                                          lastTemp = temperature;
                                          if (!metric) {
                                            temperature = dht.toFahrenheit(temperature);
                                          }
                                          gw.send(msgTemp.set(temperature, 1));
                                          Serial.print("---------- Temp: ");
                                          Serial.println(temperature);
                                        }
                                    
                                        float humidity = dht.getHumidity();
                                        if (isnan(humidity)) {
                                          Serial.println("Failed reading humidity from DHT");
                                        } else if (humidity != lastHum) {
                                          lastHum = humidity;
                                          gw.send(msgHum.set(humidity, 1));
                                          Serial.print("---------- Humidity: ");
                                          Serial.println(humidity);
                                        }
                                        // Light
                                        int lightLevel = (1023 - analogRead(LIGHT_SENSOR_ANALOG_PIN)) / 10.23;
                                        //Serial.println(lightLevel);
                                        if (lightLevel != lastLightLevel) {
                                          gw.send(msgLight.set(lightLevel));
                                          lastLightLevel = lightLevel;
                                          Serial.print("---------- Light: ");
                                          Serial.println(lightLevel);
                                        }
                                        // Measure battery
                                        float batteryV = readVcc();
                                        int batteryPcnt = (((batteryV - MIN_V) / (MAX_V - MIN_V)) * 100 );
                                        if (batteryPcnt > 100) {
                                          batteryPcnt = 100;
                                        }
                                    
                                        if (batteryPcnt != oldBatteryPcnt) {
                                          gw.sendBatteryLevel(batteryPcnt); // Send battery percentage
                                          oldBatteryPcnt = batteryPcnt;
                                        }
                                        Serial.print("---------- Battery: ");
                                        Serial.println(batteryPcnt);
                                    
                                        mytime = millis() + dht.getMinimumSamplingPeriod();
                                     
                                      }
                                         Serial.println(mytime);
                                      // Sleep until interrupt comes in on motion sensor. Send update every two minute.
                                      gw.sleep(INTERRUPT, CHANGE, SLEEP_TIME);
                                    }
                                    

                                    i get this output:
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    6608
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    6608
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    6608
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    6608
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    send: 10-10-255-255 s=255,c=3,t=7,pt=0,l=0,st=fail:
                                    send: 10-10-0-0 s=2,c=1,t=1,pt=7,l=5,st=fail:41.7
                                    ---------- Humidity: 41.70
                                    send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=fail:38
                                    ---------- Light: 38
                                    ---------- Battery: 100
                                    11148
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    11148
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    11148
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    11148
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    11148
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    send: 10-10-255-255 s=255,c=3,t=7,pt=0,l=0,st=fail:
                                    ---------- Battery: 100
                                    15441
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    15441
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    15441
                                    ---------- PIR: 0
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                    15441
                                    ---------- PIR: 1
                                    send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:1

                                    the pir sends 0 and didnt sleep right :(

                                    pimatic + MySensors + Homeduino + z-way
                                    https://github.com/n3roGit/MySensors_n3ro

                                    1 Reply Last reply
                                    0
                                    • R Offline
                                      R Offline
                                      rvendrame
                                      Hero Member
                                      wrote on last edited by
                                      #19

                                      What is this number? Did you print the milis()?

                                      send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                      6608 <<<<<<

                                      Home Assistant / Vera Plus UI7
                                      ESP8266 GW + mySensors 2.3.2
                                      Alexa / Google Home

                                      N 1 Reply Last reply
                                      0
                                      • R rvendrame

                                        What is this number? Did you print the milis()?

                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=fail:0
                                        6608 <<<<<<

                                        N Offline
                                        N Offline
                                        n3ro
                                        wrote on last edited by
                                        #20

                                        @n3ro said:

                                        Serial.println(mytime);

                                        yes :)
                                        Serial.println(mytime);

                                        pimatic + MySensors + Homeduino + z-way
                                        https://github.com/n3roGit/MySensors_n3ro

                                        1 Reply Last reply
                                        0
                                        • R Offline
                                          R Offline
                                          rvendrame
                                          Hero Member
                                          wrote on last edited by rvendrame
                                          #21
                                          #define PIR_SENSOR_DIGITAL 3           // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
                                          #define INTERRUPT PIR_SENSOR_DIGITAL-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
                                          #define HUMIDITY_SENSOR_DIGITAL_PIN 2
                                          #define LIGHT_SENSOR_ANALOG_PIN 0
                                          

                                          Are you connecting both DHT and PIR at same pin 2 ? That must be the cause (DHT is triggering the interruption, I think you want only the PIR to trigger it).

                                          Edit: looks like your PIR is at pin 3 , and DHT on pin 2 --- Should be the inverse? The PIR should be on pin 2 (to trigger the interrupt).

                                          Home Assistant / Vera Plus UI7
                                          ESP8266 GW + mySensors 2.3.2
                                          Alexa / Google Home

                                          SweebeeS 1 Reply Last reply
                                          0

                                          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


                                          12

                                          Online

                                          12.1k

                                          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