Can’t get it to format correctly on these forums
#include "elapsedMillis/elapsedMillis.h"
const long AutoCloseTime = 300000; //300000 Set Time for 5 mins no movement in garage then close door
const long DoorCloseErrTime = 30000; //30 seconds to close door, (Close time + Open backup up time) if still open then send error out
const long DoorOpenErrTime = 15000; //15 seconds to open door, if still closed then send error out
int DoorOpen = 0; int OpenDoor = 0; int CloseDoor = 0; int MotionTime = 0; int DoorClosing = 0; int DoorinError = 0; int TooLong = 0;
long Motion;
elapsedMillis LastPRIEvent;
int MotionPin = D4;
int DoorSwitchPin = D5;
int DoorCyclePin = D6;
int OpenRelayLED = D7;
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL))
void setup() {
pinMode (MotionPin, INPUT);
pinMode (DoorSwitchPin, INPUT);
pinMode (DoorCyclePin, OUTPUT);
pinMode (OpenRelayLED, OUTPUT);
delay(2000);
Spark.publish("GarageDoor","Booted: Firmware 1.55",60,PRIVATE);
Spark.subscribe("GarageDoorCommand", GDCommands, MY_DEVICES);
DoorOpen = digitalRead(DoorSwitchPin); //Read DoorSwitchPin and Set Var
if (DoorOpen == 1) {Spark.publish("GarageDoor","ColdBoot: Door is Open",60,PRIVATE);}
else {Spark.publish("GarageDoor","ColdBoot: Door is Closed",60,PRIVATE);}
}
void loop() {
CheckPRIandDoor();
CheckCloseStatus();
}
void CheckPRIandDoor(){
DoorOpen = digitalRead(DoorSwitchPin); //Read DoorSwitchPin and Set Var
if (DoorOpen == 1)
{
if (digitalRead(MotionPin)==HIGH && LastPRIEvent >= 5000) {LastPRIEvent = 0; {Spark.publish("GarageDoor","Motion",60,PRIVATE);}} // If motion detected then reset Auto-Close Timer
if (LastPRIEvent >= AutoCloseTime) {CloseDoor = 1; TooLong = 1;} // If door is open and AutoCloseTime reached, then Flag CloseDoor & TooLong Var
}
if (LastPRIEvent >= 540000) {LastPRIEvent = 0;{Spark.publish("GarageDoor","Reset Time",60,PRIVATE);}} // if the timer gets beyond 9 mins IE: Door is closed, then reset to keep from getting too large
}
void CheckCloseStatus() {
if (CloseDoor == 1) {
if (TooLong == 1) {TooLong = 0; Spark.publish("GarageDoorTxt","Left Open, Attempting to Close",60,PRIVATE);}
else {Spark.publish("GarageDoor","Attempting to Close",60,PRIVATE);}
CloseDoor = 0; LastPRIEvent = 0; DoorClosing = 1;
digitalWrite(DoorCyclePin, HIGH); digitalWrite(OpenRelayLED, HIGH); delay (500); digitalWrite(DoorCyclePin, LOW); digitalWrite(OpenRelayLED, LOW);// Pulse Relay to close door
delay (DoorCloseErrTime);
if (digitalRead(DoorSwitchPin) == HIGH) {
DoorClosing = 0;
DoorinError = 1;
Spark.publish("GarageDoorTxt","Error, Can't Close Door!!",60,PRIVATE);
}
else {
DoorClosing = 0;
DoorinError = 0;
Spark.publish("GarageDoorTxt","Confirmed Door Closed",60,PRIVATE);
}
}
if (OpenDoor == 1) {
LastPRIEvent = 0; OpenDoor = 0;
Spark.publish("GarageDoor","Attempting to Open",60,PRIVATE);
digitalWrite(DoorCyclePin, HIGH); digitalWrite(OpenRelayLED, HIGH); delay (500); digitalWrite(DoorCyclePin, LOW); digitalWrite(OpenRelayLED, LOW); // Pulse Relay to close door
delay (DoorOpenErrTime);
if (digitalRead(DoorSwitchPin) == LOW) {
DoorinError = 1;
Spark.publish("GarageDoorTxt","Error, Can't Open Door!!",60,PRIVATE);
}
else {
DoorinError = 0;
Spark.publish("GarageDoor","Confirmed Door Open",60,PRIVATE);
}
}
}
void GDCommands(const char *event, const char *data){
DoorOpen = digitalRead(DoorSwitchPin); //Read DoorSwitchPin and Set Var
if (strcmp(data,"Open")==0){
if (DoorOpen == 0) {OpenDoor = 1;}
else {Spark.publish("GarageDoor","Already Open?, Can't Open Again",60,PRIVATE);}
}
else if (strcmp(data,"Close")==0){
if (DoorOpen == 1) {CloseDoor = 1;}
else {Spark.publish("GarageDoor","Already Closed?, Can't Close Again",60,PRIVATE);}
}
else if (strcmp(data,"Status")==0){
Motion = ((LastPRIEvent / 1000) / 60);
if (DoorOpen == 1) {Spark.publish("GarageDoorTxt","Status: IS OPEN Motion Time:" + String (Motion),60,PRIVATE);}
if (DoorOpen == 0) {Spark.publish("GarageDoorTxt","Status: IS CLOSED Motion Time:" + String (Motion),60,PRIVATE);}
}
}