/
garagedoor.ino
63 lines (54 loc) · 1.51 KB
/
garagedoor.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
int openDoorButton = D0;
int doorSensor = D2;
bool state = 0;
/*
The value property of CurrentDoorState must be one of the following:
Characteristic.CurrentDoorState.OPEN = 0;
Characteristic.CurrentDoorState.CLOSED = 1;
Characteristic.CurrentDoorState.OPENING = 2;
Characteristic.CurrentDoorState.CLOSING = 3;
Characteristic.CurrentDoorState.STOPPED = 4;
*/
void setup() {
pinMode(doorSensor, INPUT_PULLUP);
pinMode(openDoorButton, OUTPUT);
digitalWrite(openDoorButton, LOW);
/* Register Particle functions */
Particle.function("onoff", onoff);
Particle.function("doorState", doorState);
}
void loop() {
// LOW = closed, HIGH = open
state = digitalRead(doorSensor);
delay(1000);
}
void pushDoorButton() {
Particle.publish("pushDoorButton", "true");
digitalWrite(openDoorButton, HIGH);
delay(500);
digitalWrite(openDoorButton, LOW);
}
int onoff(String command) {
/* Particle.functions always take a string as an argument and return an integer.*/
if (command == "state=0") {
pushDoorButton();
return 0;
}
else if (command == "state=1") {
pushDoorButton();
return 1;
}
else {
return -1;
}
}
int doorState(String command) {
/* Particle.functions always take a string as an argument and return an integer.*/
if (state == LOW) {
Particle.publish("getDoorState", "closed");
return 1;
} else {
Particle.publish("getDoorState", "open");
return 0;
}
}