Brian Wente
Published © GPL3+

YAPGO

Yet Another Particle Garage Opener

BeginnerShowcase (no instructions)2,974
YAPGO

Things used in this project

Hardware components

Photon
Particle Photon
×1
Breadboard (generic)
Breadboard (generic)
×1
2 Channel 5V Relay Shield Module for Arduino
×1
Reed Switch
×1

Software apps and online services

Particle Web IDE
IFTTT
MODX Content Management Framework

Hand tools and fabrication machines

screwdriver

Story

Read more

Schematics

Wiring Diagram

Code

Garage

C/C++
int interval = 1000;

int state = 1;
char oldState;
int ts = Time.now();

int IN1 = D1; // variable for IN1 on relay
int IN2 = D2; // variable for IN2 on relay
int reed = D5; // variable for reed switch
int led = D7;

int doorstatus = digitalRead(reed); //variable for door status

String statusCheck(){
    int status = digitalRead(reed);
    if(status == HIGH){
        return "CLOSED";
    }
    else if(status == LOW){
        return "OPEN";
    }
    else{
        return "OFFLINE";
    }
}

void setup() {
    // Time.zone(-5);
    Spark.function("relay", relayPulse); 
    Spark.function("open", openDoor);

    pinMode(reed, INPUT_PULLDOWN);
    pinMode(led, OUTPUT);
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    
    Spark.variable("state", &state, INT);
    Spark.variable("ts", &ts, INT);
    // startup status
    Spark.publish("door_status", statusCheck());
    Serial.begin(9600);

}


void loop() {

	oldState = digitalRead(reed); //Check the current state
	
	if (doorstatus != oldState){ //Compare current state to status.
		doorstatus = oldState; //Make oldState the new status
		Spark.publish("door_status", statusCheck());
		state = doorstatus;
		ts = Time.now();
		digitalWrite(led, (doorstatus) ? HIGH : LOW);
	}
    
   delay(interval); //Delay Interval millisecond, as to not to overload the server.

}
// for loacation based IFTTT
int openDoor(String command){
    if (statusCheck() == "CLOSED"){ 
    relayPulse(command);
    return 1;
    }
    return 0;
};

int relayPulse(String command){
    Spark.publish("button pushed by ", command);
    digitalWrite(IN1, HIGH);
    delay(2000);
    digitalWrite(IN1, LOW);
    return 1;
};                  

ParticleGet

PHP
This is a MODX "Snippet". It uses curl and then returns the results formatted with a "chunk".
<?php

$device = "torpedo";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$varRequest = $modx->getOption('varRequest', $scriptProperties, '');
$type = $modx->getOption('type', $scriptProperties, 'int');
$tpl = $modx->getOption('tpl', $scriptProperties, '');

$tplBoolean0 = $modx->getOption('tplBoolean0', $scriptProperties, 'DoorOpen');
$tplBoolean1 = $modx->getOption('tplBoolean1', $scriptProperties, 'DoorClosed');
$tplOffline = $modx->getOption('tplOffline', $scriptProperties, 'DoorOffline');

$rootUrl = "https://api.particle.io/v1/devices/";
$url = $rootUrl . $device . "/" . $varRequest . "?access_token=" . $token;

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec($ch);
curl_close($ch);

$varReturn = json_decode($result, true);

if ($type=="bool"){
if ($varReturn["result"]==1){
     $output = $modx->getChunk($tplBoolean1);
} elseif ($varReturn["result"]==0 && isset($varReturn["result"])){
     $output = $modx->getChunk($tplBoolean0);
} else {
     $output = $modx->getChunk($tplOffline);
}
}

if ($type=="int"){
$varResult = $varReturn["result"];
$prop = array(
    'varResult' => $varResult
);
$output = $modx->getChunk($tpl, $prop);
}

return $output;

ParticlePut

PHP
This is a MODX "Snippet". It uses curl to post the user and send the command to fire the relay function.
<?php
$user = $modx->getUser();
$profile = $user->getOne('Profile');
$fullname = $profile->get('fullname');

$device = "torpedo";
$cmd = "relay";

$rootUrl = "https://api.particle.io/v1/devices/";
$url = $rootUrl . $device . "/" . $cmd;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "access_token=xxxxxxxxxxxxxxxxxxxxxxxx&args=".$fullname);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);
curl_close($ch);

Credits

Brian Wente

Brian Wente

3 projects • 7 followers

Comments