-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
main.cpp
317 lines (271 loc) · 7.36 KB
/
main.cpp
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
PROJECT NAME: ESP32 Keypad Doorlock With Database Interface
AUTHOR: Donsky
FOR: www.donskytech.com
*/
#include <Arduino.h>
#include <Keypad.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
/**** NEED TO CHANGE THIS ACCORDING TO YOUR SETUP *****/
// The REST API endpoint - Change the IP Address
const char *base_rest_url = "http://<REST-SERVER-IP-ADDRESS>:3000/";
//SSID and Password
const char *ssid = "YOUR_WIFI_SSID";
const char *password = "YOUR_WIFI_PASSWORD";
/**** -- END -- *****/
// Keypad columns and rows
#define ROW_NUM 4 // four rows
#define COLUMN_NUM 4 // four columns
// Servo setup
Servo servoMotor;
static const int servoPin = 26;
// Length of Keycode
const int KEYCODE_SIZE = 6;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};
// GIOP19, GIOP18, GIOP5, GIOP17 for row pins
byte pin_rows[ROW_NUM] = {19, 18, 5, 17};
// GIOP13, GIOP14, GIOP015, GIOP16 for column pins
byte pin_column[COLUMN_NUM] = {13, 14, 15, 16};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
// Length of Keycode + '\0' char
char input_keypad_code[KEYCODE_SIZE + 1];
bool isComplete = false;
byte ctr = 0;
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// Check your I2C LCD Address
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
const int FIRST_LCD_ROW = 0;
const int SECOND_LCD_ROW = 1;
const int LCD_TIME_DELAY = 1000;
void closeLock()
{
Serial.println("Closing lock...");
servoMotor.write(0);
delay(500);
}
void openLock()
{
Serial.println("Opening lock...");
servoMotor.write(45);
// Close the lock after 5 seconds
delay(5000);
closeLock();
}
// Function to print to LCD
void printToLCD(const String &message, uint8_t column, uint8_t row, bool isClear)
{
if (isClear)
{
lcd.clear();
}
// set cursor to column, row
lcd.setCursor(column, row);
if (message.length() == 0)
{
lcd.setCursor(0, 1);
for (int n = 0; n < 16; n++)
{
lcd.print(" ");
}
}
else
{
lcd.print(message);
}
}
// Clear LCD display
void clearLCDLineDisplay(uint8_t row){
lcd.setCursor(0, row);
for (int n = 0; n < 16; n++)
{
lcd.print(" ");
}
}
bool parseJson(String jsonDoc)
{
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, jsonDoc);
if (error)
{
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return false;
}
// JSON return from our API Rest Service
// We are not using everything but only checking the success value if it is true or false
bool success = doc["success"]; // true
// const char *message = doc["message"]; // "Successfully retrieved keycode record"
// JsonObject keyCode = doc["keyCode"];
// const char *keyCode_id = keyCode["_id"]; // "62e89195006f4422e95cc96f"
// const char *keyCode_keyCode = keyCode["keyCode"]; // "BC9876"
// bool keyCode_active = keyCode["active"]; // true
// const char *keyCode_description = keyCode["description"]; // "Anna key code"
// const char *keyCode_createdAt = keyCode["createdAt"]; // "2022-08-02T02:53:09.096Z"
// const char *keyCode_updatedAt = keyCode["updatedAt"]; // "2022-08-02T02:53:09.096Z"
// int keyCode_v = keyCode["__v"]; // 0
// Serial.printf("success :: %d\n", success);
// Serial.printf("message :: %s\n", message);
// Serial.printf("keyCode :: ", keyCode);
return success ? true : false;
}
bool checkKeyCode(String key_code)
{
bool isPresent = false;
char rest_api_url[200];
// Calling our API server
sprintf(rest_api_url, "%sapi/keycodes/%s", base_rest_url, key_code.c_str());
Serial.printf("API URL=%s", rest_api_url);
HTTPClient http;
Serial.print("[HTTP] begin...\n");
http.begin(rest_api_url);
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0)
{
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK)
{
String payload = http.getString();
Serial.println(payload);
isPresent = parseJson(payload);
Serial.printf("isPresent :: %s\n", isPresent ? "Keycode is present!" : "Keycode not found!");
}
}
else
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
return isPresent;
}
void clearDataArray()
{
int index = 0;
while (input_keypad_code[index] != '\0')
{
input_keypad_code[index] = 0;
index++;
}
}
void keypadEvent(KeypadEvent key)
{
switch (keypad.getState())
{
case PRESSED:
if (key == '#')
{
Serial.println("Validating with the API...");
if (isComplete)
{
Serial.println("Calling the API...");
Serial.println(input_keypad_code);
bool isValid = checkKeyCode(input_keypad_code);
if (isValid)
{
printToLCD("Correct keycode!", 0, 1, false);
delay(LCD_TIME_DELAY);
openLock();
}
else
{
printToLCD("Invalid keycode!", 0, 1, false);
delay(LCD_TIME_DELAY);
closeLock();
}
delay(1000);
printToLCD("", 0, 1, false);
ctr = 0;
isComplete = false;
clearDataArray();
}
}
else if (key == '*')
{
clearDataArray();
Serial.print("After clearing...");
Serial.println(input_keypad_code);
printToLCD(input_keypad_code, 5, 1, false);
ctr = 0;
isComplete = false;
}
else
{
if(isComplete){
printToLCD("Max Length Over!", 0, 1, false);
delay(1000);
clearLCDLineDisplay(SECOND_LCD_ROW);
printToLCD(input_keypad_code, 5, 1, false);
return;
}
input_keypad_code[ctr] = key;
Serial.println(input_keypad_code);
printToLCD(input_keypad_code, 5, 1, false);
ctr++;
if (ctr == KEYCODE_SIZE)
{
Serial.println("6 digit keypad entered!");
isComplete = true;
}
}
break;
case RELEASED:
case IDLE:
case HOLD:
break;
}
}
void setup()
{
Serial.begin(115200);
keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
//Adjust the debounce accordingly
keypad.setDebounceTime(50);
// Close lock oif it is open
closeLock();
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on
printToLCD("Initializing...", 1, 0, true);
for (uint8_t t = 4; t > 0; t--)
{
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
printToLCD("Connecting to WIFI...", 2, 0, true);
printToLCD("WIFI...", 6, 1, false);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
printToLCD("Connected!", 2, 0, true);
delay(2000);
printToLCD("Input Keycode:", 0, 0, true);
servoMotor.attach(servoPin);
}
void loop()
{
// Just get the key char entered in our loop
char key = keypad.getKey();
}