Combining Multiple sensors - Reed switch not working



  • Hello:

    I have a multi sensor with a DHT, PIR, NRF, and Reed switch. The PIR is connected to PIN 3, and the Reed switch is connected to PIN 2. The reed switch is setup exactly as the mysensors example here, ( https://www.mysensors.org/build/binary ) and works perfectly fine when only using that sketch. When trying to combine a few sketches, I'm getting everything else to work except for the Reed switch, and can't figure out where I've gone wrong. Is anyone able to possibly shed some light? My code is below, and really appreciate the help thank you.

      #define MY_GW_ID 50
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>  
    #include <Bounce2.h>
    
    unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    //Constants
    #define SKETCH_NAME           "3 in 1 Sensor"
    #define SKETCH_VERSION        "1.0"
    #define CHILD_ID_TEMP 1                  // Child id for Temperature
    #define CHILD_ID_HUM 0                   // Child id for Humidity
    #define CHILD_ID_MOT 2                   // Child id for Motion
    #define HUMIDITY_SENSOR_DIGITAL_PIN 7    // Where is my DHT22 data pin connected to
    #define DIGITAL_INPUT_SENSOR 3           // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID_SW 3                    // Child id for Reed Switch
    #define BUTTON_PIN  2                    // Arduino Digital I/O pin for button/reed switch
    
    int node_id=50;                          // What is my node id
    
    
    MySensor gw;
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    DHT dht;
    //Store last values
    float lastTemp = 0 ;                  
    float lastHum = 0 ;
    ;             
    
    boolean lastTripped = false ;
    boolean metric = true; 
    
    boolean gwsend = true;              // to determine if data has to be sent
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
    MyMessage msgsw(CHILD_ID_SW,V_TRIPPED);
    
    void setup()  
    { 
      gw.begin(NULL,node_id,false);
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
      // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo("3 in 1 Sensor", "1.0",true);
    
     //  - NEW Setup the button
     
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
    
    
     //  - NEW Setup the button END
    
    
    pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
     
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_HUM, S_HUM,"Garage Humidity");
      gw.present(CHILD_ID_TEMP, S_TEMP,"Garage Temperature");
      gw.present(CHILD_ID_SW, S_DOOR,"Garage Door");
       
      metric = gw.getConfig().isMetric;
    }
    
    void loop()      
    
      // Read Reed switch value
    {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
     
      if (value != oldValue) {
         // Send in the new value
         gw.send(msgsw.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
    
      // Read Temp - Humidity Value
    
    {  
      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);
      }
    
    {     
      // Read digital motion PIR value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
            
      Serial.println(tripped);
      gw.send(msgMot.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);
      }
    }
    }
    

  • Hardware Contributor

    Afaik the debouncer doesn't work with sleep(). Try without or just do something like a

    gw.wait(5);
    

    before reading the value. At least I had the same problem and fixed it like that.



  • Thanks very much @LastSamurai removing the sleep function seemed to work, but the PIR reports like crazy!

    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    0
    send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
    
    

    I don't really need sleep, but will be adding a relay to this sketch. Any other suggestions that might not trigger the PIR all the time? Thanks very much again!



  • @rhuehn now that you have a "wait", your loop is not blocked. Then after the "wait" elapses the loop start again and you send a new PIR state (which is certainly the same than the previous loop).
    So you have to memorize the last state and check if the pir state changed in the loop. You send the msg only f state is different.
    I hope my explanation is clear 🙂



  • thanks @fets have I specified the wait time at the wrong location? In the sketch below, everything works, BUT the PIR triggers 0 over and over and over again.....

     #define MY_GW_ID 50
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>  
    #include <Bounce2.h>
    
    
    //Constants
    #define SKETCH_NAME           "3 in 1 Sensor"
    #define SKETCH_VERSION        "1.0"
    #define CHILD_ID_TEMP 11                  // Child id for Temperature
    #define CHILD_ID_HUM 10                   // Child id for Humidity
    #define CHILD_ID_MOT 2                   // Child id for Motion
    #define HUMIDITY_SENSOR_DIGITAL_PIN 7    // Where is my DHT22 data pin connected to
    #define DIGITAL_INPUT_SENSOR 3           // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID_SW 3                    // Child id for Reed Switch
    #define BUTTON_PIN  2                    // Arduino Digital I/O pin for button/reed switch
    
    #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    
    int node_id=50;                          // What is my node id
    
    
    MySensor gw;
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    DHT dht;
    //Store last values
    float lastTemp = 0 ;                  
    float lastHum = 0 ;
    ;             
    
    boolean lastTripped = false ;
    boolean metric = true; 
    
    boolean gwsend = true;              // to determine if data has to be sent
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
    MyMessage msgsw(CHILD_ID_SW,V_TRIPPED);
    
    void setup()  
    { 
      gw.begin(NULL,node_id,false);
      gw.wait(5);
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
      // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo("3 in 1 Sensor", "1.0",true);
    
    
     //  - NEW Setup the button
     
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
    
    
     //  - NEW Setup the button END
    
    
    pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
     
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_HUM, S_HUM,"Garage Humidity");
      gw.present(CHILD_ID_TEMP, S_TEMP,"Garage Temperature");
      gw.present(CHILD_ID_SW, S_DOOR,"Garage Door");
    
      metric = gw.getConfig().isMetric;
    }
    
    void loop()      
    
      // Read Reed switch value
    {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
     
      if (value != oldValue) {
         // Send in the new value
         gw.send(msgsw.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
    
      // Read Temp - Humidity Value
    
    {  
      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);
      }
    
    {     
      // Read digital motion PIR value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
       
            
      Serial.println(tripped);
      gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
      }
    }
    }
    

    Thanks


Log in to reply
 

Suggested Topics

  • 1
  • 5
  • 6
  • 3
  • 1
  • 2

1
Online

11.4k
Users

11.1k
Topics

112.7k
Posts