Hi again
on request from a reader, i will post the code i used in my homemade home burglar alarm system
first a few words about the design choices
i dont have a loud alarm or siren; the base station will beep if an alarm is triggered; the base station sits on my bedside and will wake me up if there is any trigger; the display will show which sensor triggered; sensors are installed in layers, there are door sensors on doors, and motion sensors in rooms. if a motion sensor triggers i will evaulate if it is possible that someone was in the range of that specific sensor without having triggered other sensors, that allows me to evaluate false alarms; in principle, sensors are located so that is not possible to trigger one single alarm, which will be most likely a false alarm. also, motion sensors will usually generate more than one alarm. so far i never saw a false alarm from a door sensor, but pir sensors can generate false alarms, expecially if they see direct sunlight. so sensors should be placed so they dont see direct sunlight, or reflection from a bright wall of direct sunlight.
i have a motion sensor in a garage that, in may, will often trigger false alarms at sunset, when sunlight filters under the door. why may? because after may the trees will grow leaves and shield the sunrise, and before the sun rises at an angle that will not hit the garage door :-)
there is a day mode and a night mode. in day mode the base station will show on a 20x4 display only which alarm has received
in night mode some sensors will also trigger a sound alarm, and alarm is displayed. i exclude from the night mode those sensors that i will trigger for example getting up to go to the toilet...
there is a cancel button which will clear the display from all alarms and turn the display light off
when the first alarm is received, the display light turns on, showing an alarm has been received.
there is an away-from-home mode, that will cause the base station to send a text message over the mobile network when an alarm is triggered.
there is a remote control, based on a similar design as the sensors, that is used to turn on and off the away-from-home mode. the away-from-home mode can also be used to check that the GSM module is working and is connected to the mobile network.
activating or deactivating the away-from-home mode will also trigger a text message signalling that away-from-home mode is switching on or off.
a red led on the remote will show if an ack to the activation/deactivation has been received.
the base station is powered through a mightyboost, which allow it to be completely independent from power or internet connection.
all sensors are battery powered. motion sensors also last several months, depending on how much movement there is in the area. some of them are powered from 4xAA batteries, which ensure some 2 years of functionality.
i have 2 type of sensors, door sensors and motion sensors. lets start with the door sensors.
door sensors are based on common magnetic reed switches that are normally opened. something of the sort
https://www.ebay.com/itm/Recessed-Magnetic-Window-Door-Contacts-Security-Alarm-Switch-Reed-Adhesive-TB/392104317903?hash=item5b4b3d13cf:g:kwAAAOSwiOFZO4NJwhen the door is closed, the magnetic reed switch is closed, and the interrupt enabled digital input pin used in the moteino (D3) is in low state. when the door is opened, D3 will go high; there is a pullup resistor of 330k ohm which has demonstrated to be enough for the task, and allows a 9v battery to last several months. each door sensor has its own number wich is sent OTA , which allows the base station to tell which sensor is which
all moteino used are equipped with a RFM69H transceiver, which has demonstrated to be more than adequate for the application. i have a big house with thick walls, sensors work everywhere in the house.
this is one of my first projects so the code is definitely crappy. but it works and has worked for several years now.
one thing i have noticed is that the serial data gets corrupted after the processor was in deep sleep, i never got round to figure out why, but is not critical. just need it during prototypeing
// 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 <RFM69.h> //get it here: https://www.github.com/lowpowerlab/rfm69
#include <LowPower.h> //get library from: https://github.com/rocketscream/Low-Power
//writeup here: http://www.rocketscream.com/blog/2011/07/04/lightweight-low-power-arduino-library/
#include <SPI.h> //get it here: https://github.com/lowpowerlab/spiflash
#define NODEID 21 //unique for each node on same network
// 01 - receiver 1x motion sensors 2x door switches
#define NETWORKID 99 //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 "0123456789012345" //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)
RFM69 radio;
volatile boolean motionDetected=false;
void setup() {
Serial.begin(SERIAL_BAUD);
radio.initialize(FREQUENCY,NODEID,NETWORKID);
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)
{
Serial.println("Motion!!!");
if (radio.sendWithRetry(GATEWAYID, "DO01", 4)) // door sensors send a message DO plus a number from 01 to 99
Serial.println(" ok!");
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);
}