public RfidReader(ILogger <RfidReader> logger) { _logger = logger; _rfidController = new RFIDControllerMfrc522(Pi.Spi.Channel0, 500000, Pi.Gpio[22]); _whenCardDetected = new Subject <CardData>(); }
private static void ReadCard() { Console.Clear(); "Testing RFID".Info(); var device = new RFIDControllerMfrc522(Pi.Spi.Channel0, 500000, Pi.Gpio[18]); while (true) { // If a card is found if (device.DetectCard() != RFIDControllerMfrc522.Status.AllOk) { continue; } // Get the UID of the card var uidResponse = device.ReadCardUniqueId(); // If we have the UID, continue if (uidResponse.Status != RFIDControllerMfrc522.Status.AllOk) { continue; } var cardUid = uidResponse.Data; // Print UID $"Card UID: {cardUid[0]},{cardUid[1]},{cardUid[2]},{cardUid[3]}".Info(); // Select the scanned tag device.SelectCardUniqueId(cardUid); // Authenticate sector if (device.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, cardUid, 19) == RFIDControllerMfrc522.Status.AllOk) { var data = device.CardReadData(16); var text = Encoding.ASCII.GetString(data.Data); $" Message read: {text}".Info(); } else { "Authentication error".Error(); } device.ClearCardSelection(); ExitMessage.WriteLine(); while (true) { var input = Console.ReadKey(true).Key; if (input != ConsoleKey.Escape) { continue; } break; } break; } }
private static void WriteCard() { Console.Clear(); "Testing RFID".Info(); var device = new RFIDControllerMfrc522(Pi.Spi.Channel0, 500000, Pi.Gpio[18]); var userInput = Terminal.ReadLine("Insert a message to be written in the card (16 characters only)").Truncate(16); "Place the card on the sensor".Info(); while (true) { // If a card is found if (device.DetectCard() != RFIDControllerMfrc522.Status.AllOk) { continue; } // Get the UID of the card var uidResponse = device.ReadCardUniqueId(); // If we have the UID, continue if (uidResponse.Status != RFIDControllerMfrc522.Status.AllOk) { continue; } var cardUid = uidResponse.Data; // Select the scanned tag device.SelectCardUniqueId(cardUid); // Writing data to sector 1 blocks // Authenticate sector if (device.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, cardUid, 19) == RFIDControllerMfrc522.Status.AllOk) { userInput = (userInput + new string(' ', 16)).Truncate(16); device.CardWriteData(16, Encoding.ASCII.GetBytes(userInput)); } device.ClearCardSelection(); "Data has been written".Info(); Terminal.WriteLine(ExitMessage); while (true) { var input = Console.ReadKey(true).Key; if (input != ConsoleKey.Escape) { continue; } break; } break; } }
private static void CardDetected() { Console.Clear(); "Testing RFID".Info(); var device = new RFIDControllerMfrc522(Pi.Spi.Channel0, 500000, Pi.Gpio[18]); while (true) { // If a card is found if (device.DetectCard() != RFIDControllerMfrc522.Status.AllOk) { continue; } "Card detected".Info(); // Get the UID of the card var uidResponse = device.ReadCardUniqueId(); // If we have the UID, continue if (uidResponse.Status != RFIDControllerMfrc522.Status.AllOk) { continue; } var cardUid = uidResponse.Data; // Print UID $"Card UID: {cardUid[0]},{cardUid[1]},{cardUid[2]},{cardUid[3]}".Info(); Terminal.WriteLine(ExitMessage); while (true) { var input = Console.ReadKey(true).Key; if (input != ConsoleKey.Escape) { continue; } break; } break; } }
public static void Init() { Pi.Init <BootstrapWiringPi>(); // set IO Pin to Output _blinkingPin = Pi.Gpio[17]; _blinkingPin.PinMode = GpioPinDriveMode.Output; _pulsePin = Pi.Gpio[18]; _pulsePin.PinMode = GpioPinDriveMode.Output; Console.WriteLine("** GPIO Initialization done **"); // init RC522 Reader // default constructor uses the BCM22 pin _nfcReader = new RFIDControllerMfrc522(); Console.WriteLine("** RC522 Initialization done **"); _initDone = true; Console.WriteLine("\n**** Raspberry Informations: ****\n"); Console.WriteLine(Pi.Info); }
private void LoginScreen() { var dev = new RFIDControllerMfrc522(Pi.Spi.Channel0, Pi.Spi.Channel0Frequency, Pi.Gpio[18]); while (!loggedIn) { if (dev.DetectCard() != RFIDControllerMfrc522.Status.AllOk) { continue; } var uidResponse = dev.ReadCardUniqueId(); if (uidResponse.Status != RFIDControllerMfrc522.Status.AllOk) { continue; } var cardUid = uidResponse.Data; txt_login.Text = $"Card UID: {uidResponse}"; loggedIn = true; } }
private static void TestRfidController() { var device = new RFIDControllerMfrc522(); while (true) { // If a card is found if (device.DetectCard() == RFIDControllerMfrc522.Status.AllOk) { "Card detected".Info(); } // Get the UID of the card var uidResponse = device.ReadCardUniqueId(); // If we have the UID, continue if (uidResponse.Status == RFIDControllerMfrc522.Status.AllOk) { var cardUid = uidResponse.Data; // Print UID $"Card UID: {cardUid[0]},{cardUid[1]},{cardUid[2]},{cardUid[3]}".Info(); // Select the scanned tag device.SelectCardUniqueId(cardUid); // Check if authenticated if (device.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, cardUid) == RFIDControllerMfrc522.Status.AllOk) { device.ReadRegister(RFIDControllerMfrc522.Register.Status2Reg); device.ClearCardSelection(); } else { "Authentication error".Error(); } } } }
private static void TestRfidController() { "Testing RFID".Info(); var device = new RFIDControllerMfrc522(Pi.Spi.Channel1, 500000, Pi.Gpio[18]); while (true) { // If a card is found if (device.DetectCard() == RFIDControllerMfrc522.Status.AllOk) { "Card detected".Info(); // Get the UID of the card var uidResponse = device.ReadCardUniqueId(); // If we have the UID, continue if (uidResponse.Status == RFIDControllerMfrc522.Status.AllOk) { var cardUid = uidResponse.Data; // Print UID $"Card UID: {cardUid[0]},{cardUid[1]},{cardUid[2]},{cardUid[3]}".Info(); // Select the scanned tag device.SelectCardUniqueId(cardUid); // Writing data to sector 1 blocks // Authenticate sector if (device.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, cardUid, 7) == RFIDControllerMfrc522.Status.AllOk) { var data = new byte[16 * 3]; for (var x = 0; x < data.Length; x++) { data[x] = (byte)(x + 65); } for(int b = 0; b < 3; b++) { device.CardWriteData((byte)(4 + b), data.Skip(b * 16).Take(16).ToArray()); } } // Reading data var continueReading = true; for (int s = 0; s < 16 && continueReading; s++) { // Authenticate sector if (device.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, cardUid, (byte)((4 * s) + 3)) == RFIDControllerMfrc522.Status.AllOk) { $"Sector {s}".Info(); for (int b = 0; b < 3 && continueReading; b++) { var data = device.CardReadData((byte)((4 * s) + b)); if (data.Status != RFIDControllerMfrc522.Status.AllOk) { continueReading = false; break; } $" Block {b} ({data.Data.Length} bytes): {string.Join(" ", data.Data.Select(x => x.ToString("X2")))}".Info(); } } else { "Authentication error".Error(); break; } } device.ClearCardSelection(); } } } }
private static void ReadAllCardSectors() { Console.Clear(); "Testing RFID".Info(); var device = new RFIDControllerMfrc522(Pi.Spi.Channel0, 500000, Pi.Gpio[18]); while (true) { // If a card is found if (device.DetectCard() != RFIDControllerMfrc522.Status.AllOk) { continue; } // Get the UID of the card var uidResponse = device.ReadCardUniqueId(); // If we have the UID, continue if (uidResponse.Status != RFIDControllerMfrc522.Status.AllOk) { continue; } var cardUid = uidResponse.Data; // Print UID $"Card UID: {cardUid[0]},{cardUid[1]},{cardUid[2]},{cardUid[3]}".Info(); // Select the scanned tag device.SelectCardUniqueId(cardUid); // Reading data var continueReading = true; for (var s = 0; s < 16 && continueReading; s++) { // Authenticate sector if (device.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, cardUid, (byte)((4 * s) + 3)) == RFIDControllerMfrc522.Status.AllOk) { $"Sector {s}".Info(); for (var b = 0; b < 3 && continueReading; b++) { var data = device.CardReadData((byte)((4 * s) + b)); if (data.Status != RFIDControllerMfrc522.Status.AllOk) { continueReading = false; break; } $" Block {b} ({data.Data.Length} bytes): {string.Join(" ", data.Data.Select(x => x.ToString("X2")))}".Info(); } } else { $"Authentication error, sector {s}".Error(); } } device.ClearCardSelection(); Terminal.WriteLine(ExitMessage); while (true) { var input = Console.ReadKey(true).Key; if (input != ConsoleKey.Escape) { continue; } break; } break; } }
private async Task Run() { "Idle".Info(); var device = new RFIDControllerMfrc522(Pi.Spi.Channel0, 500000, Pi.Gpio[22]); while (true) { // If a card is found if (device.DetectCard() != RFIDControllerMfrc522.Status.AllOk) { continue; } // Get the UID of the card var uidResponse = device.ReadCardUniqueId(); // If we have the Uid continue if (uidResponse.Status != RFIDControllerMfrc522.Status.AllOk) { continue; } var uid = uidResponse.Data; device.SelectCardUniqueId(uid); // Authentication if (device.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, uid, 19) == RFIDControllerMfrc522.Status.AllOk) { var data = device.CardReadData(16); var secretText = Encoding.ASCII.GetString(data.Data); if (secretText == "HelloHelloHellos") { // Card with access "Access granted!".Info(); ControlLed.BlinkLed(Pi.Gpio[BcmPin.Gpio22], -1); await _unitOfWork.Logs.AddAsync(new Log { Uid = uid, AttemptType = AttemptType.Success, Message = "Door has been successfully unlocked." }).ConfigureAwait(false); await _doorService.UpdateDoorStateAsync(DoorStatus.Open).ConfigureAwait(false); _access = true; //Wait until button is pressed. while (!_readAgain) { await Task.Delay(500).ConfigureAwait(false); } _readAgain = false; ControlLed.BlinkLed(Pi.Gpio[BcmPin.Gpio22], 0); await _doorService.UpdateDoorStateAsync(DoorStatus.Closed).ConfigureAwait(false); } else { // Card without access "Access denied!".Info(); ControlLed.BlinkLed(Pi.Gpio[BcmPin.Gpio18], 3000); await _unitOfWork.Logs.AddAsync(new Log { AttemptType = AttemptType.Fail, Message = "Card does not have permission to unlock door!" }).ConfigureAwait(false); } } else { // Unknown Card ControlLed.BlinkLed(Pi.Gpio[BcmPin.Gpio18], 3000); await _unitOfWork.Logs.AddAsync(new Log { AttemptType = AttemptType.UnkownUid, Message = "Authentication error, can't read card!" }).ConfigureAwait(false); } device.ClearCardSelection(); await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false); } }