Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# 03-04-2018 ADDED SUPPORT FOR ESP32

# micropython-mfrc522
(Micro)Python class to access the MFRC522 RFID reader

Expand All @@ -13,23 +15,29 @@ the [WiPy](https://github.com/micropython/micropython/tree/master/cc3200).
Put the modules ``mfrc522.py``, ``examples/read.py``, ``examples/write.py`` to the root of the flash FS on your board.
For the ESP8266 there are multiple solutions to do that. E.g. use the
[WebREPL file transfer](https://github.com/micropython/webrepl), or [mpfshell](https://github.com/wendlers/mpfshell).


For ESP32 I use ampy to transfer the files.

I used the following pins for my setup:

| Signal | GPIO ESP8266 | GPIO WiPy | Note |
| Signal | GPIO ESP8266 | GPIO WiPy | ESP32 |
| --------- | ------------ | -------------- | ------------------------------------ |
| sck | 0 | "GP14" | |
| mosi | 2 | "GP16" | |
| miso | 4 | "GP15" | |
| rst | 5 | "GP22" | |
| cs | 14 | "GP14" |Labeled SDA on most RFID-RC522 boards |

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't have a "cs" on my board.
so, if it really is labelled "sda" for me, keeping that comment seems useful.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can confirm that cs is labelled sda for me. at least it practically works if i wire it like that.

| sck | 0 | "GP14" | 18 |
| mosi | 2 | "GP16" | 23 |
| miso | 4 | "GP15" | 19 |
| rst | 5 | "GP22" | 4 |
| cs | 14 | "GP14" | 2 |

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't have some of these pins on my wems oled esp32 devboard. 16, 5, 26, 25, 4 worked for me.


Now enter the REPL you could run one of the two exmaples:

For detecting, authenticating and reading from a card:

import read
read.do_read()
cs_pin = 2
reader_number = 0
read.do_read(cs_pin, reader_number)

This way you can use multiple readers. Just make sure to assign a new cs_pin. The other pins can be shared.

This will wait for a MifareClassic 1k card. As soon the card is detected, it is authenticated, and
16 bytes are read from address 0x08.
Expand Down
9 changes: 7 additions & 2 deletions examples/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
from os import uname


def do_read():
def do_read(x,y):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pep8: please have a blank after the comma

also, don't call it x and y, but give good names.


if uname()[0] == 'WiPy':
rdr = mfrc522.MFRC522("GP14", "GP16", "GP15", "GP22", "GP17")
elif uname()[0] == 'esp8266':
rdr = mfrc522.MFRC522(0, 2, 4, 5, 14)

elif uname()[0] == 'esp32':
rdr = mfrc522.MFRC522(18, 23, 19, 4, x)

else:
raise RuntimeError("Unsupported platform")

print("")
print('READER %s' %y)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pep8: please have a blank after the percent

print("Place card before reader to read from address 0x08")
print("")

Expand Down Expand Up @@ -43,4 +48,4 @@ def do_read():
print("Failed to select tag")

except KeyboardInterrupt:
print("Bye")
print("Bye")
2 changes: 2 additions & 0 deletions examples/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def do_write():
rdr = mfrc522.MFRC522("GP14", "GP16", "GP15", "GP22", "GP17")
elif uname()[0] == 'esp8266':
rdr = mfrc522.MFRC522(0, 2, 4, 5, 14)
elif uname()[0] == 'esp32':
rdr = mfrc522.MFRC522(18, 23, 19, 4, 2)
else:
raise RuntimeError("Unsupported platform")

Expand Down
3 changes: 3 additions & 0 deletions mfrc522.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def __init__(self, sck, mosi, miso, rst, cs):
elif board == 'esp8266':
self.spi = SPI(baudrate=100000, polarity=0, phase=0, sck=self.sck, mosi=self.mosi, miso=self.miso)
self.spi.init()
elif board == 'esp32':
self.spi = SPI(baudrate=100000, polarity=0, phase=0, sck=self.sck, mosi=self.mosi, miso=self.miso)
self.spi.init()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as you are doing precisely the same thing as 3 lines above, how about just changing line 32 to:

elif board == 'esp8266' or board == 'esp32':

else:
raise RuntimeError("Unsupported platform")

Expand Down