Tyler BlankenshipMelissa Demarest
Published © GPL3+

Track When Your Roommate Steals Your Food!

If you would like to be informed about when your roommate decides to munch on your snacks, see how to use a Particle Photon to do it.

BeginnerFull instructions provided2 hours842
Track When Your Roommate Steals Your Food!

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
Resistor 1k ohm
Resistor 1k ohm
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×2
Breadboard (generic)
Breadboard (generic)
×2

Software apps and online services

Particle.io
Maker service
IFTTT Maker service
Google Sheets
Google Sheets
Fritzing

Story

Read more

Schematics

Motion Sensor Photon 1

This Photon detects motion using the PIR Motion Sensor, lights up LED pin D7 and publishes the event

Notification Alert Photon 2

Photon 2 will be subscribe to Photon 1's events, then send a notification and light up the LED on pin D7 in return.

Code

Motion Sensor Photon 1 Code

C/C++
This code is used to sense motion using the PIR Sensor to light up the LED pin D7. It publishes the event to public so the Notification Photon can be alerted and send a notification when motion is detected.
int inputPin = D0;              // choose the 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("Missy-food-tracker","intact",PUBLIC);
      delay(1000);
      // Update the current state
      pirState = HIGH;
      setLED( pirState );
    }
  } else {
    if (pirState == HIGH) {
      Particle.publish("Missy-food-tracker","broken",PUBLIC);
      delay(1000);
      pirState = LOW;
      setLED( pirState );
    }
  }
}

void setLED( int state )
{
  digitalWrite( ledPin, state );
}

Notification Photon 2 Code

C/C++
This code will light up the LED on pin D7 whenever motion is detected on the Motion Sensor Photon 1, also, It will publish an even to It's console whenever motion is detected on the Motion Sensor Photon 1.
// -----------------------------------------
// Publish and Subscribe with PIR Motion Sensors
/* -----------------------------------------

We are going to Particle.publish a public event to the cloud.
That means that everyone can see you event and anyone can subscribe to it.
You and your buddy will both publish an event, and listen for each others events.

------------------------------------------*/


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 (lighting up the LED)
  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("Missy-food-tracker", 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 beam is broken.
  if (analogRead(photoresistor)>beamThreshold) {
    if (beamBroken==true) {
        Particle.publish("Tyler-food-tracker","intact",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("Tyler-food-tracker","broken",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,"intact")==0) {
    // if your buddy's beam is intact, then turn your board LED on
    digitalWrite(boardLed,HIGH);
     Particle.publish("Tyler-food-tracker","intact",60,PUBLIC);
  }
  else if (strcmp(data,"broken")==0) {
    // if your buddy's beam is broken, 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

Tyler Blankenship

Tyler Blankenship

1 project • 0 followers
Melissa Demarest

Melissa Demarest

1 project • 1 follower

Comments