dotnet / iot

This repo includes .NET Core implementations for various IoT boards, chips, displays and PCBs.
MIT License
2.15k stars 581 forks source link

Add bindings for RFID-RC522 (MF-RC522) #175

Closed krwq closed 3 years ago

krwq commented 5 years ago

This is currently WIP https://github.com/dotnet/iot/pull/293 - please pick up from here when implementing (PR is closed)

marinasundstrom commented 5 years ago

@krwq I just started to port a module that I had previously contributed to another project, then I saw that you already had started making one. What is the status?

Anyway, here is my port: https://github.com/robertsundstrom/iot/commit/7cfba1f35dd5f5d5eb0b16347c4453eb2fab229d

It needs some polishing on the interface, as it is based on older code (not mine) written in Java and Python.

krwq commented 5 years ago

@robertsundstrom I have only barebones (the super low level stuff), reading UIDs from MIFARE tags and detecting presence of card (no authentication) but was mostly porting https://github.com/miguelbalboa/rfid with some improvement and the code is currently very far from pretty (although I have made it mostly non-allocating) - I still do not fully understand some of the pieces from that code (I only started working on this again today after not touching the code for 2 weeks). You can find the code in here (I usually push to the private fork first though but can push directly to public) - I didn't even call my project correctly at first (RfidRc522 instead of MFRC522): https://github.com/krwq/iot/tree/rfid_rc522/src/devices/RfidRc522 (I might have over-complicated SPI part a bit)

Your code seems much simpler than mine so what we can do is perhaps:

what I don't like in my version of code is that there is a lot of magic numbers all around and basically impossible to read without the spec (although yours is simpler and much easier to read still has many "magic" pieces).

I currently lack of higher level context on how exactly RFID tags are used. I could only think of reading tag IDs and I know that at least some tags support some form of authentication but have very little knowledge about how this exactly works... MFRC522 has fairly hard to read datasheet (at least for me) although have found some more interesting collection of datasheets in here: https://github.com/dalmirdasilva/ArduinoRadioFrequencyIdentification/tree/master/MifareClassic/datasheet but haven't had time to skim through that yet

krwq commented 5 years ago

Seems https://github.com/dalmirdasilva/ArduinoRadioFrequencyIdentification/blob/master/ReaderMFRC522/datasheet/TagTypes.pdf has some more high level description on different types of tags. All tags have some kind of UID and some devices allow to change the data and then permanently lock the data. Depending on the tag type they have different protocols on what can be done on them with some common stuff.

My rough idea (feel free to propose something better since I only have some basic knowledge in the subject) - I'd split this into 2 parts:

First version should support at minimum 1 or 2 most common tag types. I currently got this set: https://www.banggood.com/RC522-Chip-IC-Card-Induction-Module-RFID-Reader-p-81067.html?p=DQ30066511122014069J&utm_campaign=educ8stv&utm_content=3216&cur_warehouse=CN

krwq commented 5 years ago

@robertsundstrom also please make sure the license on the code you are porting is fine (in the case of library I shared it is public domain)

marinasundstrom commented 5 years ago

@krwq I also lack sufficient knowledge about the hardware and protocol.

As I have said, the code was ported to C#, and then rewritten when I contributed it to Unosquare.RaspberryIO. They built a light but better interface that still reflect the core concepts with messages as requests and responses.

I suggest having a look at it.

marinasundstrom commented 5 years ago

Unosquare.RapsberryIO

Source: https://github.com/unosquare/raspberryio/tree/master/src/Unosquare.RaspberryIO.Peripherals/Rfid

Playground: https://github.com/unosquare/raspberryio/blob/master/src/Unosquare.RaspberryIO.Playground/Program.Rfid.cs

marinasundstrom commented 5 years ago

I would not create a higher-level abstraction unless the purpose is to handle multiple types of readers and formats through one common interface.

This is device-specific.

krwq commented 5 years ago

fair enough, @robertsundstrom I'm fine with you picking this up and I can help/join in both writing and testing. Please make sure to add some APIs to read UID and add couple of samples on the authentication. Feel free to use the code I shared in any way if you find anything useful there. The moment we add second similar device we can think about having common abstraction - until then I'd stick to being device specific. Feel free to open WIP PR, I can help by pushing directly to that PR branch (i.e. remove allocations and stuff - make sure to mark PR appropriately if you do so)

marinasundstrom commented 5 years ago

Alright. I will see what I can accomplish. Will notify when I have something.

marinasundstrom commented 5 years ago

After some experimentation: I have updated the commit to the branch in my fork: https://github.com/robertsundstrom/iot/commit/293e87e45323f36c10109c332fbfe3740ebcaa91

Tags are being read correctly now.

I originally ported the code from Python and I did some mistakes that they (Unosquare) discovered. That is now fixed here as well.

The interface is the original and it should reflect the terminology used in the actual protocol.

Some refactoring, changes of variable names and such are welcome. Let's discuss how to improve the interface.

Functionality other than reading card needs to be tested as well.

krwq commented 5 years ago

I've added comments on that commit - I think as long as we don't deviate much from original naming it should be fine to deviate. (i.e. AntiColl => AntiCollision)

I'd recommend creating PR with this. Also please make sure to add samples for the common scenario which end up reading tag (currently I've seen a sample but for someone who doesn't have any knowledge on the subject it might be hard to understand)

(i.e create one section in the readme per scenario)

marinasundstrom commented 5 years ago

I've added comments on that commit - I think as long as we don't deviate much from original naming it should be fine to deviate. (i.e. AntiColl => AntiCollision)

I'd recommend creating PR with this. Also please make sure to add samples for the common scenario which end up reading tag (currently I've seen a sample but for someone who doesn't have any knowledge on the subject it might be hard to understand)

(i.e create one section in the readme per scenario)

Agree.

I'm on it! Will go through your comments when I've done my own refactoring.

I will submit a PR when I'm ready.

krwq commented 5 years ago

Ref: https://github.com/dotnet/iot/issues/320

marinasundstrom commented 5 years ago

I have now got a proper workstation at home. This enables me to resume my work on this binding. My goal is to clean it up.

I would like to reopen the PR and fix what has been pointed out.

krwq commented 5 years ago

@robertsundstrom please wait for @Ellerbach to comment since he was planning to merge our work with his PN532 (#320) which he already got much further than we did (per our offline conversation). We want to merge as much code as possible here since this code is rather complicated comparing to other bindings.

marinasundstrom commented 5 years ago

@krwq OK. Anyway, I have updated my branch.

Ellerbach commented 5 years ago

@robertsundstrom, I have been working on it a bit. As @krwq mentionned, I have been working on the PN532 and I'm about to propose a first PR. I took out what is related to the Mifare cards and created an abstract function that can be impleted by any RFID/NFC reader that supports 14443 for thos kind of cards. I did the same for credit cards (144443 type B) for a project. And The same function can be used by any reader as well. @robertsundstrom , where can I find your branch?

marinasundstrom commented 5 years ago

@Ellerbach Here: https://github.com/robertsundstrom/iot/tree/Mfrc522 🙂

Ellerbach commented 5 years ago

@robertborr , I tryed my reader on an ESP8266 and looks like it has some issues. As soon as I do an authentication, it blocks. Same behavior I had with .NET Core. I did couple of modifications in your files and Added few registers to make it easier to debug. Please have a look at: https://github.com/Ellerbach/Pn532/tree/master/Mfrc522 I have implemented as well a full Mifare card (it's currently Under PR) but you have the full project available. And you can try what I did. It may work (again, I had issues with the hardware).

marinasundstrom commented 5 years ago

I should have mentioned that I have not focused on authentication. It was the last things I worked on before pausing. So it os not working at the moment. I’ll have a look at it later.

If you want, you could have a look at Unosquare’s code (link either here in issue or in the PR). It is based on my original code. They might have fixed the issue there.

Get Outlook for iOShttps://aka.ms/o0ukef


From: Laurent Ellerbach notifications@github.com Sent: Tuesday, July 16, 2019 4:04:44 PM To: dotnet/iot Cc: Robert Sundström; Mention Subject: Re: [dotnet/iot] Add bindings for RFID-RC522 (MF-RC522) (#175)

@robertborrhttps://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Frobertborr&data=02%7C01%7C%7Cea043a6b18464fa0892d08d709f68e8b%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636988826858790598&sdata=RN2yrDwgplEms3NZ0HppS%2FWehoiKJy%2FIuHVOoclC74Y%3D&reserved=0 , I tryed my reader on an ESP8266 and looks like it has some issues. As soon as I do an authentication, it blocks. Same behavior I had with .NET Core. I did couple of modifications in your files and Added few registers to make it easier to debug. Please have a look at: https://github.com/Ellerbach/Pn532/tree/master/Mfrc522https://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FEllerbach%2FPn532%2Ftree%2Fmaster%2FMfrc522&data=02%7C01%7C%7Cea043a6b18464fa0892d08d709f68e8b%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636988826858800602&sdata=49Vk4uTESf9x%2FAfdTJg6CmaXJpTBmSf7I4rrZkdAd8E%3D&reserved=0 I have implemented as well a full Mifare card (it's currently Under PR) but you have the full project available. And you can try what I did. It may work (again, I had issues with the hardware).

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fdotnet%2Fiot%2Fissues%2F175%3Femail_source%3Dnotifications%26email_token%3DAAHAQHHPDD662WHTGT7RXRDP7XIPZA5CNFSM4GTFD3U2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2A6SMQ%23issuecomment-511830322&data=02%7C01%7C%7Cea043a6b18464fa0892d08d709f68e8b%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636988826858800602&sdata=LoOkP8t7%2FHVslmFWf6bQe27d%2FqumjXknB1nIYjCdRkA%3D&reserved=0, or mute the threadhttps://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAAHAQHFYME4FDNJ7LQOL3LDP7XIPZANCNFSM4GTFD3UQ&data=02%7C01%7C%7Cea043a6b18464fa0892d08d709f68e8b%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636988826858810606&sdata=UKlQSe90QyKG%2Fwa6SMnaJcf3t1qUeDDFqzV45mMAZ8E%3D&reserved=0.

robertborr commented 5 years ago

@Ellerbach - while this is indeed interesting, I'm probably not the person you want. :)

marinasundstrom commented 5 years ago

I left a comment here: https://github.com/Ellerbach/Pn532/issues/1

marinasundstrom commented 4 years ago

What is the status?

krwq commented 4 years ago

@robertsundstrom PN532 is merged, for MFRC522 not sure cc: @Ellerbach

marinasundstrom commented 4 years ago

@krwq It is not inte package @Ellerbach

forki commented 4 years ago

Is there something I could test for MFRC522?

forki commented 4 years ago

@robertsundstrom did you create a PR? where is it?

Ellerbach commented 4 years ago

I4ve been working on the MFRC522 but it does not even work correctly with an Arduino. Authentication for the Miface cards is not working correctly. Even with various Arduino library, I did not managed to make it working well enough. I had the same issues with .NET Core IoT. so I basically drop the work to focus more on the PN532 which is indeded a great chip. If anyone managed to find a way to make the authentication working perfectly, don't forget to implement the CardTransciver class: https://github.com/dotnet/iot/blob/master/src/devices/Card/CardTransceiver.cs This will allow to reuse all the Miface card code. In general, any RFID reader we'll write has to implement this class to support all the common card work already published.

forki commented 4 years ago

What's the status of MFRC522?

Ellerbach commented 4 years ago

What's the status of MFRC522?

I did spend quite some time and decided to stop as I didn't even manage to make it working correctly on an Arduino with the official libraries and the basic examples. Authentication failed on all my cards even specifying correct keys. I bought 2 different hardware and I have the same issue on both hardware, so very unlikely that both are defective. This hardware is very limited anyway. That said, if you have time, you can try to make it working. Again, to get the support of all the libraries that are already in the framework, implement CardTrasceiver and all the rest will be magic.

forki commented 4 years ago

Is there any progress on this?

Ellerbach commented 4 years ago

Not really, there seems to be an issue with the hardware itself. It's working ok on an Arduino for example but not with an ESP8266. I have multiple versions of the MFRC522 so tried with different hardware. Result is never the same. So I would prefer not to spend too much time on it giving the fact that there are more readers, on the same price range with better features reading a broader range of cards which are already implemented.

forki commented 4 years ago

issue with the hardware itself

It works in many many IoT scenarios with node. I really was hoping to eventually port it to dotnet core especially since asp.net core deprecated INodeServices. Bummer

krwq commented 3 years ago

@Ellerbach this is also closed with your recent PR

forki commented 3 years ago

@krwq are there docs on how to use this?

krwq commented 3 years ago

Hey @forki,

Refer to README.md and samples folder here: https://github.com/dotnet/iot/tree/master/src/devices/Mfrc522

If you haven't bought your reader yet then consider buying this one instead: https://github.com/dotnet/iot/tree/master/src/devices/Pn532

let me know if you have any specific questions.