-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSouceCode.ion
More file actions
632 lines (521 loc) · 15.6 KB
/
Copy pathSouceCode.ion
File metadata and controls
632 lines (521 loc) · 15.6 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <ESP32Servo.h>
// WiFi credentials
const char* ssid = "HotTab";
const char* password = "haha123ha";
// MQTT Broker settings - HiveMQ Cloud
const char* mqtt_server = "6efaf3dbba2d4829a5f3864174ae1a59.s1.eu.hivemq.cloud";
const int mqtt_port = 8883;
const char* mqtt_user = "MyDoorBro";
const char* mqtt_password = "MyDoorKnows$22";
// Device ID
const char* deviceId = "smartlock001";
// MQTT Topics
String telemetryTopic = "smartlock/telemetry/" + String(deviceId);
String commandTopic = "smartlock/cmd/" + String(deviceId);
// Pin definitions
#define SERVO_PIN 18
#define LED_RED 25
#define LED_GREEN 26
#define REED_SWITCH_PIN 27
#define SHOCK_SENSOR_PIN 14
// Joystick and verify button pins
#define JOYSTICK_VRX 34
#define JOYSTICK_VRY 35
#define VERIFY_BUTTON 33
// Servo
Servo doorLock;
// Continuous rotation servo settings
const int SERVO_STOP = 90;
const int SERVO_CW = 0;
const int SERVO_CCW = 180;
const int ROTATION_TIME = 500;
// State variables
bool isLocked = true;
bool doorClosed = false;
bool tamperDetected = false;
// Reed switch debounce variables
unsigned long lastReedDebounce = 0;
const unsigned long debounceDelay = 50;
int lastReedState = HIGH;
int stableReedState = HIGH;
// Shock sensor variables
volatile unsigned long shockPulseCount = 0;
volatile unsigned long lastShockTime = 0;
unsigned long shockWindowStart = 0;
const unsigned long SHOCK_WINDOW = 2000;
const int SHOCK_THRESHOLD = 3;
unsigned long tamperCooldown = 0;
const unsigned long TAMPER_COOLDOWN_TIME = 5000;
// WiFi and MQTT clients
WiFiClientSecure espClient;
PubSubClient client(espClient);
// Timing for telemetry
unsigned long lastTelemetry = 0;
const unsigned long telemetryInterval = 2000;
// Tamper siren timing
unsigned long lastSirenTime = 0;
const unsigned long SIREN_DURATION = 2000;
const unsigned long SIREN_FLASH_INTERVAL = 100;
// Joystick PIN system
enum Direction { NONE, UP, DOWN, LEFT, RIGHT };
const int JOYSTICK_THRESHOLD_LOW = 500;
const int JOYSTICK_THRESHOLD_HIGH = 3500;
// Debug mode - set to true to see raw joystick values
const bool JOYSTICK_DEBUG = false;
unsigned long lastDebugPrint = 0;
const unsigned long DEBUG_INTERVAL = 500; // Print every 500ms
// PIN storage (4 digits)
Direction pinBuffer[4] = {NONE, NONE, NONE, NONE};
int pinBufferIndex = 0;
// Default PIN: UP, DOWN, LEFT, RIGHT
Direction correctPin[4] = {UP, DOWN, LEFT, RIGHT};
// Joystick state
Direction lastDirection = NONE;
bool joystickReturned = true;
// PIN input timeout
unsigned long lastPinInput = 0;
const unsigned long PIN_TIMEOUT = 10000; // 10 seconds
// Verify button debounce
unsigned long lastVerifyDebounce = 0;
const unsigned long verifyDebounceDelay = 50;
int lastVerifyState = HIGH;
int stableVerifyState = HIGH;
// Interrupt Service Routine for shock sensor
void IRAM_ATTR shockDetected() {
unsigned long currentTime = millis();
if (currentTime - lastShockTime > 100) {
shockPulseCount++;
lastShockTime = currentTime;
}
}
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("\n=== IoT Smart Door Lock Starting ===");
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(REED_SWITCH_PIN, INPUT_PULLUP);
pinMode(SHOCK_SENSOR_PIN, INPUT_PULLUP);
// Joystick and verify button setup
pinMode(JOYSTICK_VRX, INPUT);
pinMode(JOYSTICK_VRY, INPUT);
pinMode(VERIFY_BUTTON, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(SHOCK_SENSOR_PIN), shockDetected, FALLING);
shockWindowStart = millis();
doorLock.attach(SERVO_PIN);
doorLock.write(SERVO_STOP);
isLocked = false;
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
Serial.println("🔓 Initial state: UNLOCKED");
setup_wifi();
espClient.setInsecure();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(mqttCallback);
Serial.println("🕹️ Joystick PIN system ready");
Serial.println(" Default PIN: UP, DOWN, LEFT, RIGHT");
}
void setup_wifi() {
Serial.println("\nConnecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void mqttCallback(char* topic, byte* payload, unsigned int length) {
String message = "";
for (int i = 0; i < length; i++) message += (char)payload[i];
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]: ");
Serial.println(message);
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, message);
if (error) {
Serial.print("JSON parsing failed: ");
Serial.println(error.c_str());
return;
}
if (doc.containsKey("lock")) {
bool shouldLock = doc["lock"];
if (shouldLock && !isLocked) {
if (doorClosed) {
lockDoor();
Serial.println("Door locked via MQTT command");
} else {
Serial.println("⚠️ Cannot lock - door is OPEN!");
showLockFailFeedback();
}
} else if (!shouldLock && isLocked) {
unlockDoor();
Serial.println("Door unlocked via MQTT command");
}
}
if (doc.containsKey("resetTamper")) {
if (doc["resetTamper"] == true) {
tamperDetected = false;
shockPulseCount = 0;
Serial.println("✓ Tamper manually reset via MQTT");
publishTelemetry();
}
}
// Change PIN via MQTT: {"setPin": ["UP", "LEFT", "DOWN", "RIGHT"]}
if (doc.containsKey("setPin")) {
JsonArray newPin = doc["setPin"].as<JsonArray>();
if (newPin.size() == 4) {
for (int i = 0; i < 4; i++) {
String dir = newPin[i].as<String>();
dir.toUpperCase();
if (dir == "UP") correctPin[i] = UP;
else if (dir == "DOWN") correctPin[i] = DOWN;
else if (dir == "LEFT") correctPin[i] = LEFT;
else if (dir == "RIGHT") correctPin[i] = RIGHT;
}
Serial.println("✓ PIN updated via MQTT");
printCurrentPin();
}
}
}
void printCurrentPin() {
Serial.print(" Current PIN: ");
for (int i = 0; i < 4; i++) {
Serial.print(directionToString(correctPin[i]));
if (i < 3) Serial.print(", ");
}
Serial.println();
}
String directionToString(Direction dir) {
switch (dir) {
case UP: return "UP";
case DOWN: return "DOWN";
case LEFT: return "LEFT";
case RIGHT: return "RIGHT";
default: return "NONE";
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP32Client-" + String(deviceId);
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
Serial.println("connected!");
client.subscribe(commandTopic.c_str());
Serial.print("Subscribed to: ");
Serial.println(commandTopic);
publishTelemetry();
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" - trying again in 5s");
delay(5000);
}
}
}
void lockDoor() {
doorLock.write(SERVO_CW);
delay(ROTATION_TIME);
doorLock.write(SERVO_STOP);
isLocked = true;
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
Serial.println("🔒 DOOR LOCKED");
publishTelemetry();
}
void unlockDoor() {
doorLock.write(SERVO_CCW);
delay(ROTATION_TIME);
doorLock.write(SERVO_STOP);
isLocked = false;
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
Serial.println("🔓 DOOR UNLOCKED");
publishTelemetry();
}
void showLockFailFeedback() {
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
int halfRotation = ROTATION_TIME / 2;
doorLock.write(SERVO_CW);
delay(halfRotation);
doorLock.write(SERVO_STOP);
delay(300);
doorLock.write(SERVO_CCW);
delay(halfRotation);
doorLock.write(SERVO_STOP);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
Serial.println("↩️ Lock attempt failed - door is open (servo returned)");
publishLockFailedTelemetry();
}
void publishLockFailedTelemetry() {
StaticJsonDocument<256> doc;
doc["lockStatus"] = "unlocked";
doc["doorStatus"] = "open";
doc["tamper"] = tamperDetected;
doc["shockCount"] = shockPulseCount;
doc["timestamp"] = millis();
doc["lockFailed"] = "door_open";
String output;
serializeJson(doc, output);
if (client.publish(telemetryTopic.c_str(), output.c_str())) {
Serial.print("📡 Published (lock failed): ");
Serial.println(output);
} else {
Serial.println("❌ Publish failed");
}
}
void runTamperSiren() {
Serial.println("🚨 Tamper siren activated!");
unsigned long sirenStart = millis();
bool redOn = true;
while (millis() - sirenStart < SIREN_DURATION) {
if (redOn) {
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
} else {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
}
redOn = !redOn;
delay(SIREN_FLASH_INTERVAL);
client.loop();
}
restoreLedState();
lastSirenTime = millis();
}
void runIncorrectPinSiren() {
Serial.println("🚨 Incorrect PIN siren!");
unsigned long sirenStart = millis();
bool redOn = true;
while (millis() - sirenStart < SIREN_DURATION) {
if (redOn) {
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
} else {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
}
redOn = !redOn;
delay(SIREN_FLASH_INTERVAL);
client.loop();
}
restoreLedState();
}
void restoreLedState() {
if (isLocked) {
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
} else {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
}
}
void handleTamperSiren() {
if (tamperDetected) {
runTamperSiren();
}
}
// ============ JOYSTICK PIN SYSTEM ============
Direction readJoystickDirection() {
int vrx = analogRead(JOYSTICK_VRX);
int vry = analogRead(JOYSTICK_VRY);
// Debug output to find your joystick's actual values
if (JOYSTICK_DEBUG && (millis() - lastDebugPrint > DEBUG_INTERVAL)) {
Serial.print("🎮 VRx: ");
Serial.print(vrx);
Serial.print(" | VRy: ");
Serial.println(vry);
lastDebugPrint = millis();
}
// Check if joystick is in center position
bool xCenter = (vrx > JOYSTICK_THRESHOLD_LOW && vrx < JOYSTICK_THRESHOLD_HIGH);
bool yCenter = (vry > JOYSTICK_THRESHOLD_LOW && vry < JOYSTICK_THRESHOLD_HIGH);
if (xCenter && yCenter) {
return NONE;
}
// Determine direction based on which axis is outside center
if (vry <= JOYSTICK_THRESHOLD_LOW) return UP;
if (vry >= JOYSTICK_THRESHOLD_HIGH) return DOWN;
if (vrx <= JOYSTICK_THRESHOLD_LOW) return LEFT;
if (vrx >= JOYSTICK_THRESHOLD_HIGH) return RIGHT;
return NONE;
}
void handleJoystickInput() {
Direction currentDir = readJoystickDirection();
// Check for PIN timeout
if (pinBufferIndex > 0 && (millis() - lastPinInput > PIN_TIMEOUT)) {
Serial.println("⏱️ PIN input timeout - buffer cleared");
clearPinBuffer();
}
// Joystick returned to center
if (currentDir == NONE) {
joystickReturned = true;
return;
}
// Register new direction only if joystick returned to center first
if (joystickReturned && currentDir != NONE) {
joystickReturned = false;
lastPinInput = millis();
// Shift buffer if full (keep last 4 inputs)
if (pinBufferIndex >= 4) {
for (int i = 0; i < 3; i++) {
pinBuffer[i] = pinBuffer[i + 1];
}
pinBufferIndex = 3;
}
pinBuffer[pinBufferIndex] = currentDir;
pinBufferIndex++;
Serial.print("🕹️ Input registered: ");
Serial.print(directionToString(currentDir));
Serial.print(" [");
for (int i = 0; i < pinBufferIndex; i++) {
Serial.print(directionToString(pinBuffer[i]));
if (i < pinBufferIndex - 1) Serial.print(", ");
}
Serial.println("]");
}
}
void handleVerifyButton() {
int reading = digitalRead(VERIFY_BUTTON);
if (reading != lastVerifyState) {
lastVerifyDebounce = millis();
}
if ((millis() - lastVerifyDebounce) > verifyDebounceDelay) {
if (reading != stableVerifyState) {
stableVerifyState = reading;
// Button pressed (LOW because of INPUT_PULLUP)
if (stableVerifyState == LOW) {
verifyPin();
}
}
}
lastVerifyState = reading;
}
void verifyPin() {
Serial.println("🔐 Verifying PIN...");
// Need exactly 4 inputs
if (pinBufferIndex < 4) {
Serial.println("❌ Not enough digits entered (need 4)");
runIncorrectPinSiren();
clearPinBuffer();
return;
}
// Compare with correct PIN
bool correct = true;
for (int i = 0; i < 4; i++) {
if (pinBuffer[i] != correctPin[i]) {
correct = false;
break;
}
}
if (correct) {
Serial.println("✅ PIN CORRECT!");
if (isLocked) {
unlockDoor();
Serial.println("🔓 Door unlocked via joystick PIN");
} else {
Serial.println("ℹ️ Door already unlocked");
}
} else {
Serial.println("❌ PIN INCORRECT!");
runIncorrectPinSiren();
}
clearPinBuffer();
}
void clearPinBuffer() {
for (int i = 0; i < 4; i++) {
pinBuffer[i] = NONE;
}
pinBufferIndex = 0;
}
// ============ SENSORS ============
void readReedSwitch() {
int reedReading = digitalRead(REED_SWITCH_PIN);
if (reedReading != lastReedState) lastReedDebounce = millis();
if ((millis() - lastReedDebounce) > debounceDelay) {
if (reedReading != stableReedState) {
stableReedState = reedReading;
if (stableReedState == LOW && !doorClosed) {
doorClosed = true;
Serial.println("🚪 Door CLOSED");
lockDoor();
publishTelemetry();
} else if (stableReedState == HIGH && doorClosed) {
doorClosed = false;
Serial.println("🚪 Door OPEN");
if (isLocked) {
Serial.println("⚠️ Door opened while locked - unlocking!");
unlockDoor();
} else {
publishTelemetry();
}
}
}
}
lastReedState = reedReading;
}
void checkShockSensor() {
unsigned long currentTime = millis();
if (currentTime - shockWindowStart > SHOCK_WINDOW) {
if (!tamperDetected && shockPulseCount > 0) {
Serial.print("ℹ️ Shock count reset (below threshold): ");
Serial.println(shockPulseCount);
}
shockPulseCount = 0;
shockWindowStart = currentTime;
}
if (shockPulseCount >= SHOCK_THRESHOLD && !tamperDetected) {
tamperDetected = true;
tamperCooldown = currentTime;
Serial.print("⚠️ TAMPER DETECTED! (");
Serial.print(shockPulseCount);
Serial.println(" shocks detected)");
publishTelemetry();
}
if (tamperDetected && (currentTime - tamperCooldown > TAMPER_COOLDOWN_TIME)) {
if (currentTime - lastShockTime > TAMPER_COOLDOWN_TIME) {
tamperDetected = false;
shockPulseCount = 0;
Serial.println("✓ Tamper auto-reset (no activity detected)");
publishTelemetry();
}
}
}
void publishTelemetry() {
StaticJsonDocument<256> doc;
doc["lockStatus"] = isLocked ? "locked" : "unlocked";
doc["doorStatus"] = doorClosed ? "closed" : "open";
doc["tamper"] = tamperDetected;
doc["shockCount"] = shockPulseCount;
doc["timestamp"] = millis();
String output;
serializeJson(doc, output);
if (client.publish(telemetryTopic.c_str(), output.c_str())) {
Serial.print("📡 Published: ");
Serial.println(output);
} else {
Serial.println("❌ Publish failed");
}
}
void loop() {
if (!client.connected()) reconnect();
client.loop();
readReedSwitch();
checkShockSensor();
handleTamperSiren();
// Joystick PIN system
handleJoystickInput();
handleVerifyButton();
if (millis() - lastTelemetry > telemetryInterval) {
publishTelemetry();
lastTelemetry = millis();
}
}