Add support for MFRC522

Add support for SPI connected MFRC522 13.56MHz rfid card reader (#9916)
This commit is contained in:
Theo Arends 2020-12-06 16:32:07 +01:00
parent 80ab642076
commit 931929c952
5 changed files with 27 additions and 24 deletions

View File

@ -150,6 +150,7 @@
| | | | | | | | | | | | | | | | | |
| Feature or Sensor | minimal | lite | tasmota | knx | sensors | ir | display | Remarks | Feature or Sensor | minimal | lite | tasmota | knx | sensors | ir | display | Remarks
| USE_SPI | - | - | - | - | - | - | x | | USE_SPI | - | - | - | - | - | - | x |
| USE_RC522 | - | - | - | - | - | - | - |
| USE_MHZ19 | - | - | - | - | x | - | - | | USE_MHZ19 | - | - | - | - | x | - | - |
| USE_SENSEAIR | - | - | - | - | x | - | - | | USE_SENSEAIR | - | - | - | - | x | - | - |
| USE_PMS5003 | - | - | - | - | x | - | - | | USE_PMS5003 | - | - | - | - | x | - | - |

View File

@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.
- TyuaMcu update 2/3 by Federico Leoni (#10004) - TyuaMcu update 2/3 by Federico Leoni (#10004)
- Optional CCloader support for CC25xx Zigbee or CC26xx BLE by Christian Baars (#9970) - Optional CCloader support for CC25xx Zigbee or CC26xx BLE by Christian Baars (#9970)
- Command ``RfProtocol`` to control RcSwitch receive protocols by BBBits (#10063) - Command ``RfProtocol`` to control RcSwitch receive protocols by BBBits (#10063)
- Support for SPI connected MFRC522 13.56MHz rfid card reader (#9916)
### Breaking Changed ### Breaking Changed
- KNX DPT9 (16-bit float) to DPT14 (32-bit float) by Adrian Scillato (#9811, #9888) - KNX DPT9 (16-bit float) to DPT14 (32-bit float) by Adrian Scillato (#9811, #9888)

View File

@ -73,6 +73,7 @@ The attached binaries can also be downloaded from http://ota.tasmota.com/tasmota
- Support for Shelly Dimmer 1 and 2 by James Turton (#9854) - Support for Shelly Dimmer 1 and 2 by James Turton (#9854)
- Support for common anode sevenseg displays by adding ``#define USE_DISPLAY_SEVENSEG_COMMON_ANODE`` by Ken Sanislo (#9963) - Support for common anode sevenseg displays by adding ``#define USE_DISPLAY_SEVENSEG_COMMON_ANODE`` by Ken Sanislo (#9963)
- Support for multiple WeMo devices by Magic73 (#9208) - Support for multiple WeMo devices by Magic73 (#9208)
- Support for SPI connected MFRC522 13.56MHz rfid card reader (#9916)
- KNX read reply for Power (#9236, #9891) - KNX read reply for Power (#9236, #9891)
- Fallback NTP server from x.pool.ntp.org if no ntpservers are configured - Fallback NTP server from x.pool.ntp.org if no ntpservers are configured
- Optional CCloader support for CC25xx Zigbee or CC26xx BLE by Christian Baars (#9970) - Optional CCloader support for CC25xx Zigbee or CC26xx BLE by Christian Baars (#9970)

View File

@ -625,6 +625,9 @@
// #define USE_DISPLAY_SSD1351 // [DisplayModel 9] // #define USE_DISPLAY_SSD1351 // [DisplayModel 9]
// #define USE_DISPLAY_RA8876 // [DisplayModel 10] [I2cDriver39] (Touch) // #define USE_DISPLAY_RA8876 // [DisplayModel 10] [I2cDriver39] (Touch)
// #define USE_RC522 // Add support for MFRC522 13.56Mhz Rfid reader (+6k code) // #define USE_RC522 // Add support for MFRC522 13.56Mhz Rfid reader (+6k code)
// #define USE_RC522_DATA_FUNCTION // Add support for reading data block content (+0k4 code)
// #define USE_RC522_TYPE_INFORMATION // Add support for showing card type (+0k4 code)
#endif // USE_SPI #endif // USE_SPI
// -- Serial sensors ------------------------------ // -- Serial sensors ------------------------------

View File

@ -36,7 +36,8 @@
#define XSNS_80 80 #define XSNS_80 80
#define USE_RC522_DATA_FUNCTION //#define USE_RC522_DATA_FUNCTION // Add support for reading data block content (+0k4 code)
//#define USE_RC522_TYPE_INFORMATION // Add support for showing card type (+0k4 code)
#include <MFRC522.h> #include <MFRC522.h>
MFRC522 *Mfrc522; MFRC522 *Mfrc522;
@ -52,22 +53,19 @@ void RC522ScanForTag(void) {
if (!Mfrc522->PICC_IsNewCardPresent() || !Mfrc522->PICC_ReadCardSerial()) { return; } if (!Mfrc522->PICC_IsNewCardPresent() || !Mfrc522->PICC_ReadCardSerial()) { return; }
ToHex_P((unsigned char*)Mfrc522->uid.uidByte, Mfrc522->uid.size, Rc522.uids, sizeof(Rc522.uids)); ToHex_P((unsigned char*)Mfrc522->uid.uidByte, Mfrc522->uid.size, Rc522.uids, sizeof(Rc522.uids));
ResponseTime_P(PSTR(",\"RC522\":{\"UID\":\"%s\""), Rc522.uids);
MFRC522::PICC_Type piccType = Mfrc522->PICC_GetType(Mfrc522->uid.sak); MFRC522::PICC_Type picc_type = Mfrc522->PICC_GetType(Mfrc522->uid.sak);
AddLog_P(LOG_LEVEL_DEBUG, PSTR("MFR: UID %s, Type %s"), Rc522.uids, Mfrc522->PICC_GetTypeName(piccType));
#ifdef USE_RC522_DATA_FUNCTION #ifdef USE_RC522_DATA_FUNCTION
bool didit = false; if ( picc_type == MFRC522::PICC_TYPE_MIFARE_MINI
if ( piccType == MFRC522::PICC_TYPE_MIFARE_MINI || picc_type == MFRC522::PICC_TYPE_MIFARE_1K
|| piccType == MFRC522::PICC_TYPE_MIFARE_1K || picc_type == MFRC522::PICC_TYPE_MIFARE_4K) {
|| piccType == MFRC522::PICC_TYPE_MIFARE_4K) {
uint8_t trailerBlock = 7;
MFRC522::MIFARE_Key key; MFRC522::MIFARE_Key key;
for (uint32_t i = 0; i < 6; i++) { for (uint32_t i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF; key.keyByte[i] = 0xFF;
} }
MFRC522::StatusCode status = Mfrc522->PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(Mfrc522->uid)); MFRC522::StatusCode status = Mfrc522->PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(Mfrc522->uid));
if (status == MFRC522::STATUS_OK) { if (status == MFRC522::STATUS_OK) {
uint8_t buffer[18]; // The buffer must be at least 18 bytes because a CRC_A is also returned uint8_t buffer[18]; // The buffer must be at least 18 bytes because a CRC_A is also returned
uint8_t size = sizeof(buffer); uint8_t size = sizeof(buffer);
@ -81,23 +79,21 @@ void RC522ScanForTag(void) {
card_datas[i] = '\0'; card_datas[i] = '\0';
} }
} }
didit = true; ResponseAppend_P(PSTR(",\"" D_JSON_DATA "\":\"%s\""), card_datas);
ResponseTime_P(PSTR(",\"RC522\":{\"UID\":\"%s\",\"" D_JSON_DATA "\":\"%s\"}}"), Rc522.uids, card_datas);
} }
} }
} }
if (!didit) { #endif // USE_RC522_DATA_FUNCTION
ResponseTime_P(PSTR(",\"RC522\":{\"UID\":\"%s\"}}"), Rc522.uids); #ifdef USE_RC522_TYPE_INFORMATION
} ResponseAppend_P(PSTR(",\"" D_JSON_TYPE "\":\"%s\""), Mfrc522->PICC_GetTypeName(picc_type));
#else #endif // USE_RC522_TYPE_INFORMATION
ResponseTime_P(PSTR(",\"RC522\":{\"UID\":\"%s\"}}"), Rc522.uids); ResponseJsonEndEnd();
#endif
MqttPublishTeleSensor(); MqttPublishTeleSensor();
// Halt PICC Mfrc522->PICC_HaltA(); // Halt PICC
Mfrc522->PICC_HaltA(); Mfrc522->PCD_StopCrypto1(); // Stop encryption on PCD
Rc522.scantimer = 7; // Ignore tags found for two seconds Rc522.scantimer = 7; // Ignore tags found for two seconds
} }
void RC522Init(void) { void RC522Init(void) {
@ -105,7 +101,7 @@ void RC522Init(void) {
Mfrc522 = new MFRC522(Pin(GPIO_SPI_CS), Pin(GPIO_RC522_RST)); Mfrc522 = new MFRC522(Pin(GPIO_SPI_CS), Pin(GPIO_RC522_RST));
SPI.begin(); SPI.begin();
Mfrc522->PCD_Init(); Mfrc522->PCD_Init();
if (Mfrc522->PCD_PerformSelfTest()) { // if (Mfrc522->PCD_PerformSelfTest()) { // Saves 0k5 code
uint8_t v = Mfrc522->PCD_ReadRegister(Mfrc522->VersionReg); uint8_t v = Mfrc522->PCD_ReadRegister(Mfrc522->VersionReg);
char ver[8] = { 0 }; char ver[8] = { 0 };
switch (v) { switch (v) {
@ -118,13 +114,14 @@ void RC522Init(void) {
ToHex_P((unsigned char*)empty_uid, sizeof(empty_uid), Rc522.uids, sizeof(Rc522.uids)); ToHex_P((unsigned char*)empty_uid, sizeof(empty_uid), Rc522.uids, sizeof(Rc522.uids));
AddLog_P(LOG_LEVEL_INFO, PSTR("MFR: RC522 Rfid Reader detected %s"), ver); AddLog_P(LOG_LEVEL_INFO, PSTR("MFR: RC522 Rfid Reader detected %s"), ver);
Rc522.present = true; Rc522.present = true;
} // }
// Mfrc522->PCD_Init(); // Re-init as SelfTest blows init
} }
} }
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
void RC522Show(void) { void RC522Show(void) {
WSContentSend_PD(PSTR("{s}RC522 UID{m}%s {e}"), Rc522.uids); WSContentSend_PD(PSTR("{s}RC522 UID{m}%s{e}"), Rc522.uids);
} }
#endif // USE_WEBSERVER #endif // USE_WEBSERVER