From 4fbd64a51bb5068c039cfecff924828e62441f1a Mon Sep 17 00:00:00 2001 From: Stefan Kratochwil Date: Tue, 19 Nov 2024 21:12:40 +0100 Subject: [PATCH] Added deployment script and example main.py. --- software/src/mfrc522/deploy.sh | 33 ++++++++++++++++++ software/src/mfrc522/main.py | 61 ++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100755 software/src/mfrc522/deploy.sh create mode 100644 software/src/mfrc522/main.py diff --git a/software/src/mfrc522/deploy.sh b/software/src/mfrc522/deploy.sh new file mode 100755 index 0000000..64b0d6c --- /dev/null +++ b/software/src/mfrc522/deploy.sh @@ -0,0 +1,33 @@ +#!/usr/bin/bash +set -eu + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +MFRC522_LIBDIR=$(readlink -f "$SCRIPT_DIR"/../../lib/micropython-mfrc522) + +error_exit() +{ + MESSAGE="$1" + + echo "Error: $MESSAGE" + echo "Exit." + exit 1 +} + +check_command() +{ + CMD="$1" + + if ! command -v "$CMD" 2>&1 >/dev/null + then + error_exit "'$CMD' not found." + fi +} + +check_command mpremote + +mpremote fs cp "$MFRC522_LIBDIR/mfrc522.py" ":mfrc522.py" + +mpremote reset + +echo "Library deployed. Run main.py with 'mpremote run main.py'" diff --git a/software/src/mfrc522/main.py b/software/src/mfrc522/main.py new file mode 100644 index 0000000..2e13163 --- /dev/null +++ b/software/src/mfrc522/main.py @@ -0,0 +1,61 @@ +from mfrc522 import MFRC522 +import utime + + +def uidToString(uid): + mystring = "" + for i in uid: + mystring = "%02X" % i + mystring + return mystring + +def main(): + reader = MFRC522(spi_id=1,sck=10,miso=12,mosi=11,cs=13,rst=9) + + print("") + print("Please place card on reader") + print("") + + PreviousCard = [0] + + try: + while True: + reader.init() + + (stat, tag_type) = reader.request(reader.REQIDL) + #print('request stat:',stat,' tag_type:',tag_type) + if stat == reader.OK: + (stat, uid) = reader.SelectTagSN() + if uid == PreviousCard: + continue + if stat == reader.OK: + print("Card detected {} uid={}".format(hex(int.from_bytes(bytes(uid),"little",False)).upper(),reader.tohexstring(uid))) + + if reader.IsNTAG(): + print("Got NTAG{}".format(reader.NTAG)) + reader.MFRC522_Dump_NTAG(Start=0,End=reader.NTAG_MaxPage) + #print("Write Page 5 to 0x1,0x2,0x3,0x4 in 2 second") + #utime.sleep(2) + #data = [1,2,3,4] + #reader.writeNTAGPage(5,data) + #reader.MFRC522_Dump_NTAG(uid,Start=5,End=6) + else: + (stat, tag_type) = reader.request(reader.REQIDL) + if stat == reader.OK: + (stat, uid2) = reader.SelectTagSN() + if stat == reader.OK: + if uid != uid2: + continue + defaultKey = [255,255,255,255,255,255] + reader.MFRC522_DumpClassic1K(uid,Start=0, End=64, keyA=defaultKey) + PreviousCard = uid + else: + pass + else: + PreviousCard=[0] + utime.sleep_ms(50) + + except KeyboardInterrupt: + pass + +if __name__ == "__main__": + main()