Jeremiah SmithRuslanUNC
Published © GPL3+

Floodlight Security System

The Floodlight Security System uses the Particle Photon platform to turn on a lamp when motion is detected.

IntermediateFull instructions provided15 hours832
Floodlight Security System

Things used in this project

Hardware components

Photon
Particle Photon
×2
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Relay (generic)
×1

Story

Read more

Code

Code for Sensing Photon

C/C++
// This code is for the first photon, named "Photobatics".
// This photon is responsible for monitoring the status of the motion sensor
// When motion is detected, this photon publishes the data to the cloud.
//Finally, this photon is responsible for recieving a signal from the second photon, named "turtle_station", confirming that turtle station has turned the lamp on.
//When it recieves the confirmation signal, it will turn on the onboard LED. 

int boardLed = D7; // This is the LED that is already on your device. It will be used here to confirm that this photon is receiving confirmation from the other photon.


int motionsensor = A0; // This is where the motionsensor sensor pin is plugged in.

int Threshold; //Because the motionsensor is read with an analog pin, a threshold will be used to determine if the motionsensor is trigger "ON" or "OFF"

int power = A5; // This pin powers the motion sensor

bool motion = false; // This flag will be used to mark if we have a new status or not. We will use it in the loop.

// We start with the setup function.

void setup() {
    Particle.subscribe("ConfirmationLED", myHandler, "39004c000651353530373132");
    //set the modes of the pins which we need:
  pinMode(boardLed,OUTPUT);
  pinMode(motionsensor,INPUT);
  pinMode(power,OUTPUT);

  // Next, write the power of the motionsensor to be the maximum possible, which is 4095 in analog.
  digitalWrite(power,HIGH);

 //Ideally, the motion sensor pin will read 0 when there is no motion and 4095 with motion, but we'll accept anything within about 2000 ticks of those values
  Threshold = 2000;

}

void myHandler(const char *event, const char *data) {
    if (strcmp(data, "ON")==0) {
    digitalWrite(boardLed , HIGH);
    }
    
    else {
        delay(2000);
        digitalWrite(boardLed, LOW);
    }
}
// Now for the loop.

void loop() {

  if (analogRead(motionsensor)<Threshold) {

    if (motion==true) {
        // If there was not already motion before, then this is a new status.
        // We will send a publish to the cloud
        Particle.publish("FloodLight800916541","OFF",60);

        // Finally, set the flag to reflect the current status of the motion.
        motion=false;
    }
    else {
        // Otherwise, this isn't a new status, and we don't have to do anything.
    }
  }

  else {
      // If you are below the threshold, there is no motion
      if (motion==false) {

        // Send a publish...
        Particle.publish("FloodLight800916541","ON",60);

        // Finally, set the flag to reflect the current status of the motion.
        motion=true;
      }
      else {
          // Otherwise, this isn't a new status, and we don't have to do anything.
      }
  }
}

Code for Control Photon

C/C++
int led= D7;

void setup() {
    Particle.subscribe("FloodLight800916541", myHandler, "3f0025000f47343438323536");
    pinMode(led, OUTPUT);
    digitalWrite(led, LOW);

}

void myHandler(const char *event, const char *data) {
    if (strcmp(data, "ON")==0) {
    digitalWrite(led , HIGH);
    Particle.publish("ConfirmationLED", "ON", 60);
    Particle.publish("Thingspeak", "1", 60);
    }
    
    else {
        delay(15000);
        digitalWrite(led, LOW);
        Particle.publish("ConfirmationLED", "OFF", 60);
        Particle.publish("Thingspeak", "0", 60);
    }
}

void loop() {
}

Credits

Jeremiah Smith

Jeremiah Smith

1 project • 4 followers
RuslanUNC

RuslanUNC

0 projects • 5 followers

Comments