AJ MennutiMason Slingluff
Published

Motion detected gun safe alarm!

When it comes to weapons there is always room for more safety. Give yourself some needed piece of mind with an extra layer of security.

BeginnerFull instructions provided2 hours2,273
Motion detected gun safe alarm!

Things used in this project

Hardware components

Photon
Particle Photon
×2
Jumper wires (generic)
Jumper wires (generic)
×4
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Breadboard (generic)
Breadboard (generic)
×2
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×2

Software apps and online services

Maker service
IFTTT Maker service
Particle Pi
Particle Pi
Fritzing.org
Actions on Google
Actions on Google

Story

Read more

Schematics

Masons Particle - Motion Sensing

Masons particle board is responsible for sensing the motion in the gun case. When motion sensor is activated the LED light corresponding to pin 7 will turn on. The "Open" command will then be sent up to the cloud.

AJs Particle - IFTTT Integration

AJs Particle board is responsible for taking the event from Masons Particle from the cloud. When the "Open" command is received it will in tern light up the LED on Pin D7. The Applet in IFTTT will pick up on this and send an alert to my cell phone.

Code

Masons Particle Code - Motion Sensing

C/C++
This Code uses the motion sensor attached to the Particle Photon to detect when the case has be opened. It will then set off the LED light at pin D7. Finally, the program will publish the event to the cloud where it can be called on for further operation.
int inputPin = D0;              //  input 
int ledPin = D7;                // LED Pin
int pirState = LOW;             // assuming no motion detected
int val = 0;                    // variable for reading the pin
int calibrateTime = 1000;      // wait for calibratation to end
void setup()
{
  pinMode(ledPin, OUTPUT );
  pinMode(inputPin, INPUT);     // declare sensor as input
}
void loop()
{
  // if the sensor is calibrated
  if ( calibrated() )
  {
  // get the data from sensor
    readTheSensor();
    // report if the state has changed
    reportTheData();
  }
}
void readTheSensor() {
  val = digitalRead(inputPin);
}
bool calibrated() {
  return millis() - calibrateTime > 0;
}
void reportTheData() {
  // if the sensor reads high or there is motion
  if (val == HIGH) {
    // the current state is no motion
    // call the change by publishing an eent
    if (pirState == LOW) {
      // we have just turned on
      Particle.publish("Gun-Safe","Closed",PUBLIC);
      delay(1000);
      // Update the current state
      pirState = HIGH;
      setLED( pirState );
    }
  } else {
    if (pirState == HIGH) {
      Particle.publish("Gun-Safe","Opened",PUBLIC);
      delay(1000);
      pirState = LOW;
      setLED( pirState );
    }
  }
}
void setLED( int state )
{
  digitalWrite( ledPin, state );
}

AJs Particle Code - IFTTT Integration

C/C++
This Code will now call the event published from Masons Photon from the cloud. The LED at Pin D7 will be set off, showing that the event was received, and then the event will published to IFTTT. At this point the applet in IFTTT will recognize the specific event and send an alert notification to my cell phone.
int led = D0;
int boardLed = D7;
int photoresistor = A0;
int power = A5;

int intactValue;
int brokenValue;
int beamThreshold;

bool beamBroken = false;

// We start with the setup function.

void setup() {
      // This part is mostly the same:
  pinMode(led,OUTPUT); // Our LED pin is output
  pinMode(boardLed,OUTPUT); // Our on-board LED is output as well
  pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
  pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)


  // Here we are going to subscribe to your buddy's event using Particle.subscribe
  Particle.subscribe("Gun-Safe",myHandler);
  // Subscribe will listen for the event buddy_unique_event_name and, when it finds it, will run the function myHandler()
  // (Remember to replace buddy_unique_event_name with your buddy's actual unique event name that they have in their firmware.)
  // myHandler() is declared later in this app.
}


void loop() {
  // This loop sends a publish when the case is open.
  if (analogRead(photoresistor)>beamThreshold) {
    if (beamBroken==true) {
        Particle.publish("Phone Alert","Opened",60,PUBLIC);
        // publish this public event
        // rename your_unique_event_name with your actual unique event name. No spaces, 63 ASCII characters.
        // give your event name to your buddy and have them put it in their app.

        // Set the flag to reflect the current status of the beam.
        beamBroken=false;
    }
  }

  else {
      if (beamBroken==false) {
        // Same deal as before...
        Particle.publish("Phone Alert","Closed",60,PUBLIC);
        beamBroken=true;
      }
  }
}


// Now for the myHandler function, which is called when the cloud tells us that our buddy's event is published.
void myHandler(const char *event, const char *data)
{
  if (strcmp(data,"Opened")==0) {
    // if your buddy's case is open, then turn your board LED on
    digitalWrite(boardLed,HIGH);
     Particle.publish("Phone Alert","Opened",60,PUBLIC);
  }
  else if (strcmp(data,"Closed")==0) {
    // if your buddy's case is closed, turn your board LED off
    digitalWrite(boardLed,LOW);
  }
  else {
    // if the data is something else, don't do anything.
    // Really the data shouldn't be anything but those two listed above.
  }
}

Credits

AJ Mennuti

AJ Mennuti

1 project • 0 followers
Mason Slingluff

Mason Slingluff

1 project • 0 followers
Thanks to Attilio Mennuti and Mason Slingluff.

Comments