Joey SuprickMatt Clermont
Published

Gotcha Security System

Leave your bedroom door unlocked with ease knowing no one has entered your room. They will get caught otherwise.

IntermediateShowcase (no instructions)4 hours888
Gotcha Security System

Things used in this project

Hardware components

PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Photon
Particle Photon
×2
LED (generic)
LED (generic)
×2
Breadboard (generic)
Breadboard (generic)
×2
Jumper wires (generic)
Jumper wires (generic)
×1
keypad
×1

Software apps and online services

Maker service
IFTTT Maker service

Story

Read more

Schematics

PIR Sensor Diagram

Switch Diagram

Code

PIR Sensor

C/C++
This is a code for a PIR Sensor that will detect if there is motion
int supply = D0;
int sensor = A1;


int val = 0;



int intactValue;
int brokenValue;
int beamThreshold;

bool beamBroken = false;

// We start with the setup function.

void setup() {
  
 
 
  pinMode(sensor,INPUT);  
  pinMode(supply,OUTPUT); 

  // Here we are going to subscribe to your buddy's event using Particle.subscribe
 
  // Subscribe will listen for the event motiondetect and, when it finds it, will run the function myHandler()
  
  


  Particle.subscribe("Security_System_IOTProject_289494", myHandler);

  intactValue = 4095;
  brokenValue = 0;
  beamThreshold = 2000;

}




void loop() {
  // This loop sends a publish when the motion is detected.
  
  
   
    
   
   
if (val == 1)
   {
   
  if (analogRead(sensor)>beamThreshold) {
    if (beamBroken==true) {
        Particle.publish("motiondetect289494","broken");
        // publish this public event
        
        
        // Set the flag to reflect the current status of the beam.
        beamBroken=false;
        delay(2000);
    }
  }

  else {
      if (beamBroken==false) {
        // Same deal as before...
        Particle.publish("motiondetect289494","intact");
        beamBroken=true;
        delay(2000);
      }
      
  }
  
  
    }

    
  





else if (val == 0)
{
    delay(2000);
}
else
{

}
}


void myHandler(const char *event, const char *data)
{
  /* Particle.subscribe handlers are void functions, which means they don't return anything.
  They take two variables-- the name of your event, and any data that goes along with your event.
  In this case, the event will be "buddy_unique_event_name" and the data will be "intact" or "broken"

  Since the input here is a char, we can't do
     data=="intact"
    or
     data=="broken"

  chars just don't play that way. Instead we're going to strcmp(), which compares two chars.
  If they are the same, strcmp will return 0.
  */

  if (strcmp(data,"0")==0) {
    // if your buddy's beam is intact, then turn your board LED off
    val = 1;
  }
  else if (strcmp(data,"1")==0) {
    // if your buddy's beam is broken, turn your board LED on
    val = 0;
  }
  else {
    // if the data is something else, don't do anything.
    // Really the data shouldn't be anything but those two listed above.
  }
}

Switch code

C/C++
const String key = "OHI4ETVQAXIEXK3C";
int ledgreen = A3;
int ledred = A0;
int pushButton = D0; // Push button is connected to D0
int var = 0;
int value = analogRead(D7);
#define publish_delay 10000
unsigned int lastPublish = 0;


void setup()
{

   // Here's the pin configuration, same as last time
   pinMode(ledgreen, OUTPUT);
   pinMode(ledred, OUTPUT);
   pinMode(pushButton, INPUT_PULLUP); 
   pinMode(D7, INPUT_PULLDOWN);
  // Initialize D2 pin as input with an internal pull-up resistor

   digitalWrite(ledgreen, HIGH);
   digitalWrite(ledred, LOW);

}



void loop()
{
    
    
    int pushButtonState = digitalRead(pushButton);
    delay(100);
  if(pushButtonState == LOW)
  { 
      delay(100);
    if(var == 1)
    {
        
    digitalWrite(ledgreen, HIGH);

    digitalWrite(ledred, LOW);
    
    Particle.publish("Security_System_IOTProject_289494", "0", 10000, PUBLIC);
    
    var = 0;
    
    
    
    }
    
    else if (var == 0) {
    
    digitalWrite(ledgreen, LOW);
    
    digitalWrite(ledred, HIGH);
    
    Particle.publish("Security_System_IOTProject_289494", "1", 10000, PUBLIC);
    
    var = 1;
      
    
    }
    
    
    
  }
  else {
      
  }
  
  
  unsigned long now = millis();
    if ((now - lastPublish) < publish_delay) {
        return;
    }
    
    Particle.publish("thingSpeakWrite_A0", "{ \"1\": \"" + String(value) + "\", \"k\": \"OHI4ETVQAXIEXK3C\" }", 60, PRIVATE);
    lastPublish = now;
 
   
}

Credits

Joey Suprick

Joey Suprick

1 project • 0 followers
Matt Clermont

Matt Clermont

1 project • 0 followers

Comments