/**Control and Query The Garage Door
By Paul Elsberg
Hardware
Button:
Connect the Garage door power to the VIN IF you want to power the photon from the coin cell battery on the charger!!!
Connect Garage Door ground to Photon ground
Find and connect a signal generated by pressing the opener button to center bottom C of a PNP transistor (my button push signal was - )
| ^
v |
Connect the ground to the top of the transistor C
connect D0 to the center pin of the transistor to use a HIGH signal to trigger the flow of current and a LOW signal to stop it
Force Sensor:
Connect 3.3v to 1kohm to one pin of force sensor
Connect A0 on to the resistor (force pin side)
Connect ground to other pin of force sensor
link to visual
https://community.particle.io/t/coffee-break-anybody-with-maker-kit-publish/4091?redirected=true
**/
//declar variables
int force = 0;
int button = D0;
void setup() {
//Sensing Force
pinMode(A0,INPUT);
//controlling opener
pinMode(button,OUTPUT);
Particle.variable("force",&force,INT);
//find out the state of the door
Particle.function("garageStatus",garageStatus);
//tell the door to close or open
Particle.function("garageButton",garageButton);
Serial.begin(9600);
}
void loop() {
}
//measures state using force threshhold
int garageStatus(String command){
//read force of door
force = (int)analogRead(A0);
//adjust threshhold to your setup
if(force > 400){
//the garageStatus event provides various data based on the state
Particle.publish("garageStatus","closed");
//returning an integer allows other particle functions to easily communicate
return 1;
}
else{
Particle.publish("garageStatus","open");
return 0;
}
}
//tell the garage whether to close or open
int garageButton(String command){
//accept both to account for automatic uppercase when starting a text
if ((command =="close")||(command=="Close")){
//make sure that the door is open
if(garageStatus("")==0){
//hit the garage button
digitalWrite(button,HIGH);
delay(200);
digitalWrite(button,LOW);
//wait the time it takes for YOUR garage to close
delay(5000);
//ensure the garage did actually close
if(garageStatus("")==1)
{
Particle.publish("garageStatus","successClose");
//sucess
return 1;
}
//either the force sensor is not contacted by the door or something is in the way
else{
Particle.publish("garageStatus","notWorking");
//failure
return 0;
}
}
//the door doesn't need to be closed
else{
Particle.publish("garageStatus","alreadyClose");
//desired state achieved
return 1;
}
}
//similar structure to open the door
else if ((command =="open")||(command=="Open")){
//if the door's closed
if(garageStatus("")==1){
//hit the garage door button
digitalWrite(button,HIGH);
delay(200);
digitalWrite(button,LOW);
//wait the time it takes for YOUR garage door to open
delay(5000);
//make sure it opened
if(garageStatus("")==0)
{
Particle.publish("garageStatus","successOpen");
//success
return 1;
}
//button's not working
else{
Particle.publish("garageStatus","notWorking");
//failure
return 0;
}
}
//the door doesn't need to be opened
else{
Particle.publish("garageStatus","alreadyOpen");
//desired state achieved
return 1;
}
}
//not a valid command
else{
return 0;
}
}
Comments