Help with PIR and Water Sensor or Nano - Water Sensor won't trigger



  • I'm hoping someone can possibly help me, I'm sure I've overlooked something but after a few days I can't seem to get this working right. I have a PIR, DHT, and a Water sensor attached to a Nano. I need the Water sensor to trigger as soon as it detects water. If I use a sketch on it's own, it works great, BUT with the combined sketch including the PIR, the sensor only sends an alert for water if the PIR becomes active, and I can't figure out why. It works fine without the PIR and sends an alert, but with the PIR and water sensor, it will only trigger water if the PIR detects motion.... Please help! 🙂

    My Sketch is below:

    
    #include <MySensor.h>
    #include <SPI.h>
    #include <DHT.h> 
    
    #define MY_NODE_ID 16         // My Node ID variable
    #define MY_DEBUG              // Turn on Debug
    #define MY_RADIO_NRF24        // Enable and select radio type attached
    
    #define SKETCH_NAME           "4 in 1 Sensor"
    #define SKETCH_VERSION        "1.0"
    #define DIGITAL_INPUT_SENSOR 3 // Arduino Digital I/O pin for PIR Motion Sensor
    #define CHILD_ID_HUM 10       // Child id for Humidity
    #define CHILD_ID_TEMP 11      // Child id for Temperature
    #define CHILD_ID_MOT 2      // Child id for Motion
    
    #define DIGITAL_INPUT_WATER_SENSOR 2 // Digital input did you attach your soil sensor.
    #define CHILD_ID_WAT 4      // Child id for Motion
    
    
    #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define HUMIDITY_SENSOR_DIGITAL_PIN 7 // Where is my DHT22 data pin connected to
    unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    
    
    MySensor gw;
    DHT dht;
    float lastTemp;
    float lastHum;
    boolean metric = true;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
    MyMessage msgWat(CHILD_ID_WAT, V_TRIPPED);
    int lastWaterValue = -1; 
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin();
    // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Laundry Room Sensor", "1.0");
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
      pinMode(DIGITAL_INPUT_WATER_SENSOR, INPUT);
        
        // 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);
        gw.present(CHILD_ID_MOT, S_MOTION);
        gw.present(CHILD_ID_WAT, S_MOTION);
    
        metric = gw.getConfig().isMetric;
        
    }
      
    
    
    void loop() 
    {
    // Read digital Water value
    int WaterValue = digitalRead(DIGITAL_INPUT_WATER_SENSOR); // 1 = Not triggered, 0 = In water
    if (WaterValue != lastWaterValue) {
    Serial.println(WaterValue);
    if (WaterValue==0)
    {
    Serial.println("Water!!!");
    gw.send(msgWat.set(0));
    }
    else
    {
    Serial.println("No Water");
    gw.send(msgWat.set(1)); // Send tripped when water detected
    lastWaterValue = WaterValue;
    }
    }
    
    
    
    
    
    {     
      // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
            
      Serial.println(tripped);
      gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
     
    {
    
    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("T: ");
        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("H: ");
          Serial.println(humidity);
      }
      // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
      gw.sleep(INTERRUPT,CHANGE);
    }
    }
    }
    

    Debug:

    sensor started, id=16, parent=0, distance=1
    send: 16-16-0-0 s=255,c=3,t=11,pt=0,l=19,sg=0,st=ok:Laundry Room Sensor
    send: 16-16-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.0
    send: 16-16-0-0 s=10,c=0,t=7,pt=0,l=0,sg=0,st=ok:
    send: 16-16-0-0 s=11,c=0,t=6,pt=0,l=0,sg=0,st=ok:
    send: 16-16-0-0 s=2,c=0,t=1,pt=0,l=0,sg=0,st=ok:
    send: 16-16-0-0 s=4,c=0,t=1,pt=0,l=0,sg=0,st=ok:
    1
    No Water
    send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:1
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    send: 16-16-0-0 s=11,c=1,t=0,pt=7,l=5,sg=0,st=ok:25.0
    T: 25.00
    send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:39.0
    H: 39.00
    1
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    Water!!!
    send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
    1
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
    send: 16-16-0-0 s=11,c=1,t=0,pt=7,l=5,sg=0,st=ok:26.0
    T: 26.00
    0
    Water!!!
    send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    Water!!!
    send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
    1
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
    send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:40.0
    H: 40.00
    0
    Water!!!
    send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:39.0
    H: 39.00
    1
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    1
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
    

    Thank you very much


  • Hero Member

    Your sleep statement is only activated when the pir has a mode change. You need to add your SLEEP_TIME timeout as well

    Haven't used sleep yet but I think you need something like.
    gw.sleep(INTERRUPT,CHANGE,SLEEP_TIME );



  • Thats @Boots33 but unfortunately I tried that as well, without success.

    I added the following to the end:

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

    Debug below, showing that the sensor works but only when PIR is triggered:

    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    send: 16-16-0-0 s=11,c=1,t=0,pt=7,l=5,sg=0,st=ok:23.0
    T: 23.00
    1
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    send: 16-16-0-0 s=11,c=1,t=0,pt=7,l=5,sg=0,st=ok:24.0
    T: 24.00
    send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:40.0
    H: 40.00
    0
    Water!!!
    send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
    1
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
    send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:39.0
    H: 39.00
    0
    Water!!!
    send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:40.0
    H: 40.00
    
    

  • Hero Member

    The water sensor will only be checked every 2 minutes, it will not fire off while the arduino is sleeping.
    If you comment out the sleep statement does the sketch work then.



  • Hi @Boots33 Yes it will work if I comment out the sleep for the PIR, but the PIR will then fire every second as it is not detecting motion. sketch below, ( commented out ) but is there not a way to have the water sensor trigger in theory the same as the PIR triggers? ie: sleep for a bit UNLESS motion / water is detected?

    
    #include <MySensor.h>
    #include <SPI.h>
    #include <DHT.h> 
    
    #define MY_NODE_ID 16         // My Node ID variable
    #define MY_DEBUG              // Turn on Debug
    #define MY_RADIO_NRF24        // Enable and select radio type attached
    
    #define SKETCH_NAME           "4 in 1 Sensor"
    #define SKETCH_VERSION        "1.0"
    #define DIGITAL_INPUT_SENSOR 3 // Arduino Digital I/O pin for PIR Motion Sensor
    #define CHILD_ID_HUM 10       // Child id for Humidity
    #define CHILD_ID_TEMP 11      // Child id for Temperature
    #define CHILD_ID_MOT 2      // Child id for Motion
    
    #define DIGITAL_INPUT_WATER_SENSOR 2 // Digital input did you attach your soil sensor.
    #define CHILD_ID_WAT 4      // Child id for Motion
    
    
    #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define HUMIDITY_SENSOR_DIGITAL_PIN 7 // Where is my DHT22 data pin connected to
    // unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    
    
    MySensor gw;
    DHT dht;
    float lastTemp;
    float lastHum;
    boolean metric = true;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
    MyMessage msgWat(CHILD_ID_WAT, V_TRIPPED);
    int lastWaterValue = -1; 
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin();
    // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Laundry Room Sensor", "1.0");
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
      pinMode(DIGITAL_INPUT_WATER_SENSOR, INPUT);
        
        // 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);
        gw.present(CHILD_ID_MOT, S_MOTION);
        gw.present(CHILD_ID_WAT, S_MOTION);
    
        metric = gw.getConfig().isMetric;
        
    }
      
    
    
    void loop() 
    {
    // Read digital Water value
    int WaterValue = digitalRead(DIGITAL_INPUT_WATER_SENSOR); // 1 = Not triggered, 0 = In water
    if (WaterValue != lastWaterValue) {
    Serial.println(WaterValue);
    if (WaterValue==0)
    {
    Serial.println("Water!!!");
    gw.send(msgWat.set(0));
    }
    else
    {
    Serial.println("No Water");
    gw.send(msgWat.set(1)); // Send tripped when water detected
    lastWaterValue = WaterValue;
    }
    }
    
    
    
    
    
    {     
      // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
            
      Serial.println(tripped);
      gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
     
    {
    
    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("T: ");
        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("H: ");
          Serial.println(humidity);
      }
      // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
     // gw.sleep(INTERRUPT,CHANGE,SLEEP_TIME);
    }
    }
    }
    

    Below is the latest debug:

    end: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:39.0
    H: 39.00
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:40.0
    H: 40.00
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:39.0
    H: 39.00
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:40.0
    H: 40.00
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    Water!!!
    send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    Water!!!
    send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    Water!!!
    send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:39.0
    H: 39.00
    0
    Water!!!
    send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:40.0
    H: 40.00
    0
    Water!!!
    send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
    0
    send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    
    

  • Hero Member

    Yes the sleep function allows you to check for a second interrupt I think. You would need to setup your water sensor to be connected to the other interrupt.



  • Got it working with this sleep function properly:

      // Sleep until interrupt comes in on motion or water sensor. Send update every two minute. 
      gw.sleep(DIGITAL_INPUT_SENSOR - 2, CHANGE, DIGITAL_INPUT_WATER_SENSOR - 2, CHANGE, SLEEP_TIME);
    

Log in to reply
 

Suggested Topics

6
Online

11.4k
Users

11.1k
Topics

112.7k
Posts