-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoorlock.ino
148 lines (127 loc) · 3.58 KB
/
doorlock.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <Ciao.h>
#define DEBUG
int relay_pin = 13;
bool locked = true;
bool unlocking = false;
int timer = 0;
int max_time = 3000;
String lock_component = String("yunlock1");
String lock_property = String("status");
void setup() {
pinMode(relay_pin, OUTPUT);
Ciao.begin(250000);
#ifdef DEBUG
Serial.begin(57600);
while(!Serial){}
Serial.println("Started Ciao!");
#endif
digitalWrite(relay_pin, HIGH);
delay(3000);
digitalWrite(relay_pin, LOW);
}
void loop() {
#ifdef DEBUG
while(!Serial){}
#endif
CiaoData data;
data = Ciao.read("muzzley");
String command[5];
String io;
String component;
String property;
String value;
String cid;
String delimiter = String("|");
if(!data.isEmpty()){
String id = data.get(0);
String message = data.get(1);
String _cid = data.get(2);
#ifdef DEBUG
Serial.print("\n\n\nReceived a new message:");
Serial.print("\nMessage Id: "); // Ciao message ID
Serial.print(id);
Serial.print("\nMessage: "); // Message (io/Component/Property/Value)
Serial.print(message);
Serial.print("\n_CID: "); // Muzzley cid
Serial.print(_cid);
Serial.print("\n");
#endif
splitString(message, delimiter, command, 4);
io = command[0];
component = command[1];
property = command[2];
value = command[3];
#ifdef DEBUG
//Display the received message info
Serial.print("\nIo:");
Serial.print(command[0]);
Serial.print("\nComponent:");
Serial.print(command[1]);
Serial.print("\nProperty:");
Serial.print(command[2]);
Serial.print("\nValue:");
Serial.print(command[3]);
Serial.print("\n");
#endif
String response_msg;
if (io.equals("r")) {
if (component == lock_component && property == lock_property) {
if(locked){
#ifdef DEBUG
Serial.print("\nLock is locked!");
#endif
response_msg = lock_component + "|" + lock_property + "|" + "False";
}else{
#ifdef DEBUG
Serial.print("\nLock is unlocked!");
#endif
response_msg = lock_component + "|" + lock_property + "|" + "True";
}
Ciao.writeResponse("muzzley", id, response_msg);
#ifdef DEBUG
Serial.print("\nResponse: " + response_msg);
#endif
}
}
if(io.equals("w")){
if (component == lock_component && property == lock_property){
if (value == "False") {
timer=0;
locked = true;
unlocking = false;
response_msg = lock_component + "|" + lock_property + "|" + "False";
digitalWrite(relay_pin, LOW);
#ifdef DEBUG
Serial.print("\nLocked!");
#endif
} else if (value == "True" && unlocking == false){
timer=0;
locked = false;
unlocking = true;
response_msg = lock_component + "|" + lock_property + "|" + "True";
digitalWrite(relay_pin, HIGH);
#ifdef DEBUG
Serial.print("\nUnlocked!");
#endif
}
Ciao.write("muzzley", response_msg);
#ifdef DEBUG
Serial.print("\nUpdate Message sent: " + response_msg);
#endif
}
}
}
// Prevent unlocking for a long time
if (timer >= max_time) {
timer=0;
locked = true;
unlocking = false;
digitalWrite(relay_pin, LOW);
String update_msg = lock_component + "|" + lock_property + "|" + "False";
Ciao.write("muzzley", update_msg);
}
delay(100);
if (unlocking == true) {
timer += 100;
}
}