Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArduinoBasics/22da154ba7928711c47e210774589e67 to your computer and use it in GitHub Desktop.
Save ArduinoBasics/22da154ba7928711c47e210774589e67 to your computer and use it in GitHub Desktop.
Using a PIR sensor to detect movement, and a Photoresistor as the trigger in a laser trip-wire setup.
/*
Title: PIR and Photoresistor with Ethernet Shield connected to Cayenne
Author: ScottC
Date: 10th August 2016
Arduino IDE version: 1.6.9
Website: http://arduinobasics.blogspot.com.au/2016/08/arduino-based-security-project-using.html
Description: Using a PIR sensor to detect movement, and a Photoresistor as the trigger in a laser trip-wire setup.
When motion is detected, or when the laser beam is broken, the Arduino will notify Cayenne of these events.
There are two Arduino MCUs involved in this project. This arduino is used to detect the presence of an intruder.
Once an event is generated, the other Arduino is notified. The other Arduino then requests the person to identify themselves.
If the person identifies themselves successfully within 10 seconds, the alarm is deactivated,
otherwise an alarm is triggered, and an alert is sent via email/SMS (using the Cayenne service) to notify me of the intrusion.
Libraries required:
CayenneEthernet.h : is within the Cayenne-Arduino library download file: https://github.com/myDevicesIoT/Cayenne-Arduino-Library
Credits: Some of the code used in this sketch was adapted from code examples at this site:
http://www.cayenne-mydevices.com/docs/#introduction
------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
#include <CayenneEthernet.h> //This library is required for an Arduino UNO using an Ethernet Shield with a WIZNET 5100 chip
char token[] = "INSERT YOUR TOKEN HERE"; // Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
// Global variables
const int PIR_Pin = 6; //PIR sensor connected to Digital Pin 6
int PIRcurrentState = 0; //The current state of the PIR sensor (0/1)
int PIRprevState = 0; //The previous state of the PIR sensor (0/1)
int vPin0 = 0; //vPin0 is paired with Virtual Pin 0 in Cayenne, and is used as a "Master Control Button" to enable/disable Intruder monitoring
int vPin4 = 0; //vPin4 is paired with Virtual Pin 4 in Cayenne, and is used to manage the "Laser Trigger"
int photoR = 0; //photoR is used to get/store the photoresistor readings from Analog Pin A2 (Values range from 0 to 1023)
unsigned int laserThreshold = 500; //laserThreshold (default = 500), is used to control the level of light at which to laser is triggered. i.e. When reading drops below this level.
boolean masterJustTurnedOn = false; //masterJustTurnedOn: this variable was required to help tidy up some loose ends - when the "Master Control Button" was switched on.
boolean masterState = false; //masterState: is used to monitor the state (on/off) of the "Master Control Button"
/*
* =======================SETUP()==========================================================================================================================================
*/
void setup(){
//Serial.begin(9600); //Turn on Serial communication with the computer for debugging purposes.
Cayenne.begin(token); //Initialise the connection between this Arduino and Cayenne. The token must match the one in the Cayenne Dashboard.
pinMode(8, OUTPUT); //The photoresistor will be receiving 5V from digital pin 8
digitalWrite(8, HIGH); //Turn on the power to the photoresistor (connected to digital pin 8). Please note: The readings are obtained from Analog pin A2.
}
/*
* =======================LOOP()==========================================================================================================================================
*/
void loop(){
Cayenne.run(); //Synchronisation step with Cayenne service.
/*The following code will only run when the Master Control Switch is just turned on, and will be ignored thereafter.
* The masterState is changed to true, to show the Master Control Switch is NOW ON. The PIRcurrentState is set to zero, to prevent false positive intruder detections.
* Cayenne is notified of this change, to help synchronise the dashboard. vPin4 (Laser Trigger) is also reset to zero for the same reason.
* A delay is introduced to allow both sides sufficient time to update their respective sensor status.
*/
if(vPin0 && !masterJustTurnedOn){
masterJustTurnedOn = true;
masterState=true;
PIRcurrentState = 0;
Cayenne.virtualWrite(V1, PIRcurrentState);
delay(50);
vPin4 = 0;
Cayenne.virtualWrite(V4, vPin4);
delay(500);
}
/*vPin0 is attached to Virtual Pin 0 in Cayenne, and reflects the state of the Master Control Button in the Cayenne Dashboard.
* masterState reflects the state of the Master Control Button in the Arduino. Both have to match, and be ON, in order to activate the Security sensors.
* The current state of the PIR sensor is checked. Only when the PIR sensor state changes(from off to on or vice versa), does it update the Cayenne dashboard
*/
if(vPin0 && masterState){
PIRcurrentState = digitalRead(PIR_Pin);
if(compareState()){
Cayenne.virtualWrite(V1, PIRcurrentState); //Turn PIR indicator on the App ON/OFF depending on state of PIR sensor.
delay(20);
}
/* Only check the photoresistor when the Laser Trigger has not already been triggered.
* If the photoresistor reading drops below the threshold (eg. When someone blocks the laser light from hitting the sensor), then activate the
* Laser Trigger (change status from 0 to 1) - notify the Cayenne dashboard of this change.
* The light threshold can be modified/controlled from the Cayenne dashboard.
*/
if(!vPin4){
photoR = analogRead(A2);
if(photoR<laserThreshold){
vPin4 = 1;
Cayenne.virtualWrite(V4, vPin4);
}
}
}
}
/* =======================compareState()==========================================================================================================================================
* compareState() function: used to compare the current state of the PIR sensor from the previous state of the sensor.
* It allows the Cayenne dashboard to keep up to date with the status of the sensor.
*/
boolean compareState(){
if(PIRcurrentState != PIRprevState) {
PIRprevState=PIRcurrentState;
return true;
} else {
return false;
}
}
/* =======================CAYENNE_OUT(V2)==========================================================================================================================================
* CAYENNE_OUT(V2):
* Will update the Cayenne Dashboard (Virtual pin 2) with analog readings from the Arduino Analog Pin A2
*/
CAYENNE_OUT(V2){
Cayenne.virtualWrite(V2, analogRead(A2));
}
/* =======================CAYENNE_IN(V0)==========================================================================================================================================
* Will update Arduino's variable vPin0 to match Virtual Pin 0 in the Cayenne Dashboard.
* This has also been setup to reset a couple of variables, to ensure proper functionality.
*/
CAYENNE_IN(V0){
vPin0 = getValue.asInt();
if(!vPin0){
masterJustTurnedOn = false;
masterState=false;
}
}
/* =======================CAYENNE_IN(V4)==========================================================================================================================================
* Will update Arduino's variable vPin4 to match Virtual Pin 4 (Laser Trigger) in the Cayenne Dashboard.
* This allows you to set the status of vPin4 from the Cayenne Dashboard.
*/
CAYENNE_IN(V4){
vPin4 = getValue.asInt();
}
/* =======================CAYENNE_IN(V5)==========================================================================================================================================
* Will update Arduino's variable laserThreshold to match Virtual Pin 5 (Laser Threshold) in the Cayenne Dashboard.
* This allows you to set the value of the "Laser Threshold" from the Cayenne Dashboard.
*/
CAYENNE_IN(V5){
laserThreshold = getValue.asInt()/10; //Have to divide by 10 due to bug in the Cayenne dashboard
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment