Things used in this project

Hardware components:
Photon new
Particle Photon
×1
SparkFun Magnetic Door Switch Set
×1
12002 04
Breadboard (generic)
×1
11026 02
Jumper wires (generic)
×1
Software apps and online services:
Fritzing
Hand tools and fabrication machines:
Lasercutter
Laser cutter (generic)

Custom parts and enclosures

Box Schematics
Blueprints of our box in which we put our sensor
group410_veJPptJ4TV.ai
Blueprint
Group 410 blueprints ro5fzj98vk

Schematics

Fritzing Diagram
Fritzing qgl4ieb8a1

Code

Door Switch SensorArduino
// This #include statement was automatically added by the Particle IDE.
#include <SparkFunPhant.h>

double value1 = 0; 
double value2 = 0;

const char server[] = "data.sparkfun.com"; // Phant destination server
const char publicKey[] = "q5YZ7aN78LtYN3vZqyvr"; // Phant public key 
const char privateKey[] = "BV41ZGJZeoHJEnrRyor8"; // Phant private key
Phant phant(server, publicKey, privateKey); // Create a Phant object

const int POST_RATE = 3000; // Time between posts, in ms.
unsigned long lastPost = 0; // global variable to keep track of last post time

int magnetValState;
int magnetPin = D3; // Magnet part is connected to D3
int led1 = D1;

void setup()
{
    pinMode(magnetPin, INPUT_PULLUP);   // Initialize D3 pin as input with an internal pull-up resistor
    pinMode(led1, OUTPUT); //Initiates LED light
    Serial.begin(9600);
}

void loop()
{
    value1++;
    value2++;

    magnetValState = digitalRead(magnetPin);
    if(magnetValState == HIGH)      // Was motion detected
        {
            digitalWrite(led1, HIGH);
            postToPhant();
        }
    else
        {
            digitalWrite(led1, LOW);
            postToPhant();
        }
}

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.
    if(magnetValState == HIGH)
        phant.add("door", 1); //0 - closed; 1 - open
    else
        phant.add("door", 0);
        	
    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

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Blynk GPS Tracker
Intermediate
  • 41
  • 2

Full instructions

Simple GPS tracker using a Particle Photon (or Electron) and an EM406 GPS module with location visible on Blynk app map.

Pool Buddy
Intermediate
  • 1,619
  • 11

Work in progress

Monitor and log water quality (pH & ORP) and temperature from everywhere.

IoT Thermometer Using Python
Intermediate
  • 986
  • 8

Full instructions

How to develop a simple but powerful IoT thermometer using a Zerynth-powered single-board MCU and the Zerynth App.

Particle Photon Flip Dot Clock
Intermediate
  • 2,162
  • 31

Full instructions

The flip dot display uses an electromechanical dot matrix that spin from black to yellow to display text, numbers and more!

Patriot iOS App
Intermediate
  • 547
  • 9

Use an old iPhone device to control and display the status of your Patriot IoT devices.

Patriot iOS App

Team Patriot

What To Wear LCD
Intermediate
  • 48
  • 1

Ever wondered what to wear due to unpredictable weather??? Wonder no longer!

Add projectSign up / Login