From 19c530a054c71b340f78820b24c94fc96fca6b75 Mon Sep 17 00:00:00 2001 From: Simon Milvert Date: Mon, 19 Nov 2018 20:43:32 +0100 Subject: [PATCH] First work of getting PIR to work --- motion_sensor/src/motion.ino | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 motion_sensor/src/motion.ino diff --git a/motion_sensor/src/motion.ino b/motion_sensor/src/motion.ino new file mode 100644 index 0000000..8746200 --- /dev/null +++ b/motion_sensor/src/motion.ino @@ -0,0 +1,50 @@ + +#define MY_NODE_ID 143 +// Enable debug prints +#define MY_DEBUG + +// Enable and select radio type attached +//#define MY_RADIO_RF24 +#define MY_RADIO_NRF5_ESB +//#define MY_RADIO_RFM69 +//#define MY_RADIO_RFM95 + +#include + +uint32_t SLEEP_TIME = 5000; // Sleep time between reports (in milliseconds) +#define DIGITAL_INPUT_SENSOR 6 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!) +#define LED 10 +#define CHILD_ID 1 // Id of the sensor child + +// Initialize motion message +MyMessage msg(CHILD_ID, V_TRIPPED); + +void setup() +{ + pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input + hwPinMode(LED,OUTPUT_D0H1); + pinMode(LED,OUTPUT); + digitalWrite(ledPin, HIGH); +} + +void presentation() +{ + // Send the sketch version information to the gateway and Controller + sendSketchInfo("Motion Sensor", "1.0"); + + // Register all sensors to gw (they will be created as child devices) + present(CHILD_ID, S_MOTION); +} + +void loop() +{ + // Read digital motion value + bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; + + Serial.println(tripped); + send(msg.set(tripped?"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); +} +