From d5ca965e7855411eeea1685f91402edf1c1e34f5 Mon Sep 17 00:00:00 2001 From: Felix Rusu Date: Sun, 10 Nov 2013 00:26:34 -0500 Subject: [PATCH] Add motion sensor example --- Examples/MotionMote/MotionMote.ino | 70 ++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Examples/MotionMote/MotionMote.ino diff --git a/Examples/MotionMote/MotionMote.ino b/Examples/MotionMote/MotionMote.ino new file mode 100644 index 0000000..5f09126 --- /dev/null +++ b/Examples/MotionMote/MotionMote.ino @@ -0,0 +1,70 @@ +// Sample RFM69 sender/node sketch for motion sensor +// PIR motion sensor connected to D3 (INT1) +// When RISE happens on D3, the sketch transmits a "MOTION" msg to receiver Moteino and goes back to sleep +// In sleep mode, Moteino + PIR motion sensor use about ~78uA +// Library and code by Felix Rusu - felix@lowpowerlab.com +// Get the RFM69 and SPIFlash library at: https://github.com/LowPowerLab/ + +#include +#include +#include //get library from: https://github.com/rocketscream/Low-Power + //writeup here: http://www.rocketscream.com/blog/2011/07/04/lightweight-low-power-arduino-library/ + +#define NODEID 18 //unique for each node on same network +#define NETWORKID 100 //the same on all nodes that talk to each other +#define GATEWAYID 1 +//Match frequency to the hardware version of the radio on your Moteino (uncomment one): +//#define FREQUENCY RF69_433MHZ +//#define FREQUENCY RF69_868MHZ +#define FREQUENCY RF69_915MHZ +#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes! +//#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W! +#define ACK_TIME 30 // max # of ms to wait for an ack +#define LED 9 // Moteinos have LEDs on D9 +#define SERIAL_BAUD 115200 +#define MOTIONPIN 1 //hardware interrupt 1 (D3) + +int TRANSMITPERIOD = 300; //transmit a packet to gateway so often (in ms) +RFM69 radio; +volatile boolean motionDetected=false; + +void setup() { + Serial.begin(SERIAL_BAUD); + radio.initialize(FREQUENCY,NODEID,NETWORKID); +#ifdef IS_RFM69HW + radio.setHighPower(); //uncomment only for RFM69HW! +#endif + radio.encrypt(ENCRYPTKEY); + attachInterrupt(MOTIONPIN, motionIRQ, RISING); + char buff[50]; + sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915); + Serial.println(buff); +} + +void motionIRQ() +{ + motionDetected=true; +} + +void loop() { + if (motionDetected) + { + if (radio.sendWithRetry(GATEWAYID, "MOTION", 6)) + { + Serial.println(" ok!"); + Blink(LED,3); + } + else Serial.println(" nothing..."); + motionDetected=false; + } + radio.sleep(); + LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); +} + +void Blink(byte PIN, int DELAY_MS) +{ + pinMode(PIN, OUTPUT); + digitalWrite(PIN,HIGH); + delay(DELAY_MS); + digitalWrite(PIN,LOW); +}