-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ino
More file actions
244 lines (152 loc) · 4.3 KB
/
test.ino
File metadata and controls
244 lines (152 loc) · 4.3 KB
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
#include <ESP8266WiFi.h>
#include <Firebase_ESP_Client.h>
#include <addons/TokenHelper.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h>
#include <Keypad.h>
#include <SPI.h>
#include <MFRC522.h>
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 60000);
#define SS_PIN 2
#define RST_PIN 0
#define WIFI_SSID "Nijo"
#define WIFI_PASSWORD "123456789"
#define API_KEY "AIzaSyD9QSTkTQNcMGfni3NpnidQNYBEVaiZjxs"
#define FIREBASE_PROJECT_ID "lift-access-control"
#define USER_EMAIL "navaf_18@gmail.com"
#define USER_PASSWORD "11111111"
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
const byte n_rows = 1;
const byte n_cols = 3;
char keys[n_rows][n_cols] = {
{'1','2','3'}
};
byte colPins[n_cols] = {5 ,4 ,10};
byte rowPins[n_rows] = {16};
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, n_rows, n_cols);
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
char myKey = NO_KEY;
byte nuidPICC[4];
int temp_buf = 0;
int user_1 = 111,user_2 = 104, user_3 = 198;
int keyValue;
int current_lift_position = 0;
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
}
temp_buf = buffer[bufferSize - 2];
if(temp_buf == user_1)
{
Serial.println("Granted");
}
else if(temp_buf == user_2)
{
Serial.println("Granted");
}
else if(temp_buf == user_3)
{
Serial.println("Granted");
}
else
{
Serial.println("Denied");
}
}
void setup() {
Serial.begin(115200);
SPI.begin();
rfid.PCD_Init();
timeClient.begin();
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
config.api_key = API_KEY;
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
config.token_status_callback = tokenStatusCallback;
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
}
void loop() {
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
return;
}
printDec(rfid.uid.uidByte, rfid.uid.size);
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
if (temp_buf == 111 || temp_buf == 104 || temp_buf == 198)
{
while (myKey == NO_KEY){
myKey = myKeypad.getKey();
delay(10);
}
Serial.println(myKey);
}
if (temp_buf == 111)
{
String Path = "users/joelblesson2003@gmail.com";
update_db(myKey , Path);
}
if (temp_buf == 104)
{
String Path = "users/ganeshjohn253@gmail.com";
update_db(myKey , Path);
}
if (temp_buf == 198)
{
String Path = "users/navaf_18@gmail.com";
update_db(myKey , Path);
}
}
int update_db(char key , String documentPath)
{
String current_lift_path = "current_lift/current_lift_position";
if (key >= '0' && key <= '9') {
keyValue = key - '0';
}
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
setTime(epochTime);
char time_buffer[25];
sprintf(time_buffer, "%04d-%02d-%02d %02d:%02d:%02d", year(), month(), day(), hour(), minute(), second());
//Serial.println(time_buffer);
int floorNumber = 120;
FirebaseJson content;
FirebaseJson arrayEntry;
arrayEntry.set("mapValue/fields/Traveled_to/integerValue", keyValue);
arrayEntry.set("mapValue/fields/timestamp/stringValue", time_buffer);
content.set("fields/liftUsage/arrayValue/values", arrayEntry);
FirebaseJson liftPositionContent;
FirebaseJson liftPositionTime;
liftPositionContent.set("fields/lift_position/integerValue", keyValue);
liftPositionContent.set("fields/timestamp/stringValue", time_buffer);
Firebase.Firestore.patchDocument(&fbdo, FIREBASE_PROJECT_ID, "", current_lift_path, liftPositionContent.raw(), "");
Firebase.Firestore.patchDocument(&fbdo, FIREBASE_PROJECT_ID, "", documentPath.c_str(), content.raw(), "liftUsage");
temp_buf = 0;
myKey = NO_KEY;
delay(100);
return 0;
}