Jorge MarreroMarco MedinaDaniyal Latif
Published

Lane of Things Group 708

Our group used a proximity sensor placed near a door to see how many people go in and out of the attendance office.

BeginnerFull instructions provided5 hours565
Lane of Things Group 708

Things used in this project

Hardware components

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

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)

Story

Read more

Custom parts and enclosures

Enclosure Case Plans

Illustrator file for our enclosure which can be laser cut to create the enclosure that we used.

Schematics

Fritzing Diagram

Fritzing diagram of our device. It includes the breadboard, the photon, the motion sensor and where each wires should go.

Code

Code for PIR Motion Sensor

Arduino
This is the code that we used for our project which is done in particle.io.
// This #include statement was automatically added by the Particle IDE.
#include <SparkFunPhant.h>
int ledPin = 7;                // choose the pin for the LED
int inputPin = 0;               // choose the input pin (for PIR sensor)
int pirState = HIGH;             // we start, assuming no  detected
int count = 0;
int val = 0;
int value1 = 0;
int value2 = 0;
const char server[] = "data.sparkfun.com"; // Phant destination server
const char publicKey[] = "n14Zx31l5mtK19j4m97j"; // Phant public key - HAS TO BE CHANGED
const char privateKey[] = "MoM7WxopDVCM8426Z472"; // Phant private key  - HAS TO BE CHANGED
Phant phant(server, publicKey, privateKey); // Create a Phant object
const int POST_RATE = 3000; // Time between posts, in ms.
unsigned long lastPost = 0;
void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input

  Serial.begin(9600);
}
void loop(){
  value1++;
  value2++;   
  int val = digitalRead(inputPin);  // read input value
  if (val == HIGH) { 
      // check if the input is HIGH
    digitalWrite(ledPin, HIGH);// turn LED ON
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      count=count+1;
      Serial.println(count);
      postToPhant();
      
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;

    }
  }
}




 // global variable to keep track of last post time
    //Replace the if statement below with whatever your trigger is

int postToPhant()
{    
    // Use phant.add(<field>, <value>) to add data to each field.
    // Phant requires you to update each and every field before posting,
    // make sure all fields defined in the stream are added here.
    phant.add("motion", val);
    phant.add("count", count);
        	
    TCPClient client;
    char response[512];
    int i = 0;
    int retVal = 0;
    
    if (client.connect(server, 80)) // Connect to the server
    {
		// Post message to indicate connect success
        Serial.println("Posting!"); 
		
		// phant.post() will return a string formatted as an HTTP POST.
		// It'll include all of the field/data values we added before.
		// Use client.print() to send that string to the server.
        client.print(phant.post());
        delay(1000);
		// Now we'll do some simple checking to see what (if any) response
		// the server gives us.
        while (client.available())
        {
            char c = client.read();
            Serial.print(c);	// Print the response for debugging help.
            if (i < 512)
                response[i++] = c; // Add character to response string
        }
		// Search the response string for "200 OK", if that's found the post
		// succeeded.
        if (strstr(response, "200 OK"))
        {
            Serial.println("Post success!");
            retVal = 1;
        }
        else if (strstr(response, "400 Bad Request"))
        {	// "400 Bad Request" means the Phant POST was formatted incorrectly.
			// This most commonly ocurrs because a field is either missing,
			// duplicated, or misspelled.
            Serial.println("Bad request");
            retVal = -1;
        }
        else
        {
			// Otherwise we got a response we weren't looking for.
            retVal = -2;
        }
    }
    else
    {	// If the connection failed, print a message:
        Serial.println("connection failed");
        retVal = -3;
    }
    client.stop();	// Close the connection to server.
    return retVal;	// Return error (or success) code.
}

Credits

Jorge Marrero

Jorge Marrero

-1 projects • 1 follower
Marco Medina

Marco Medina

-1 projects • 0 followers
Daniyal Latif

Daniyal Latif

-1 projects • 0 followers

Comments