Shawn G OsborneTracy L Easter
Published © GPL3+

Photon Home Alert System

A home defense system that utilizes motion detection, web based camera and buzzer.

BeginnerFull instructions provided6 hours2,201
Photon Home Alert System

Things used in this project

Story

Read more

Schematics

Mailbox Particle w/ Distance Sensor & Battery

IPhone with Manything App

Buzzer Circuit

Distance Sensor Circuit

Traffic Data Acquisition

Guest Data Acquisition

Code

Position-Indication

Arduino
load on the Particle IDE
/* HC-SR04 Sensor, iPhone camera, 2x particle photon, blackweb battery brick, & piezo buzzer.

   The circuit:
	* VCC connection of the sensor attached to +5V (VIN)
	* GND connection of the sensor attached to ground
	* TRIG connection of the sensor attached to digital pin D6
	* ECHO connection of the sensor attached to digital pin D5
   
   rDPulsIn function by Ric from Particle Community
   This example code is in the public domain.
 */


const int trigPin = D4;
const int echoPin = D3;
int k=0;
int traffic_counter=0;
int guest_counter=0;
unsigned long distance_in,duration,nowtime,looptime;
unsigned long duration2,duration3,duration4,duration5,duration6,duration7,duration8;
unsigned long distance_in2,distance_in3,distance_in4,distance_in5,distance_in6,distance_in7,distance_in8;



void setup() {
    
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(D0,OUTPUT);
  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);

  Particle.variable("Distance-in", distance_in);
  Particle.variable("pulseIn", duration);
  Serial.begin(9600);
  }



void loop()
{
  nowtime=micros();
    
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(10);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  duration = rdPulseIn(echoPin, HIGH,29);
  distance_in=duration/148;

/*The following if statement will publish events for people entering the driveway*/
/*A0 is an led that is also toggle so that by looking at the Particle we know the code has been tripped*/
/*Note that the rdPulseIn function returns 0 for any distance that 'timeouts'*/

/*Because the sensor gave false readings approximately 1 of 10 times... the next several if statements 
repeatedly verify the measurement*/
 
 if (distance_in>20&&distance_in<60)/*if statement #1*/
 {
     // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
     // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
     digitalWrite(trigPin, LOW);
     delayMicroseconds(10);
     digitalWrite(trigPin, HIGH);
     delayMicroseconds(10);
     digitalWrite(trigPin, LOW);
     // Read the signal from the sensor: a HIGH pulse whose
     // duration is the time (in microseconds) from the sending
     duration2 = rdPulseIn(echoPin, HIGH,29);
     distance_in2=duration2/148;
     Serial.print("1.1");
 }/*end if statement #1*/
     if (distance_in2>20&&distance_in2<60)/*if statement #2*/
     {
         // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
         // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
         digitalWrite(trigPin, LOW);
         delayMicroseconds(10);
         digitalWrite(trigPin, HIGH);
         delayMicroseconds(10);
         digitalWrite(trigPin, LOW);
         // Read the signal from the sensor: a HIGH pulse whose
         // duration is the time (in microseconds) from the sending
         duration3 = rdPulseIn(echoPin, HIGH,29);
         distance_in3=duration3/148;
         Serial.print("1.2");
     }/*end if statement #2*/
     if (distance_in3>20&&distance_in3<60)/*if statement #3*/
     {
         // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
         // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
         digitalWrite(trigPin, LOW);
         delayMicroseconds(10);
         digitalWrite(trigPin, HIGH);
         delayMicroseconds(10);
         digitalWrite(trigPin, LOW);
         // Read the signal from the sensor: a HIGH pulse whose
         // duration is the time (in microseconds) from the sending
         duration4 = rdPulseIn(echoPin, HIGH,29);
         distance_in4=duration4/148;
         Serial.print("1.3");
     }/*end if statement #3*/
     if (distance_in4>20&&distance_in4<60)/*if statement #4*/
     {
         // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
         // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
         digitalWrite(trigPin, LOW);
         delayMicroseconds(10);
         digitalWrite(trigPin, HIGH);
         delayMicroseconds(10);
         digitalWrite(trigPin, LOW);
         // Read the signal from the sensor: a HIGH pulse whose
         // duration is the time (in microseconds) from the sending
         duration5 = rdPulseIn(echoPin, HIGH,29);
         distance_in5=duration5/148;
         Serial.print("1.4");
     }/*end if statement #4*/

     if (distance_in5>20&&distance_in5<60)/*if statement #5*/
     {
         distance_in=distance_in5;
         duration=duration5;
         /*serial data for troubleshooting*/
         Serial.print("1.5");
         Serial.print("/Someone-is-in-the-drivewway/");
         Serial.print("Distance=");
         Serial.print(String(distance_in4));
         Serial.print(duration);
         Serial.println("Microseconds/");
         
         guest_counter=guest_counter+1;
         Particle.publish("Guest_at_Home","true");/*event published for 'IFTTT' (for email notification) and the second particle (for buzzer)*/
         Particle.publish("Guest_Counts",String(guest_counter));/*event published for 'thingspeak' to graph data*/
         
         /*The following line prevents one passing car from being counted twice*/
         /*and lights an led to acknowledge visually that the particle code worked*/
         digitalWrite(A1,HIGH);
         delay(6000);
         digitalWrite(A1,LOW);
         
         /*The following if statement sends pulses to the power brick to keep it on*/
         digitalWrite(D0,HIGH);
         delay(250);
         digitalWrite(D0,LOW);
     }/*end if statement #5*/
     
 
 
 
 
 /*The following if statement will publish events for traffic passing by*/
 
 /*the sensor didn't give many false readings, if any, from this distance... the next three if statements 
wil still reduce the probablity of getting a false reading as an extra precaution*/

 else if (distance_in>60&&distance_in<170)/*if statement #1*/
 {
     // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
     // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
     digitalWrite(trigPin, LOW);
     delayMicroseconds(10);
     digitalWrite(trigPin, HIGH);
     delayMicroseconds(10);
     digitalWrite(trigPin, LOW);
     // Read the signal from the sensor: a HIGH pulse whose
     // duration is the time (in microseconds) from the sending
     duration6 = rdPulseIn(echoPin, HIGH,29);
     distance_in6=duration6/148;
     Serial.print("1.2.1");
 }/*if statement #1*/ 
 if (distance_in6>60&&distance_in6<170)/*if statement #2*/ 
 {
     // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
     // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
     digitalWrite(trigPin, LOW);
     delayMicroseconds(10);
     digitalWrite(trigPin, HIGH);
     delayMicroseconds(10);
     digitalWrite(trigPin, LOW);
     // Read the signal from the sensor: a HIGH pulse whose
     // duration is the time (in microseconds) from the sending
     duration7 = rdPulseIn(echoPin, HIGH,29);
     distance_in7=duration7/148;
     Serial.print("1.2.2");
 }/*if statement #2*/  
  if (distance_in7>60&&distance_in7<170)/*if statement #3*/ 
 {
     // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
     // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
     digitalWrite(trigPin, LOW);
     delayMicroseconds(10);
     digitalWrite(trigPin, HIGH);
     delayMicroseconds(10);
     digitalWrite(trigPin, LOW);
     // Read the signal from the sensor: a HIGH pulse whose
     // duration is the time (in microseconds) from the sending
     duration8 = rdPulseIn(echoPin, HIGH,29);
     distance_in8=duration8/148;
     Serial.print("1.2.3");
 }/*if statement #3*/ 
     if (distance_in8>60&&distance_in8<170)/*if statement #4*/
     {
         distance_in=distance_in8;
         /*serial data for troubleshooting*/
         Serial.print("1.2.4");
         Serial.print("/Traffic-passed-by/");
         Serial.println("Microseconds=");
         Serial.print(duration);
         
         traffic_counter=traffic_counter+1;
         Particle.publish("Plus_One_for_Traffic","true");
         Particle.publish("Traffic_Counts",String(traffic_counter));/*event published for 'thingspeak' to graph data*/
         
         /*The following line prevents one passing car from being counted twice*/
         delay(2000);
    
     /*The following if statement sends pulses to the power brick to keep it on*/
        digitalWrite(D0,HIGH);
        delay(250);
        digitalWrite(D0,LOW);     
     }/*end if statement #4*/


/*The following if statement sends pulses at leasst every 20 seconds to the power brick to keep it on*/ 
 else if (Time.second()==0||Time.second()==20||Time.second()==40)
     {
        digitalWrite(D0,HIGH);
        delay(250);
        digitalWrite(D0,LOW);
     }    
   
/*The following if statement will restart the 'traffic' and 'guest' counters at midnight*/   
  if (Time.hour()==0&&Time.minute()==0)
  {
      traffic_counter=0;
      guest_counter=0;
      Serial.print(traffic_counter);
      Serial.print(guest_counter);
  }
  
  looptime=micros()-nowtime;
  Serial.print("/time to loop=");
  Serial.print(looptime);

/*reinitialize variables*/
distance_in=0;
distance_in2=0;
distance_in3=0;
distance_in4=0;
distance_in5=0;
distance_in6=0;
distance_in7=0;
distance_in8=0;
  
}/*end of loop*/



/*!!!Below is the rdPulseIn function with a timeout specificaiton: in the event the sensor sees nothing it will timeout instead of continuing to wait and returning false measurements!!!*/
unsigned long rdPulseIn(int pin, int value, int timeout) { // the following comments assume that we're passing HIGH as value. timeout is in milliseconds
    
    unsigned long now = micros();
    while(pinReadFast(pin) == value) { // wait if pin is already HIGH when the function is called, but timeout if it never goes LOW
        if (micros()-now > (timeout*1000)) {
            duration=0;            
            return 0;
        }
    }
    
    now = micros(); // could delete this line if you want only one timeout period from the start until the actual pulse width timing starts
    while (pinReadFast(pin) != value) { // pin is LOW, wait for it to go HIGH befor we start timing, but timeout if it never goes HIGH within the timeout period
        if (micros()-now > (timeout*1000)) { 
            duration=0;
            return 0;
        }
    }
    
    now = micros();
    while (pinReadFast(pin) == value) { // start timing the HIGH pulse width, but time out if over timeout milliseconds
        if (micros()-now > (timeout*1000)) {
            duration=0;
            return 0;
        }
    }
    
    return micros() - now;
}

Buzzer Sound

Arduino
load on the Particle IDE
void setup()
{                
    pinMode(D1, OUTPUT);  // buzzer on pin D1
    Particle.subscribe("Guest_at_Home",myHandler);
}

void myHandler(const char *event, const char *data)
{
    if (strcmp(data,"true")==0)
    {
        digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(200);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(200);digitalWrite(D1,HIGH);
        delay(200);digitalWrite(D1,LOW);delay(50);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(200);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);
        delay(400);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(100);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(2000);
        //series 2
        digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(200);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(200);digitalWrite(D1,HIGH);
        delay(200);digitalWrite(D1,LOW);delay(50);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(200);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);
        delay(400);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(100);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(2000);
        //series 3
        digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(200);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(200);digitalWrite(D1,HIGH);
        delay(200);digitalWrite(D1,LOW);delay(50);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(200);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);
        delay(400);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);delay(100);digitalWrite(D1,HIGH);delay(200);digitalWrite(D1,LOW);
    }
    
    else
    {
    delay(100);
    }
}

Credits

Shawn G Osborne

Shawn G Osborne

1 project • 2 followers
Tracy L Easter

Tracy L Easter

1 project • 3 followers

Comments