Double Pir sensors



  • Hi there i could use some help, i tried to expand the mysensors motion sketch but i would like to use 2 pir sensors.

    I changed the sketch and get a strange message;
    #define digitalPinToInterrupt(p) ((p) == 2 ? 0 : ((p) == 3 ? 1 : NOT_AN_INTERRUPT))

                                     ^
    

    exit status 1
    redefinition of 'MyMessage msg'

    i thought nr 2 and 3 are with interrupt.

    Than i think i'll have to change:
    // Sleep until interrupt comes in on motion sensor. Send update every two minute.
    sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);

    Well maybe someone have some time to help me.

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     *
     * DESCRIPTION
     * Motion Sensor example using HC-SR501
     * http://www.mysensors.org/build/motion
     *
     */
    
    // Enable debug prints
    // #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <MySensors.h>
    #define MY_NODE_ID 9
    unsigned long SLEEP_TIME = 5000 ; // 120000 Sleep time between reports (in milliseconds)
    #define DIGITAL_INPUT_SENSOR1 2   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define DIGITAL_INPUT_SENSOR2 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    
    #define CHILD_ID_PIR1 21   // Id of the sensor child
    #define CHILD_ID_PIR2 22   // Id of the sensor child
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    // Initialize motion message
    MyMessage msg(CHILD_ID_PIR1, V_TRIPPED);
    MyMessage msg(CHILD_ID_PIR2, V_TRIPPED);
    
    void setup()
    {
    	pinMode(DIGITAL_INPUT_SENSOR1, INPUT);      // sets the motion sensor digital pin as input
      pinMode(DIGITAL_INPUT_SENSOR2, INPUT);      // sets the motion sensor digital pin as input
     }
    
    void presentation()
    {
    	// Send the sketch version information to the gateway and Controller
    	sendSketchInfo("PIR Sensor", "3.0");
    
    	// Register all sensors to gw (they will be created as child devices)
    	present(CHILD_ID_PIR1, S_MOTION);
      present(CHILD_ID_PIR2, S_MOTION);
    }
    
    void loop()
    {
    	// Read digital motion value
    	bool tripped = digitalRead(DIGITAL_INPUT_SENSOR1) == HIGH;
      bool tripped2 = digitalRead(DIGITAL_INPUT_SENSOR2) == HIGH;
     
    	Serial.println(tripped);
    	send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
      
      Serial.println(tripped2);
      send(msg.set(tripped2?"1":"0"));  // Send tripped value to gw
    	
    	// Sleep until interrupt comes in on motion sensor. Send update every two minute.
    	sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    }
    
    

  • Mod

    @Rene046 see https://www.mysensors.org/download/sensor_api_20#sleeping for how to use the sleep function with two interrupts.

    int8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms=0);
    


  • thx for helping with last problem
    only problem left is with why
    MyMessage msg(CHILD_ID_PIR2, V_TRIPPED);
    is not accepted.

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     *
     * DESCRIPTION
     * Motion Sensor example using HC-SR501
     * http://www.mysensors.org/build/motion
     *
     */
    
    // Enable debug prints
    // #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <MySensors.h>
    #define MY_NODE_ID 9
    unsigned long SLEEP_TIME = 5000 ; // 120000 Sleep time between reports (in milliseconds)
    #define DIGITAL_INPUT_SENSOR1 2   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define DIGITAL_INPUT_SENSOR2 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    
    #define CHILD_ID_PIR1 21   // Id of the sensor child
    #define CHILD_ID_PIR2 22   // Id of the sensor child
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    // Initialize motion message
    MyMessage msg(CHILD_ID_PIR1, V_TRIPPED);
    MyMessage msg(CHILD_ID_PIR2, V_TRIPPED);
    
    void setup()
    {
    	pinMode(DIGITAL_INPUT_SENSOR1, INPUT);      // sets the motion sensor digital pin as input
      pinMode(DIGITAL_INPUT_SENSOR2, INPUT);      // sets the motion sensor digital pin as input
     }
    
    void presentation()
    {
    	// Send the sketch version information to the gateway and Controller
    	sendSketchInfo("PIR Sensor", "3.0");
    
    	// Register all sensors to gw (they will be created as child devices)
    	present(CHILD_ID_PIR1, S_MOTION);
      present(CHILD_ID_PIR2, S_MOTION);
    }
    
    void loop()
    {
    	// Read digital motion value
    	bool tripped = digitalRead(DIGITAL_INPUT_SENSOR1) == HIGH;
      bool tripped2 = digitalRead(DIGITAL_INPUT_SENSOR2) == HIGH;
     
    	Serial.println(tripped);
    	send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
      
      Serial.println(tripped2);
      send(msg.set(tripped2?"1":"0"));  // Send tripped value to gw
    	
    	// Sleep until interrupt comes in on motion sensor. Send update every two minute.
    	sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR1), CHANGE, (DIGITAL_INPUT_SENSOR2), CHANGE, SLEEP_TIME);
    }
    
    

  • Mod

    @Rene046 there can't be two variables with the same name
    You can change all occurrences of the first msg to msg1 and all occurrences of the second msg to msg2


  • Mod

    You'll need to use digitalPinToInterrupt for sensor 2 as well (the code you posted only uses digitalPinToInterrupt for the first sensor)



  • thx.

    Aha i missed those

    sketch is accepted, waiting for second pir sensor to test in real time.

    thx again learned something new again...
    Starting to have fun with Mysensors ...

    For other users who like to try.

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     *
     * DESCRIPTION
     * Motion Sensor example using HC-SR501
     * http://www.mysensors.org/build/motion
     *
     */
    
    // Enable debug prints
    // #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <MySensors.h>
    #define MY_NODE_ID 9
    unsigned long SLEEP_TIME = 5000 ; // 120000 Sleep time between reports (in milliseconds)
    #define DIGITAL_INPUT_SENSOR1 2   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define DIGITAL_INPUT_SENSOR2 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    
    #define CHILD_ID_PIR1 21   // Id of the sensor child
    #define CHILD_ID_PIR2 22   // Id of the sensor child
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    // Initialize motion message
    MyMessage msg1(CHILD_ID_PIR1, V_TRIPPED);
    MyMessage msg2(CHILD_ID_PIR2, V_TRIPPED);
    
    void setup()
    {
    	pinMode(DIGITAL_INPUT_SENSOR1, INPUT);      // sets the motion sensor digital pin as input
      pinMode(DIGITAL_INPUT_SENSOR2, INPUT);      // sets the motion sensor digital pin as input
     }
    
    void presentation()
    {
    	// Send the sketch version information to the gateway and Controller
    	sendSketchInfo("PIR Sensor", "3.0");
    
    	// Register all sensors to gw (they will be created as child devices)
    	present(CHILD_ID_PIR1, S_MOTION);
      present(CHILD_ID_PIR2, S_MOTION);
    }
    
    void loop()
    {
    	// Read digital motion value
    	bool tripped = digitalRead(DIGITAL_INPUT_SENSOR1) == HIGH;
      bool tripped2 = digitalRead(DIGITAL_INPUT_SENSOR2) == HIGH;
     
    	Serial.println(tripped);
    	send(msg1.set(tripped?"1":"0"));  // Send tripped value to gw
      
      Serial.println(tripped2);
      send(msg2.set(tripped2?"1":"0"));  // Send tripped value to gw
    	
    	// Sleep until interrupt comes in on motion sensor. Send update every two minute.
    	sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR1), CHANGE, (DIGITAL_INPUT_SENSOR2), CHANGE, SLEEP_TIME);
    }
    
    

  • Mod

    Great work @Rene046! Thanks for sharing your finished sketch.


Log in to reply
 

Suggested Topics

  • 3
  • 2
  • 5
  • 1

15
Online

11.4k
Users

11.1k
Topics

112.7k
Posts