-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP32_State_Machine.ino
309 lines (284 loc) · 7.57 KB
/
ESP32_State_Machine.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
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
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <MFRC522.h>
#include <Wire.h>
/*Pinout
zone1 -D34
RC522 - SDA -- D16
RC522 - SCK -- D18
RC522 - MOSI - D23
RC522 - MISO - D19
RC522 - IRQ - Not connected
RC522 - GND - G
RC522 - RST -- D4
RC522 - 3.3v - V
NOKIA 5110 - RST -- D25
NOKIA 5110 - CE -- D26
NOKIA 5110 - DC -- D27
NOKIA 5110 - DIN -- MOSI -- D13
NOKIA 5110 - CLK -- D14
NOKIA 5110 - VCC -- V
NOKIA 5110 - LIGHT -- Not connected
NOKIA 5110 - GND -- G
*/
#define SS_PIN 16
#define RST_PIN 4
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
bool uid = 0;
bool armed = 0;
bool zone1 = 0;
bool zone2 = 0;
bool zone3 = 0;
bool toneBuzzer = 0;
bool siren = 0;
unsigned long startMillis;
unsigned long currentMillis;
unsigned long timerValue;
bool latch;
const unsigned long exitDelay = 15000; // Exit delay
const unsigned long disableDelay = 10000; // Entry delay
const unsigned long alarmDelay = 240000; // Stop siren after delay
enum {systemReady, systemExit, systemArmed, systemDelayed, systemIntrusion}; // system states
unsigned char systemState;
String State;
/* Declare LCD object for SPI
Adafruit_PCD8544(CLK,DIN,D/C,CE,RST); */
Adafruit_PCD8544 display = Adafruit_PCD8544(14, 13, 27, 26, 25);
int contrastValue = 60; // Default Contrast Value
const int adcPin = 34;
int adcValue = 0;
void setup()
{
pinMode(2, OUTPUT);
pinMode(34, INPUT);
Serial.begin(115200);
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
/* Initialize the Display*/
display.begin();
/* Change the contrast using the following API*/
display.setContrast(contrastValue);
/* Clear the buffer */
display.clearDisplay();
display.display();
delay(1000);
/* Now let us display some text */
display.setTextColor(WHITE, BLACK);
display.setCursor(0,1);
display.setTextSize(2);
display.println("|ESP32|");
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(22,20);
display.println("|Nokia|");
display.setCursor(22,32);
display.println("|5110|");
display.display();
delay(2000);
}
void zones(){
int frontDoor = digitalRead(34);
if (frontDoor== 1){
zone1 = 1;
}
else if (frontDoor==0) {
zone1 = 0;
}
display.clearDisplay(); display.setCursor(0,1);
display.println(State);
display.print("Zone 1: "); display.println(zone1);
display.print("Alarme: "); display.println(siren);
display.print("Buzzer: "); display.println(toneBuzzer);
display.print("Armed: "); display.println(armed);
display.print("Timer "); display.println(timerValue);
display.display();
}
void TCA9548A(uint8_t bus)
{
Wire.beginTransmission(0x70); // TCA9548A address is 0x70
Wire.write(1 << bus); // send byte to select bus
Wire.endTransmission();
//Serial.println("TCA9548A");
}
void rfid() {
TCA9548A(0);
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
content.toUpperCase();
Serial.print("Identification... "); Serial.println(content); // Open serial monitor to READ the UID
display.clearDisplay(); display.setCursor(0,4); display.println("UID..."); display.println(); display.println("APPROVED"); display.display();
if (content.substring(1) == "56 AC FA 1F") //Main access
{ uid=1;}
if (content.substring(1) == "C4 A6 23 D5") //change here the UID of the card/cards that you want to give access
{ uid=1;}
if (content.substring(1) == "7B 52 24 D5") //change here the UID of the card/cards that you want to give access
{ uid=1;}
else { }
if (uid == 1) {
Serial.println("Carte lue");
Serial.print("armed: "); Serial.println(armed);
if (armed == 1 ){
armed = 0;
Serial.print("armed = 0");
delay(2000);
}
else if (armed == 0){
armed = 1;
Serial.print("armed = 1");
delay(2000);
}
uid=0;
}
}
void timer(int state) {
if (state==0){
currentMillis = millis();
startMillis = millis();
timerValue = 0;
}
if (state==1){
if (latch==0){
startMillis = millis();
latch = 1;
}
currentMillis = millis();
timerValue = currentMillis - startMillis;
}
}
void loop()
{
switch (systemState) {
case systemReady: // Waiting
State = "SystemReady";
Serial.println(State); //delay(1000);
zones();
rfid();
timer(0); // Reset timer
toneBuzzer = 0; siren = 0;
if ((armed==1) && (zone1==1)){
systemState = systemExit;
break;
}
if ((armed==1) && (zone1==0)) {
toneBuzzer = 1; siren = 0;
display.clearDisplay(); display.setCursor(0,1); display.println("Zone ouverte"); display.display();
delay(2000);
break;
}
else {
break;
}
case systemExit: // Exit delay
State = "SystemExit";
Serial.println(State); //delay (1000);
rfid();
zones();
timer(1); // Start timer
Serial.println("debug0");
if (armed==0) {
systemState = systemReady; Serial.println("debug2");
break;
}
if (timerValue<exitDelay) {
toneBuzzer=1; siren=0; Serial.println("debug1");
}
if ((armed==1) && (timerValue>=exitDelay)){
systemState = systemArmed;
break;
}
else {
break;
}
case systemArmed: // Armed
State = "SystemArmed";
Serial.println(State); //delay (1000);
zones();
timer(0); // Reset timer
rfid();
toneBuzzer=0; siren=0;
if ((armed==1) && (zone1==0)) { //Main door is opened
//wifiMessage(1); //zone 1
systemState = systemDelayed; Serial.println("Main door is opened");
break;
}
/*
if ((armed==1) && (zone2==0)) { //patio door is opened
wifiMessage(2); //zone 2
systemState = systemIntrusion;
break;
}
if((armed==1) && (zone3==0)) { //IR sensor detection
wifiMessage(3); //zone 3
systemState = systemIntrusion;
break;
}
*/
else {
break;
}
case systemDelayed: // Front door intrusion delay
State = "SystemDelayed";
Serial.println(State); //delay (1000);
zones();
timer(1); // Start timer
rfid();
if (armed==0) {
systemState = systemReady;
break;
}
if (timerValue<disableDelay) {
toneBuzzer=1; siren=0;
}
if ((armed==1) && (timerValue>=disableDelay)){
timer(0); // Reset timer
systemState = systemIntrusion;
break;
}
else {
break;
}
case systemIntrusion: // Siren activated
State = "Intrusion";
Serial.println(State); //delay (1000);
zones();
timer(1); // Start timer
rfid();
toneBuzzer=1; siren=1;
// wifiMessage(4) //Siren activated
if ((armed==1) && (timerValue<alarmDelay)){
break;
}
else if ((armed==1) && (timerValue>=alarmDelay)) {
systemState = systemArmed; // stop siren after intrusion delay
}
else if (armed==0) {
systemState = systemReady;
break;
}
else {
break;
}
default:
systemState = systemReady;
break;
}
}