From 0609aaef55b37eb08bea6cd7771f9803e9681a95 Mon Sep 17 00:00:00 2001 From: Stefan Kratochwil Date: Tue, 11 Feb 2025 22:30:55 +0100 Subject: [PATCH] DO NOT MERGE: Just a PoC. Web request blocks, but it yields a uid if chip is presented. --- software/src/microdot/main.py | 46 ++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/software/src/microdot/main.py b/software/src/microdot/main.py index a98ff28..8b7acfc 100644 --- a/software/src/microdot/main.py +++ b/software/src/microdot/main.py @@ -1,6 +1,9 @@ -import rp2 +import asyncio import network import ubinascii +import rp2 + +from mfrc522 import MFRC522 from microdot import Microdot rp2.country('DE') @@ -21,6 +24,35 @@ print(f" essid: {wlan.config('essid')}") print(f" txpower: {wlan.config('txpower')}") print(f"ifconfig: {wlan.ifconfig()}") + +def uid_to_string(uid: list): + return '0x' + ''.join(f'{i:02x}' for i in uid) + + + +async def get_tag_uid(reader: MFRC522, poll_interval_ms: int = 50) -> list: + ''' + The maximum measured delay with poll_interval_ms=50 and a reader with tocard_retries=5 is + 15.9 ms: + delay (min / max / avg) [µs]: (360 / 15945 / 1.892923e-06) + + The maximum measured delay dropped to 11.6 ms by setting tocard_retries=1: + delay (min / max / avg) [µs]: (368 / 11696 / 6.204211e-06) + ''' + while True: + reader.init() + + # For now we omit the tag type + (stat, _) = reader.request(reader.REQIDL) + if stat == reader.OK: + (stat, uid) = reader.SelectTagSN() + if stat == reader.OK: + print(f"uid={uid_to_string(uid)}") + return uid + + await asyncio.sleep_ms(poll_interval_ms) + + app = Microdot() @@ -35,4 +67,16 @@ async def index(request): print(f" cookies: {request.cookies}") return "TonberryPico says 'Hello World!'" + +@app.route('/nfc') +async def blafasel(request): + print("/nfc") + + reader = MFRC522(spi_id=1, sck=10, miso=12, mosi=11, cs=13, rst=9, tocard_retries=10) + task = asyncio.create_task(get_tag_uid(reader)) + + uid = await task + + return f"result={uid_to_string(uid)}" + app.run(port=80)