Waleed AlgamdiAbdulrahman Alghamdi
Published

Security System

MEGR3171 Introduction to Instrumentation - Fall 2017 @UNC Charlotte / 2017 Fall IOT Project

IntermediateFull instructions provided12.5 hours1,613
Security System

Things used in this project

Hardware components

Photon
Particle Photon
×2
Breadboard (generic)
Breadboard (generic)
×2
Jumper wires (generic)
Jumper wires (generic)
×1
Buzzer
Buzzer
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
power bank
×1

Software apps and online services

ThingSpeak API
ThingSpeak API
Maker service
IFTTT Maker service

Story

Read more

Schematics

Ultrasonic Sensor Circuit

Buzzer Circuit

Thief Count Graph

Code

Ultrasonic Sensor

C/C++
/* 
   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 = D6;
const int echoPin = D5;
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_in5>20&&distance_in5<60)/*if statement #5*/
     {
         distance_in=distance_in5;
         duration=duration5;
         /*serial data for troubleshooting*/
         Serial.print("1.5");
         Serial.print("/Someoneatthehouse/");
         Serial.print("Distance=");
         Serial.print(String(distance_in4));
         Serial.print(duration);
         Serial.println("Microseconds/");
         
         guest_counter=guest_counter+1;
         Particle.publish("Someone_at_Home","true");/*event published for 'IFTTT' (for email notification) and the second particle (for buzzer)*/
         Particle.publish("movement1",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_in8>60&&distance_in8<170)/*if statement #2*/
     {
         distance_in=distance_in8;
         /*serial data for troubleshooting*/
         Serial.print("1.2.4");
         Serial.print("/Someonehere/");
         Serial.println("Microseconds=");
         Serial.print(duration);
         
         traffic_counter=traffic_counter+1;
         Particle.publish("911","true");
         Particle.publish("movement2",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 #2*/


/*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 Code

C/C++
void setup()
{                
    pinMode(D1, OUTPUT);  // buzzer on pin D1
    pinMode(D7, OUTPUT); // LED
    Particle.subscribe("911",myHandler);
}

void myHandler(const char *event, const char *data)
{
    if (strcmp(data,"true")==0)
    {
        digitalWrite(D7,HIGH);
        delay(1000);
        digitalWrite(D7,LOW);
        
        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);

    
    else
    {
    delay(100);
    }
}

Credits

Waleed Algamdi

Waleed Algamdi

2 projects • 3 followers
Abdulrahman Alghamdi

Abdulrahman Alghamdi

2 projects • 3 followers

Comments