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 22.7k 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.
  • rvendrameR Offline
    rvendrameR 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

    n3roN 1 Reply Last reply
    0
    • rvendrameR 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);
      
      n3roN Offline
      n3roN 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
      • rvendrameR Offline
        rvendrameR 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

        n3roN 1 Reply Last reply
        0
        • rvendrameR 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() ; 
          
          }
          
          n3roN Offline
          n3roN 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
          • rvendrameR Offline
            rvendrameR 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

            n3roN 1 Reply Last reply
            0
            • rvendrameR 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 <<<<<<

              n3roN Offline
              n3roN 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
              • rvendrameR Offline
                rvendrameR 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
                • rvendrameR rvendrame
                  #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).

                  SweebeeS Offline
                  SweebeeS Offline
                  Sweebee
                  wrote on last edited by
                  #22

                  @rvendrame Pir sensor is defined as 3

                  interrupt pin is defined as INT1: 3 - 2 = 1

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

                    So PIR must connect on PIN 1 and not PIN 3.

                    Edit: Better, how did you connect DHT and PIR?

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

                    n3roN SweebeeS 2 Replies Last reply
                    0
                    • rvendrameR rvendrame

                      So PIR must connect on PIN 1 and not PIN 3.

                      Edit: Better, how did you connect DHT and PIR?

                      n3roN Offline
                      n3roN Offline
                      n3ro
                      wrote on last edited by n3ro
                      #24

                      @rvendrame The DHT is on pin 2 and the pir on pin 3. i use the MYS PCB.

                      i suspect the delay function disturb the sleep function. if i dont use the delay, the dht cant "heat up".

                      far as I know the interrupt is only an internal value. right?

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

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

                        Yes, it looks fine. What exactly error are you having? From your first serial monitor, it looks like your sensor woke on every 10min aprox, ( 6608 / 11148 / 15441 ).

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

                        n3roN 1 Reply Last reply
                        0
                        • rvendrameR rvendrame

                          Yes, it looks fine. What exactly error are you having? From your first serial monitor, it looks like your sensor woke on every 10min aprox, ( 6608 / 11148 / 15441 ).

                          n3roN Offline
                          n3roN Offline
                          n3ro
                          wrote on last edited by n3ro
                          #26

                          @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);
                          }
                          

                          i get this output after starting:
                          sensor started, id 10
                          send: 10-10-0-0 s=255,c=0,t=17,pt=0,l=5,st=ok:1.4.1
                          send: 10-10-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
                          read: 0-0-10 s=255,c=3,t=6,pt=0,l=1:M
                          send: 10-10-0-0 s=255,c=3,t=11,pt=0,l=13,st=ok:Motion Sensor
                          send: 10-10-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                          send: 10-10-0-0 s=1,c=0,t=1,pt=0,l=5,st=ok:1.4.1
                          send: 10-10-0-0 s=255,c=3,t=11,pt=0,l=8,st=ok:Humidity
                          send: 10-10-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                          send: 10-10-0-0 s=2,c=0,t=7,pt=0,l=5,st=ok:1.4.1
                          send: 10-10-0-0 s=3,c=0,t=6,pt=0,l=5,st=ok:1.4.1
                          send: 10-10-0-0 s=255,c=3,t=11,pt=0,l=12,st=ok:Light Sensor
                          send: 10-10-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                          send: 10-10-0-0 s=4,c=0,t=16,pt=0,l=5,st=ok:1.4.1
                          send: 10-10-0-0 s=255,c=3,t=0,pt=1,l=1,st=ok:100
                          ---------- 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:16.6
                          ---------- Temp: 16.60
                          send: 10-10-0-0 s=2,c=1,t=1,pt=7,l=5,st=ok:43.8
                          ---------- Humidity: 43.80
                          send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:39
                          ---------- Light: 39
                          ---------- 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:37
                          ---------- Light: 37
                          ---------- 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:34
                          ---------- Light: 34
                          ---------- 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:36
                          ---------- Light: 36
                          ---------- 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:37
                          ---------- Light: 37
                          ---------- 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:38
                          ---------- Light: 38

                          The DHT gives only output the first start and the pir interrupt works perfect.

                          When delay(dht.getMinimumSamplingPeriod()); is activated the DHT works and the pit interrupts the whole time :(
                          In this configuration the arduino never sleeps ..

                          sensor started, id 10
                          send: 10-10-0-0 s=255,c=0,t=17,pt=0,l=5,st=ok:1.4.1
                          send: 10-10-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
                          read: 0-0-10 s=255,c=3,t=6,pt=0,l=1:M
                          send: 10-10-0-0 s=255,c=3,t=11,pt=0,l=13,st=ok:Motion Sensor
                          send: 10-10-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                          send: 10-10-0-0 s=1,c=0,t=1,pt=0,l=5,st=ok:1.4.1
                          send: 10-10-0-0 s=255,c=3,t=11,pt=0,l=8,st=ok:Humidity
                          send: 10-10-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                          send: 10-10-0-0 s=2,c=0,t=7,pt=0,l=5,st=ok:1.4.1
                          send: 10-10-0-0 s=3,c=0,t=6,pt=0,l=5,st=ok:1.4.1
                          send: 10-10-0-0 s=255,c=3,t=11,pt=0,l=12,st=ok:Light Sensor
                          send: 10-10-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                          send: 10-10-0-0 s=4,c=0,t=16,pt=0,l=5,st=ok:1.4.1
                          send: 10-10-0-0 s=255,c=3,t=0,pt=1,l=1,st=ok:100
                          ---------- 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:16.5
                          ---------- Temp: 16.50
                          send: 10-10-0-0 s=2,c=1,t=1,pt=7,l=5,st=ok:43.9
                          ---------- Humidity: 43.90
                          send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:38
                          ---------- Light: 38
                          ---------- 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:16.6
                          ---------- Temp: 16.60
                          send: 10-10-0-0 s=2,c=1,t=1,pt=7,l=5,st=ok:43.8
                          ---------- Humidity: 43.80
                          send: 10-10-0-0 s=4,c=1,t=23,pt=2,l=2,st=ok:37
                          ---------- Light: 37
                          ---------- 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=3,c=1,t=0,pt=7,l=5,st=ok:16.5
                          ---------- Temp: 16.50
                          ---------- 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: 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:38

                          EDIT: i have changed the sleeptime for testing to a shorter time

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

                          1 Reply Last reply
                          0
                          • rvendrameR rvendrame

                            So PIR must connect on PIN 1 and not PIN 3.

                            Edit: Better, how did you connect DHT and PIR?

                            SweebeeS Offline
                            SweebeeS Offline
                            Sweebee
                            wrote on last edited by
                            #27

                            @rvendrame When the PIR is connected to PIN3 the interrupt is INT1. Pin 1 does not have an interrupt.

                            Only pin 2 and pin 3 have an interrupt.

                            Pin 2 = int0
                            pin 3 = int1

                            n3roN 1 Reply Last reply
                            0
                            • rvendrameR Offline
                              rvendrameR Offline
                              rvendrame
                              Hero Member
                              wrote on last edited by
                              #28

                              if you put the delay() inside that 'if ( mills() > mytime ) ' , what happens?

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

                              n3roN 1 Reply Last reply
                              0
                              • SweebeeS Sweebee

                                @rvendrame When the PIR is connected to PIN3 the interrupt is INT1. Pin 1 does not have an interrupt.

                                Only pin 2 and pin 3 have an interrupt.

                                Pin 2 = int0
                                pin 3 = int1

                                n3roN Offline
                                n3roN Offline
                                n3ro
                                wrote on last edited by
                                #29

                                @Sweebee is it possible to fix the problem when DHT is connected to pin 4?

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

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

                                  @n3ro said:

                                  s it possible to fix the problem when DHT is connected to pin 4?

                                  It worth a try ...

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

                                  1 Reply Last reply
                                  0
                                  • rvendrameR rvendrame

                                    if you put the delay() inside that 'if ( mills() > mytime ) ' , what happens?

                                    n3roN Offline
                                    n3roN Offline
                                    n3ro
                                    wrote on last edited by
                                    #31

                                    @rvendrame
                                    i have posted it before this post:

                                    "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 <<<<<<"

                                    in the posted sketch the delay is inside millis

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

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

                                      I didn't find the if ( millis() > my time) in the last sketch you posted ...

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

                                      1 Reply Last reply
                                      0
                                      • n3roN Offline
                                        n3roN Offline
                                        n3ro
                                        wrote on last edited by
                                        #33

                                        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 = 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);
                                            }
                                            mytime = millis() + dht.getMinimumSamplingPeriod();
                                        
                                          }
                                          // Sleep until interrupt comes in on motion sensor. Send update every two minute.
                                          gw.sleep(INTERRUPT, CHANGE, SLEEP_TIME);
                                        }
                                        

                                        i get this output:
                                        send: 10-10-0-0 s=255,c=3,t=11,pt=0,l=13,st=ok:Motion Sensor
                                        send: 10-10-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                                        send: 10-10-0-0 s=1,c=0,t=1,pt=0,l=5,st=ok:1.4.1
                                        send: 10-10-0-0 s=255,c=3,t=11,pt=0,l=8,st=ok:Humidity
                                        send: 10-10-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                                        send: 10-10-0-0 s=2,c=0,t=7,pt=0,l=5,st=ok:1.4.1
                                        send: 10-10-0-0 s=3,c=0,t=6,pt=0,l=5,st=ok:1.4.1
                                        send: 10-10-0-0 s=255,c=3,t=11,pt=0,l=12,st=ok:Light Sensor
                                        send: 10-10-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                                        send: 10-10-0-0 s=4,c=0,t=16,pt=0,l=5,st=ok:1.4.1
                                        ---------- 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:17.2
                                        ---------- Temp: 17.20
                                        send: 10-10-0-0 s=2,c=1,t=1,pt=7,l=5,st=ok:43.8
                                        ---------- Humidity: 43.80
                                        ---------- PIR: 1
                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:1
                                        ---------- PIR: 0
                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
                                        ---------- PIR: 0
                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
                                        ---------- PIR: 0
                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
                                        ---------- PIR: 1
                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:1
                                        ---------- PIR: 0
                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
                                        ---------- PIR: 1
                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:1
                                        ---------- PIR: 0
                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
                                        ---------- PIR: 1
                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:1
                                        ---------- PIR: 0
                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
                                        ---------- PIR: 0
                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0
                                        ---------- PIR: 0
                                        send: 10-10-0-0 s=1,c=1,t=16,pt=0,l=1,st=ok:0

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

                                        1 Reply Last reply
                                        0
                                        • SweebeeS Offline
                                          SweebeeS Offline
                                          Sweebee
                                          wrote on last edited by
                                          #34

                                          This can't work well. Your interrupt is set on pin1 - 2 = int-1?

                                          What happens when you remove the pir and use a switch instead?

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


                                          26

                                          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