-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblynkcalls.ino
277 lines (225 loc) · 8.45 KB
/
blynkcalls.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
NFC Door lock
Auther Michael Burton
Creative Commons License
NFC Door lock
by Michael Dale Burton
is licensed under a Creative Commons Attribution-NonCommercial
4.0 International License.
*/
BLYNK_CONNECTED() {
rtc.begin(); // Synchronize time on connection
bridge_master.setAuthToken(reader_token); // Token of reader
}
BLYNK_WRITE(vcount) {
openspiffs();
rowIndex = read16data(DATASTART); //read the card count
closespiffs();
}
BLYNK_READ(vcount)
{
Blynk.virtualWrite(vcount, rowIndex); // write the rowIndex to the server
// openspiffs();
// write16data(DATASTART, rowIndex); //save the card count in fram at offset 0
// closespiffs();
}
BLYNK_WRITE(flagpin)
{
accessFlags[0] = param.asInt();
}
BLYNK_READ(flagpin)
{
Blynk.virtualWrite(flagpin, accessFlags[0]); //update the flag display on the app
}
BLYNK_WRITE(updatepin) {
if (param.asInt()) {
int tempid = findUid(cardId[0]);
if (!tempid) { // if no card has this id then see if we can add a card
if (rowIndex < maxcards) { // if the maximum number of cards has not been stored then add a card
storeCard(++rowIndex, cardId[0], cardHolder[0], accessFlags[0]); //copy temp data into card memory
}
} else storeCard(tempid, cardId[0], cardHolder[0], accessFlags[0]); //replace the data
}
}
// called when the name nameDisplay has had input
BLYNK_WRITE(namedisplay)
{
String textIn = param.asStr(); // Text Input Widget - Strings
cardHolder[0] = textIn; //get the Text Input Widget - String
}
BLYNK_READ(namedisplay)
{
Blynk.virtualWrite(namedisplay, cardHolder[0]); //update the name display on the app
}
BLYNK_WRITE(cardiddisplay)
{
String textIn = param.asStr(); // Text Input Widget - Strings
cardId[0] = textIn; //set the temp data for the card ID
}
/*
cycle through the cards
*/
BLYNK_WRITE(indexdisplay) {
cardIndex = param.asInt();
if (cardIndex > rowIndex)
cardIndex = rowIndex;
getCard(cardIndex);
Blynk.virtualWrite(namedisplay, cardHolder[0]);
Blynk.virtualWrite(cardiddisplay, cardId[0]);
Blynk.virtualWrite(flagpin, accessFlags[0]);
Blynk.virtualWrite(indexdisplay, cardIndex);
}
BLYNK_WRITE(terminaldisplay)
{
dumpdatafromfile();
}
/*
this will copy data to the device with the id sent as param[1]
param[0] determines if we copy all data or just 1 card
*/
BLYNK_WRITE(bridgewrite)
{
String stoken = param[1].asStr(); // get the lock token string
char token[34];
byte tokenbytes[34];
stoken.getBytes(tokenbytes, 34); // copy the token string to a char array
for ( int i = 0; i < 34; i++) {
token[i] = tokenbytes[i];
}
bridge_lock.setAuthToken(token); // set the bridge token to the lock
bridge_lock.virtualWrite(bridgedata, cardHolder[0], cardId[0], accessFlags[0]); //copy current card from reader
}
/*
this will copy data to the device with the id sent as param[1]
param[0] determines if we copy all data or just 1 card
*/
BLYNK_WRITE(bridgewriteall)
{
String stoken = param[1].asStr(); // get the lock token string
char token[34];
byte tokenbytes[34];
stoken.getBytes(tokenbytes, 34); // copy the token string to a char array
for ( int i = 0; i < 34; i++) {
token[i] = tokenbytes[i];
}
bridge_lock.setAuthToken(token); // set the bridge token to the lock
for (int i = 1; i <= rowIndex; i++) {
bridge_lock.virtualWrite(bridgedata, cardHolder[i], cardId[i], accessFlags[i]); //copy all cards from reader
yield();
}
}
BLYNK_WRITE(copybutton)
{
if (param.asInt()) {
bridge_master.virtualWrite(bridgewrite, 1, lock_token); //tell masterlock to copy current card to current lock
}
}
BLYNK_WRITE(copyallbutton)
{
if (param.asInt()) {
bridge_master.virtualWrite(bridgewriteall, 2, lock_token); //tell masterlock to copy all cards to current lock
}
}
/**************************************
remove a card from valid card list
moves the last card into the place of the removed card
decraments the card count
**************************************/
BLYNK_WRITE(deletebutton) //Remove
{
int b = param.asInt();
if (b) { //only do this on the high state of the button press
if (rowIndex > 1) { // if there is more than one card
if (cardIndex != rowIndex) { // if the current card is not the last card
getCard(rowIndex); // get the last card
storeCard(cardIndex, cardId[0], cardHolder[0], accessFlags[0]); //move the data for the last card to the deleted cards position
} else {
getCard(--cardIndex); //get the previous card and decrament the card index
}
--rowIndex; // decrament the card count
} else { // remove the last card
rowIndex = 0; // set the indexes to 0 and clear the strings
cardIndex = 0;
cardHolder[0] = " ";
cardId[0] = " ";
}
openspiffs();
write16data(DATASTART, rowIndex); //save the card count in fram at offset 0
closespiffs(); //save the card count in fram at offset 0
Blynk.virtualWrite(vcount, rowIndex); // store the card count on server
Blynk.virtualWrite(indexdisplay, cardIndex); // display the card index
}
Blynk.virtualWrite(namedisplay, cardHolder[0]);
Blynk.virtualWrite(cardiddisplay, cardId[0]);
}
BLYNK_WRITE(lockbutton) { //unlock door button pressed
if (param.asInt()) {
unlockDoor();
}
}
BLYNK_WRITE(bridgedata) { // copy card to lock hardware
cardHolder[0] = param[0].asStr();
cardId[0] = param[1].asStr();
accessFlags[0] = param[2].asInt();
terminal.println("holder " + cardHolder[0]);
terminal.println("id " + cardId[0]);
terminal.print("flags " );
terminal.println(accessFlags[0]);
terminal.flush();
int tempid = findUid(cardId[0]);
if (!tempid) {
if (rowIndex < maxcards) {
storeCard(++rowIndex, cardId[0], cardHolder[0], accessFlags[0]); //put card in memory
getCard(cardIndex);
Blynk.virtualWrite(namedisplay, cardHolder[0]);
Blynk.virtualWrite(cardiddisplay, cardId[0]);
}
} else storeCard(tempid, cardId[0], cardHolder[0], accessFlags[0]); //replace the data
}
BLYNK_WRITE(timeinpin) {
TimeInputParam t(param);
if (t.hasStartTime()) // Process start time
{
SThour = String(t.getStartHour());
STmin = String(t.getStartMinute());
}
if (t.hasStopTime()) // Process stop time
{
SPhour = String(t.getStopHour());
SPmin = String(t.getStopMinute());
}
// Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
days[1] = t.isWeekdaySelected(0);
days[2] = t.isWeekdaySelected(1);
days[3] = t.isWeekdaySelected(2);
days[4] = t.isWeekdaySelected(3);
days[5] = t.isWeekdaySelected(4);
days[6] = t.isWeekdaySelected(5);
days[0] = t.isWeekdaySelected(6);
terminal.println(SThour + ":" + STmin);
terminal.println(SPhour + ":" + SPmin);
for (int i = 0; i++; i < 7) {
// if (days[i]) {
terminal.print(String(i) + " ");
// }
terminal.println(" ");
}
terminal.flush();
startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
}
/**************************************
remove all cards from valid card list
**************************************/
BLYNK_WRITE(resetallpin) //reset
{
rowIndex = 0;
cardIndex = 0;
Blynk.virtualWrite(namedisplay, " ");
Blynk.virtualWrite(cardiddisplay, " ");
Blynk.virtualWrite(vcount, rowIndex);
Blynk.virtualWrite(indexdisplay, rowIndex);
openspiffs();
write16data(DATASTART, rowIndex); //save the card count in fram at offset 0
closespiffs();
}