Faisal AlsarrafTimothy GroffAyoub Rustum
Published

IoT Home Security Project

A simple IoT project using Particle Photon devices and an ultrasonic sensor to detect the unauthorized opening of your home's main door.

IntermediateFull instructions provided15 hours1,787
IoT Home Security Project

Things used in this project

Hardware components

Photon
Particle Photon
×3
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×3
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Male/Male Jumper Wires
×11
LED (generic)
LED (generic)
×3
Through Hole Resistor, 470 ohm
Through Hole Resistor, 470 ohm
×1
Resistor 1k ohm
Resistor 1k ohm
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Maker service
IFTTT Maker service

Story

Read more

Schematics

Fritzing Diagram for Photon 1

Fritzing Diagram for Photons 2 and 3

Code

Code for Photon 1

C/C++
This is the code used for the photon that has a sensor attached to it, and is responsible for the detection of a door opening.
unsigned long sensorData;
unsigned long startingTime;
long lastTimer;
bool opCheck; 

void setup() {
    pinMode(D7, OUTPUT); //LED1: Built in LED will be used for messages coming from Third Photon.
    pinMode(D3, OUTPUT); //LED2: Added LED will be used for messages coming from Second Photon.
    pinMode(D2, OUTPUT); //TrigPin: This will be used as the trigger for the sensor.
    pinMode(A2, INPUT); //EchoPin: This will be used as the echo pin for the sensor.
    attachInterrupt(A2, echoPin, FALLING); //Falling Edge interrupt attached including echo pin function time
    
    Particle.subscribe("TimGroff3171", myHandler);
    Particle.subscribe("AyoubRustum3171", myHandler2);
}


void loop() {
    if (opCheck == false && millis() - lastTimer > 1000 ) { //To Check the operation of the system
        opCheck = true; //Check if theres no operation for over a second
        
        sensorData = 0; 
        lastTimer = millis(); //Set current time for the timer
        
        digitalWrite(D2, HIGH); //activate trigPin to send wave
        delayMicroseconds(10); //10 microseconds needed for wave to be sent and bounced back
        digitalWrite(D2, LOW); //Turn off trigPin
        
        startingTime = micros();  // get time now for start
    }

    if (sensorData) { //When sensor gets under certain threshold, publish command to friend's Photons:
        if (sensorData < 2000) { // This data is what ultrasonic sensor reads. 2000-3000 found to be ideal range for this application.
            Particle.publish("FaisalAlsarraf3171", "Intrusion");
        }
    
        else { //Do nothing if sensor data is over the threshold
        }
    
        opCheck = false; //Resetting the operation check variable
        sensorData = 0; //Resetting the sensor data value
        delay(10); //Providing a 10 millisecond delay to avoid issues
    }
}


void echoPin() {
    sensorData = micros() - startingTime; //Time function for echo pin
}


void myHandler(const char *TimGroff3171, const char *data) { //Light White LED when data is received from Tim's Photon
    digitalWrite(D3, HIGH);
    delay(1000);  
    digitalWrite(D3, LOW);
    delay(10);
}


void myHandler2(const char *AyoubRustum3171, const char *data) { //Light Built in Blue LED when data is received from Ayoub's Photon
    digitalWrite(D7, HIGH);
    delay(1000);  
    digitalWrite(D7, LOW);
    delay(10);
}

Code for Photon 2

C/C++
This is the code used for Tim's Photon, the connecting link between Photons 1 and 3.
void setup()
{   
    Particle.subscribe("FaisalAlsarraf3171", myHandler);
    Particle.subscribe("AyoubRustum3171", myHandler2);
    
    pinMode(D7, OUTPUT);   // Primary (Blue) Built in LED
    pinMode(D4, OUTPUT);   // Secondary (White) LED
}
void myHandler(const char *FaisalAlsarraf3171, const char *data) { 
    digitalWrite(D7, HIGH);
    delay(1000);  
    digitalWrite(D7, LOW);
    Particle.publish("TimGroff3171", "NotifyPhotons");
    delay(10);
    
}
void myHandler2(const char *AyoubRustum3171, const char *data) { 
    digitalWrite(D4, HIGH);
    delay(1000);
    digitalWrite(D4, LOW);
    delay(10);
}

Code for Photon 3

C/C++
This is the code for Ayoub's Photon, the third and final photon in the chain.
void setup()
{   
    Particle.subscribe("FaisalAlsarraf3171", myHandler);
    Particle.subscribe("TimGroff3171", myHandler2);

    pinMode(D7, OUTPUT);   // Primary (Blue) Built in LED
    pinMode(D2, OUTPUT);   // Secondary (White) LED 
}

void myHandler(const char *TimGroff3171, const char *data) { 
    digitalWrite(D7, HIGH);
    delay(1000);  
    digitalWrite(D7, LOW);
    Particle.publish("AyoubRustum3171","SendMessage");
    delay(10);
    
}

void myHandler2(const char *FaisalAlsarraf3171, const char *data) { 
    digitalWrite(D2, HIGH);
    delay(1000);  
    digitalWrite(D2, LOW);
    delay(10);

    
}

Credits

Faisal Alsarraf

Faisal Alsarraf

1 project • 2 followers
Timothy Groff

Timothy Groff

1 project • 1 follower
Ayoub Rustum

Ayoub Rustum

1 project • 1 follower
Thanks to John R McAlpine.

Comments