-
Notifications
You must be signed in to change notification settings - Fork 317
/
_P129_RC522_RFID.ino
232 lines (193 loc) · 6.78 KB
/
_P129_RC522_RFID.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
#ifdef USES_P129
//#######################################################################################################
//################################ Plugin-214: RC522 SPI RFID reader ####################################
//#######################################################################################################
#define PLUGIN_129
#define PLUGIN_ID_129 129
#define PLUGIN_NAME_129 "RFID - RC522 SPI"
#define PLUGIN_VALUENAME1_129 "Tag"
// #define PLUGIN_129_CS 16 //needed?
#include <SPI.h>
#include <MFRC522.h>
MFRC522 mfrc522;
uint8_t Plugin_129_packetbuffer[64];
uint8_t Plugin_129_command;
boolean Plugin_129(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_129;
Device[deviceCount].Type = DEVICE_TYPE_SINGLE;
Device[deviceCount].VType = SENSOR_TYPE_LONG;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = false;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_129);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_129));
break;
}
case PLUGIN_GET_DEVICEGPIONAMES: //define 'GPIO 1st' name in webserver
{
event->String1 = formatGpioName_output(F("CS PIN"));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
// cant compile addFormPinSelect(string, F("Reset Pin"), F("taskdevicepin3"), Settings.TaskDevicePin3[event->TaskIndex]);
addFormPinSelect(F("Reset Pin"), F("taskdevicepin3"), Settings.TaskDevicePin3[event->TaskIndex]);
success = true;
break;
}
case PLUGIN_INIT:
{
for(byte x=0; x < 3; x++)
{
String log = F("MFRC522: Init");
addLog(LOG_LEVEL_INFO, log);
if(Plugin_129_Init(Settings.TaskDevicePin1[event->TaskIndex],Settings.TaskDevicePin3[event->TaskIndex]))
break;
delay(1000);
}
break;
}
case PLUGIN_TEN_PER_SECOND:
{
static unsigned long tempcounter = 0;
static byte counter;
static byte errorCount=0;
counter++;
if (counter == 3)
{
counter = 0;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
uint8_t uidLength;
// byte error = Plugin_129_readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
byte error = Plugin_129_readPassiveTargetID(uid, &uidLength); // not defined if plugin P017_PN532 is not selected!
if (error == 1)
{
errorCount++;
String log = F("MFRC522: Read error: ");
log += errorCount;
addLog(LOG_LEVEL_ERROR, log);
}
else
errorCount=0;
if (errorCount > 2) // if three consecutive I2C errors, reset PN532
{
Plugin_129_Init(Settings.TaskDevicePin1[event->TaskIndex],Settings.TaskDevicePin3[event->TaskIndex]);
}
if (error == 0) {
unsigned long key = uid[0];
for (uint8_t i = 1; i < 4; i++) {
key <<= 8;
key += uid[i];
}
UserVar[event->BaseVarIndex] = (key & 0xFFFF);
UserVar[event->BaseVarIndex + 1] = ((key >> 16) & 0xFFFF);
String log = F("MFRC522: Tag: ");
log += key;
tempcounter++;
log += " ";
log += tempcounter;
addLog(LOG_LEVEL_INFO, log);
sendData(event);
}
}
break;
}
}
return success;
}
/*********************************************************************************************\
* MFRC522 init
\*********************************************************************************************/
boolean Plugin_129_Init(int8_t csPin, int8_t resetPin)
{
if (resetPin != -1)
{
String log = F("MFRC522: Reset on pin: ");
log += resetPin;
addLog(LOG_LEVEL_INFO, log);
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, LOW);
delay(100);
digitalWrite(resetPin, HIGH);
pinMode(resetPin, INPUT_PULLUP);
delay(10);
}
pinMode(csPin, OUTPUT);
digitalWrite(csPin, LOW);
mfrc522.PCD_Init(csPin,resetPin); // Init MFRC522 module
//If you set Antenna Gain to Max it will increase reading distance
mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
bool result = mfrc522.PCD_PerformSelfTest(); // perform the test
if (result) {
//String log = F("RC522: Found");
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
String log=F("MFRC522: Communication failure, is the MFRC522 properly connected?");
addLog(LOG_LEVEL_ERROR,log);
return false;
}
else
{
String log=F("MFRC522: Software Version: ");
if (v == 0x91)
log+=F(" = v1.0");
else if (v == 0x92)
log+=F(" = v2.0");
else
log+=F(" (unknown),probably a chinese clone?");
addLog(LOG_LEVEL_INFO, log);
}
}
else
return false;
return true;
}
/*********************************************************************************************\
* RC522 read tag ID
\*********************************************************************************************/
//byte Plugin_129_readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength)
byte Plugin_129_readPassiveTargetID(uint8_t *uid, uint8_t *uidLength) //needed ? see above (not PN532)
{
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return 2;
}
String log = F("MFRC522: New Card Detected");
addLog(LOG_LEVEL_INFO,log);
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return 1;
}
// There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
// I think we should assume every PICC as they have 4 byte UID
// Until we support 7 byte PICCs
log =F("MFRC522: Scanned PICC's UID");
addLog(LOG_LEVEL_INFO,log);
for ( uint8_t i = 0; i < 4; i++) { //
uid[i] = mfrc522.uid.uidByte[i];
//Serial.print(readCard[i], HEX);
}
*uidLength = 4;
mfrc522.PICC_HaltA(); // Stop reading
return 0;
}
#endif // USES_P129