-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessControl.ino
More file actions
115 lines (95 loc) · 2.71 KB
/
AccessControl.ino
File metadata and controls
115 lines (95 loc) · 2.71 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
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
#include "config.hpp"
#include "const.hpp"
#include "conversion.hpp"
#include "mode.hpp"
WiFiUDP udp;
NTPClient ntp(udp, "europe.pool.ntp.org", 0, interval);
MFRC522 mfrc522(SS_PIN, MFRC522::UNUSED_PIN);
LiquidCrystal_PCF8574 lcd(0x3f);
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Wire.pins(SDA_PIN, SCL_PIN);
lcd.begin(16, 2);
lcd.setBacklight(128);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
connect();
ntp.begin();
}
void loop() {
if ((millis() - lcdStarted) >= 2 * 60 * 1000) {
lcd.clear();
lcdStarted = 0;
}
connect();
ntp.update();
unsigned long currTime = ntp.getEpochTime();
if (!mfrc522.PICC_IsNewCardPresent())
return;
if (!mfrc522.PICC_ReadCardSerial())
return;
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
&& piccType != MFRC522::PICC_TYPE_MIFARE_1K
&& piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("This sample only works with MIFARE Classic cards."));
return;
}
MFRC522::MIFARE_Key key;
for (int i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
const byte bufferSize = 18;
MFRC522::StatusCode status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, TRAILOR, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
byte buffer[bufferSize];
byte bufferSizeCopy = bufferSize;
status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(BLOCK, buffer, &bufferSizeCopy);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
byte cardMode = buffer[0];
if (cardMode != WORK_MODE && cardMode != FREE_MODE) {
unset(currTime, buffer, &mfrc522, &lcd);
}
byte switchMode = getCurrentMode();
switch (switchMode) {
case WORK_MODE: {
work_mode(currTime, buffer, &mfrc522, &lcd);
} break;
case FREE_MODE: {
free_mode(currTime, buffer, &mfrc522, &lcd);
} break;
case SHOW_MODE: {
show_mode(currTime, buffer, &mfrc522, &lcd);
}
}
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
delay(1000);
}
void connect() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Connected!");
}
}