This repository has been archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserraturaYun.ino
132 lines (106 loc) · 2.71 KB
/
serraturaYun.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
#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>
#include <Bridge.h>
//#define DEBUG
#define IRQ (4)
#define RESET (6) // Not connected by default on the NFC Shield
#define PYPATH F("/mnt/sda1/arduino/www/serraturaYun/logger.py")
const int lockEnable = 9,
feedbackLed = 13;
Adafruit_NFCShield_I2C nfc(IRQ, RESET);
uint8_t uidPrev[7];
void setup() {
pinMode(lockEnable, OUTPUT);
pinMode(feedbackLed, OUTPUT);
digitalWrite(lockEnable, HIGH); // make sure the door is locked
digitalWrite(feedbackLed, LOW);
Bridge.begin();
nfc.begin();
#ifdef DEBUG
Serial.begin(115200);
while (!Serial);
Serial.println(F("Officine Arduino door opener"));
#endif
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// configure board to read RFID tags
nfc.SAMConfig();
}
void loop() {
uint8_t successID;
uint8_t uid[] = {
0, 0, 0, 0, 0, 0, 0
}; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
// it's a blocking function
successID = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (successID)
{
#ifdef DEBUG
Serial.println("Read TAG");
Serial.print("ID: ");
//nfc.PrintHex(uid, uidLength);
Serial.println(intArrayToHexString(uid, uidLength));
#endif
Process p;
p.begin("python");
p.addParameter(PYPATH);
p.addParameter(intArrayToHexString(uid, uidLength));
p.run();
#ifdef DEBUG
Serial.print(F("The user is trying to open the door\n"));
#endif
while (p.available()) {
char c = p.read();
#ifdef DEBUG
Serial.print(c);
#endif
if (c == '\n')
break;
else if (c == 'y')
{
digitalWrite(lockEnable, HIGH);
delayAndBlink(2000);
digitalWrite(lockEnable, LOW);
}
else {
digitalWrite(lockEnable, LOW);
}
}
}
// save the previous TAG ID
memcpy(uidPrev, uid, uidLength);
}
boolean compareArray (uint8_t array1[], uint8_t array2[], uint8_t len)
{
for (int i = 0; i < len; i++)
{
if (array1[i] != array2[i])
return false;
}
return true;
}
String intArrayToHexString(uint8_t array[], int length)
{
String output = "";
for (int i = 0; i < length; i++)
output += String(array[i], HEX);
output.toUpperCase();
return output;
}
void delayAndBlink(unsigned long delayTime)
{
int blinkInterval = 100;
unsigned long repetitions = delayTime / blinkInterval;
boolean ledState = LOW;
for (int i = 0; i < repetitions; i++)
{
ledState = !ledState;
digitalWrite(feedbackLed, ledState);
delay(blinkInterval);
}
digitalWrite(feedbackLed, LOW);
}