-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino_security_control.ino
More file actions
189 lines (158 loc) · 4.24 KB
/
Arduino_security_control.ino
File metadata and controls
189 lines (158 loc) · 4.24 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
#include "thingProperties.h"
#include "buildInRGB.h"
#include "buzzer.h"
#include "led.h"
#include "fileIO.h"
#include "passwordManager.h"
const int ledPin = 0;
const int buzzerPin = 1;
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 5;
unsigned long prevMillis = 0;
const long interval = 100;
const char* pwdFileName = "pwd.txt"; // password file name
const char* logFileName = "log.txt"; // log file name
const int pwdLength = 4;
const int attemptLimit = 3; // the maximum number of attempts to enter the passcode
int attempts = 0;
const long timeOffset = 0; // time offset in seconds
Buzzer buzzer(buzzerPin);
Led led(ledPin);
FileIOHandler fileIOHandler;
PasswordManager pwdManager(timeOffset);
bool inputtingPwd = false;
std::string pwdInput = "";
JsonDocument logDocument;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/* Initialise all sensors and actuators */
pinMode(buttonPin1, INPUT); // passcode button 1
pinMode(buttonPin2, INPUT); // passcode button 2
pinMode(buttonPin3, INPUT); // used to clear passcode input
buzzer.initBuzzer();
led.init();
initBuildInRGB();
pwdManager.init(pwdFileName, logFileName); // import password into the password manager
fileIOHandler.init();
setBuildInRGB(255,0,0);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information youâll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
//buzzer.entryBeeping();
}
void loop() {
ArduinoCloud.update();
//pwdManager.updateNTP();
if(ArduinoCloud.connected())
{
// set RGB to green
setBuildInRGB(0,255,0);
}
else
{
// set RGB to yellow
setBuildInRGB(255,255,0);
}
unsigned long currentMillis = millis();
if(currentMillis - prevMillis >= interval) // setting up the refresh interval
{
prevMillis = currentMillis;
/* All code should be put in this if statement*/
/* sync local password data with cloud data */
user_login_1 = pwdManager.getPassword("user_1").c_str();
user_login_2 = pwdManager.getPassword("user_2").c_str();
if(inputtingPwd)
{
led.on();
inputPassword();
}
else
{
led.off();
}
if(digitalRead(buttonPin3) == HIGH)
{
buzzer.buttonPressed();
inputtingPwd = !inputtingPwd;
}
}
}
/*
Since UserLogin1 is READ_WRITE variable, onUserLogin1Change() is
executed every time a new value is received from IoT Cloud.
*/
void onUserLogin1Change(){
pwdManager.changePassword("user_1", user_login_1.c_str());
}
/*
Since UserLogin2 is READ_WRITE variable, onUserLogin2Change() is
executed every time a new value is received from IoT Cloud.
*/
void onUserLogin2Change(){
pwdManager.changePassword("user_2", user_login_2.c_str());
}
void inputPassword()
{
while((pwdInput.length() < pwdLength) && (inputtingPwd))
{
if(digitalRead(buttonPin1) == HIGH)
{
pwdInput += "0";
buzzer.buttonPressed();
delay(500);
}
if(digitalRead(buttonPin2) == HIGH)
{
pwdInput += "1";
buzzer.buttonPressed();
delay(500);
}
if((digitalRead(buttonPin3) == HIGH)&&(pwdInput.length() > 0))
{
//buzzer.buttonPressed();
inputtingPwd = !inputtingPwd;
}
}
if(pwdManager.verifyPassword(pwdInput.c_str()))
{
attempts = 0;
buzzer.entryBeeping();
pwdInput = "";
inputtingPwd = false;
std::string loginLog = pwdManager.getLatestLoginMsg();
deserializeJson(logDocument, loginLog);
if(logDocument["userName"] == "user_1")
{
user1_log = String(loginLog.c_str());
}
else if(logDocument["userName"] == "user_2")
{
user2_log = String(loginLog.c_str());
}
}
else
{
attempts++;
buzzer.buzzerBeeping(1000, 1000);
pwdInput = "";
inputtingPwd = false;
}
if(attempts >= attemptLimit)
{
attempts = 0;
buzzer.alarm();
}
}