Multisensor PIR problem [solved]



  • Hey Folks,

    i have some problems with my battery powered arduino mini.

    When i use my sketch, the PIR doesn't signal present right. Only sometime i get a signal.

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

    But when i use this (simple) sketch with only PIR it works like charm.

    https://github.com/n3roGit/Arduino-home-automation/tree/master/2.4Ghz (MySensors)/Arduino/PirSleepBattery

    Any ideas whats wrong?

    Regards,
    n3ro



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



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

    Big THX!



  • @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


  • Hero Member

    @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



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

    thx for your info!



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


  • Hero Member

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





  • 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();
    ......



  • @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.



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


  • Hero Member

    @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.



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


  • Hero Member

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


  • @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.


  • Hero Member

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


  • @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 :(


  • Hero Member

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



  • @n3ro said:

    Serial.println(mytime);

    yes :)
    Serial.println(mytime);


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.