-
Notifications
You must be signed in to change notification settings - Fork 4
/
RFID_Subs.h
155 lines (123 loc) · 5.55 KB
/
RFID_Subs.h
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
/** Typical (MISO MOSI SCLK and CS are hard coded in MFRC522....) pin layout used:
-----------------------------------------------------------------------------------------
MFRC522 ESP866 NodeMCU NUMBER
Reader/PCD
Signal Pin Pin
-----------------------------------------------------------------------------------------
cs 15
SPI MOSI MOSI GPIO13 D7 13
SPI MISO MISO GPIO12 D6 12
SPI SCK SCK GPIO14 D5 14
GPIO9 SD2
GPIO10 SD3
RST/Reset RST GPIO4 D2 4
SPI SS SDA(SS) GPIO5 D1 5*/
//#if defined(ARDUINO_ESP8266_ESP01)
#include <SPI.h>
#include <MFRC522.h> //mine is modified to give 10Mhz with ESP #define SPI_CLOCK_DIV_4 10000000 #define myClock 10000000 //replaces SPI_CLOCK_DIV4
#define RST_PIN D0 //Configurable, see typical pin layout above
#define SS_PIN D1 //Configurable, see typical pin layout above
//Configurable, see typical pin layout above //setup
//RFID stuff***************************************************************
#define NR_OF_PORTS 1
#define NR_OF_SVS NR_OF_PORTS * 3 + 3
#define NR_OF_EXT_SVS 100 + NR_OF_PORTS * 3
MFRC522 mfrc522(SS_PIN, RST_PIN); //Create MFRC522 instance.
MFRC522::MIFARE_Key key;
void setMessageHeader(uint8_t *SendPacketSensor);
uint8_t processXferMess(uint8_t *LnRecMsg, uint8_t *LnSendMsg);
uint8_t lnCalcCheckSumm(uint8_t *cMessage, uint8_t cMesLen);
uint8_t uiLnSendMsbIdx = 12;
uint8_t uiStartChkSen;
uint8_t oldUid[UID_LEN] = {0};
uint8_t SendPacketSensor[16];
//#############################################################
bool delaying;
bool bRFIDactive;
unsigned char i = 0;
unsigned char j = 0;
uint32_t uiDelayTime = 500;
uint32_t RFIDQuietAfterTag = 3000;
#define PICC_REQIDL 0x26
int RFID_status;
int RFID_read = 0;
bool bRFIDquiet;
void SetupRFID(void ){
Serial.println("------------------------ MFRC 522 testing -----------------");
//* Setup rfidstuff *************************
SPI.begin(); //Init SPI bus//
mfrc522.PCD_Init(); //Init MFRC522 card
mfrc522.PCD_SetRegisterBitMask(mfrc522.RFCfgReg, (0x07 << 4)); //https://github.com/miguelbalboa/rfid/issues/43 //If you set Antenna Gain to Max it will increase reading distance
byte readReg = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
if ((readReg == 0x00) || (readReg == 0xFF)) { //missing reader
Serial.println(" Reader absent");
} else {
Serial.println(" Reader Present "); //would like to have spi speed here.
bReaderActive = true;
}
}
void SendUID (uint8_t *UID, uint8_t PHASE, int where ) { //phase 1 =on 0=off
//uint8_t SendMsg[4];
int tempaddr;
int addr;
addr = UID[1] + (UID[2] * 256);
addr = 100 + (addr & 127); //function to place all tags within a 127 range , starting at 100.. WILL RESULT IN DUPLICATE TAG ADDRESSES!!
if (!(addr == 100)) {
#if _SERIAL_DEBUG
//Serial.print(millis());
//Serial.print(" ms RFID ");
//Serial.print (where);
//Serial.println();
// Serial.print(" Addr:");
// Serial.print (addr);
// Serial.print (" Val:");
// Serial.println(PHASE);
#endif
if (PHASE == 1) {
SensorOutput_Inactive = false;
SendPortChange(200, 1, addr) ; //set rocnodeID to 200 for all RFID sends
//delay(50);
//SendPortChange(200,1,addr) ; //double send as experiment to see if this helps
}
if (PHASE == 0) {
SensorOutput_Inactive = true;
SendPortChange(200, 0, addr); //set rocnodeID to 200 for all RFID sends}//set rocnodeID to 200 for all RFID sends
//delay(50);
//SendPortChange(200,0,addr) ; //double send as experiment to see if this helps
}
oldUid[1] = 0;
oldUid[2] = 0;
}
}
void checkRFID() {
//RFIDCycle=RFIDCycle+20; //check rfid at 20ms intervals (would be sensible if I coud get this cycle below 20ms !!!)
if (bReaderActive) {
//OLD CODE
if ( mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
//Serial.println("Newcard and Piccreadserial line 107");
if (!compareUid( mfrc522.uid.uidByte, oldUid, mfrc522.uid.size)) { //NEW (NOT same UID)
if (!((oldUid[1] == 0) && (oldUid[2] == 0))) {
SendUID(oldUid, 0, 2); //clear old uid if it has not been cleared
}
SendUID(mfrc522.uid.uidByte, 1, 1);
RFIDCycle = millis() + RFIDQuietAfterTag; //switched on, so do'nt do any more reading for 2 secs..
copyUid(oldUid, mfrc522.uid.uidByte, mfrc522.uid.size); //save in oldUID
//uiStartTime = millis()+RFIDQuietAfterTag;
//bRFIDactive=true;
bRFIDquiet = false;
//Serial.println("Found new card..line116");
}//end NOT same UID same
else { //is the same card still...
// Serial.println("Same card..line119");
// uiStartTime = millis()+RFIDQuietAfterTag;
RFIDCycle = millis() + RFIDQuietAfterTag; //update time card was seen
} //same card end..
} else { //nothing read..
if ((!bRFIDquiet) && (millis() >= RFIDCycle + uiDelayTime)) {
// Serial.println("Nothing read for a while .line 126"); //wait delay before declaring the card gone
SendUID(oldUid, 0, 3);
bRFIDquiet = true;
} //if((uiActTime
}//nothing read
}//ifmrfc
}