From RFID to AI: Integrating the RC522 Reader with ASI Biont for Smarter Access and Inventory

Introduction

If you’ve ever built a door-lock system with an Arduino or set up a tool-tracking station in a workshop, you’ve probably used the MFRC522 RFID module. It’s cheap (around $3), reliable, and works with 13.56 MHz MiFare cards and tags. But until recently, turning that raw card UID into a meaningful action — like logging a tool checkout or granting access to a lab — required writing a custom script, setting up a database, and maintaining a web interface.

That’s where ASI Biont changes the game. Instead of spending hours coding a back-end, you simply describe your RFID reader and what you want it to do in plain English. The AI agent writes the integration code, connects to your hardware via MQTT or a COM port bridge, and starts automating tasks in seconds. This article walks you through a real-world integration: connecting an RC522 reader to ASI Biont, reading card UIDs, and automating inventory tracking — all without writing a single line of boilerplate.

Why RC522 + AI?

The RC522 is a popular RFID module because of its low cost and ease of use. However, most hobbyist and industrial setups still rely on manual programming: you flash an ESP32 with Arduino code, parse serial data, and send it to a local server. With ASI Biont, the AI agent handles the entire pipeline — from device connection to data interpretation and action triggering. This means:
- No server maintenance. The AI runs in the cloud and communicates via MQTT or WebSocket.
- Natural language control. Tell the agent “When card UID 0x1234 is scanned, log the event and send me a Telegram message” — and it writes the logic.
- Extensibility. Connect multiple readers, add conditional rules (e.g., “only allow cards from list X between 9 AM and 5 PM”), or integrate with an existing database.

Connection Method: MQTT via ESP32

For this integration, we use an ESP32 microcontroller connected to the RC522 via SPI. The ESP32 runs MicroPython and publishes card UIDs to an MQTT broker (e.g., Mosquitto running on a local Raspberry Pi or a cloud service like HiveMQ). ASI Biont’s AI agent subscribes to the same MQTT topic using the industrial_command tool with protocol='mqtt'.

Why MQTT? Because it’s lightweight, supports publish/subscribe, and works over unreliable networks — ideal for a workshop or warehouse environment. The ESP32 connects to Wi-Fi and sends a JSON payload every time a card is tapped. The AI agent (running in the ASI Biont sandbox) receives the message, parses the UID, and executes a pre-defined action.

Wiring Diagram

RC522 Pin ESP32 GPIO
SDA GPIO5
SCK GPIO18
MOSI GPIO23
MISO GPIO19
IRQ Not used
GND GND
RST GPIO22
3.3V 3.3V

Note: The RC522 operates at 3.3V — do not connect it to 5V pins.

Step 1: MicroPython Code on ESP32

Flash your ESP32 with MicroPython (official firmware from micropython.org). Then upload the following script using a tool like ampy or Thonny. The script reads the card UID and publishes it to an MQTT topic.

import network
import time
from machine import Pin, SPI
from mfrc522 import MFRC522
from umqtt.simple import MQTTClient

# Wi-Fi credentials
SSID = "YourWiFi"
PASSWORD = "YourPassword"

# MQTT broker
BROKER = "192.168.1.100"  # Replace with your broker IP
TOPIC = b"rfid/uid"

# Initialize SPI and RC522
spi = SPI(2, baudrate=1000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
rst = Pin(22, Pin.OUT)
sda = Pin(5, Pin.OUT)
reader = MFRC522(spi, rst, sda)

def connect_wifi():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(SSID, PASSWORD)
    while not wlan.isconnected():
        time.sleep(1)
    print("Wi-Fi connected")

def main():
    connect_wifi()
    client = MQTTClient("esp32_rfid", BROKER)
    client.connect()
    print("Connected to MQTT broker")

    while True:
        (stat, tag_type) = reader.request(reader.REQIDL)
        if stat == reader.OK:
            (stat, uid) = reader.SelectTagSN()
            if stat == reader.OK:
                card_uid = "{:02x}{:02x}{:02x}{:02x}".format(uid[0], uid[1], uid[2], uid[3])
                payload = '{"uid": "' + card_uid + '", "timestamp": ' + str(time.time()) + '}'
                client.publish(TOPIC, payload)
                print("Published:", payload)
                time.sleep(1)  # debounce
        time.sleep(0.1)

main()

Note: The mfrc522 library for MicroPython can be found on PyPI or GitHub. Copy mfrc522.py to your ESP32 before running the script.

Step 2: AI Agent Integration via MQTT

Now, in the ASI Biont chat, tell the AI agent to listen for RFID events. You don’t write any code — the agent generates it. Describe the task like this:

“Connect to MQTT broker at 192.168.1.100:1883, subscribe to topic ‘rfid/uid’. When a message arrives, parse the JSON, extract the uid, and if the uid is ‘a1b2c3d4’, log the event to a file and send me a notification via Telegram.”

The agent will use the industrial_command tool with protocol='mqtt' to subscribe and process messages. Under the hood, it writes a Python script using paho-mqtt that runs in the sandbox. Here’s what that generated script looks like (for illustration):

import paho.mqtt.client as mqtt
import json

def on_message(client, userdata, msg):
    try:
        data = json.loads(msg.payload.decode())
        uid = data.get("uid")
        print(f"Card scanned: {uid}")
        if uid == "a1b2c3d4":
            # Log to a file (simulated)
            with open("/tmp/rfid_log.txt", "a") as f:
                f.write(f"{uid} at {data.get('timestamp')}\n")
            print("Authorized card — sending Telegram alert")
            # Telegram code omitted for brevity
    except Exception as e:
        print(f"Error: {e}")

client = mqtt.Client()
client.on_message = on_message
client.connect("192.168.1.100", 1883, 60)
client.subscribe("rfid/uid")
client.loop_forever()  # Note: sandbox timeout is 30s, so in production use loop_start()

Important: In the ASI Biont sandbox, loop_forever() will be stopped after 30 seconds. For production, the agent uses a non-blocking loop or a scheduled check.

Step 3: Automating Real-World Tasks

Once the AI agent can read card UIDs, you can build sophisticated automations:

Scenario Action
Tool crib checkout When a tool with RFID tag is scanned, log the employee ID (from a separate card) and tool ID. If the tool is not returned within 8 hours, send a Slack reminder.
Access control Maintain a whitelist of UIDs in a Google Sheet. The AI checks the sheet via HTTP API and unlocks a relay (connected to another ESP32 GPIO) if the UID is valid.
Inventory in/out Each item has an RFID tag. Scanning at a door reader increments or decrements a live count in a PostgreSQL database. Generate a weekly report of missing items.
Personalized greetings Combine RFID with a small OLED display (connected to the same ESP32) to show a welcome message when an employee taps their badge.

All of these can be configured through natural language in the ASI Biont chat. The AI writes the integration code, connects to your MQTT broker, and starts executing the logic immediately.

Alternative Connection: COM Port via Hardware Bridge

If you prefer a wired connection (e.g., an Arduino Uno + RC522 connected to a PC via USB), use the Hardware Bridge method. Run bridge.py on your computer with the COM port and baud rate, then tell the AI: “Connect via COM3 at 9600 baud. Read any ASCII string that starts with ‘UID:’ and parse the card number.” The AI sends commands through industrial_command(protocol='serial://', command='read', ...), and the bridge reads from the serial port. This is ideal for legacy systems or when Wi-Fi is unavailable.

Why This Matters

Traditional RFID integration requires:
- Setting up a database
- Writing a REST API
- Building a front-end for logs
- Debugging serial protocols

With ASI Biont, the AI agent does all that in seconds. You describe the hardware and the desired outcome; the agent generates the MicroPython firmware, the MQTT subscriber, the database schema, and even the notification logic. This drastically reduces time-to-automation from days to minutes.

Get Started

Ready to turn your RC522 into an AI-powered access or inventory system? Head over to asibiont.com, create an account, and tell the AI: “Connect my ESP32 with an RC522 reader via MQTT at broker 192.168.1.100. When a card with UID 0xABCD is scanned, log it to a file and notify me on Telegram.” The agent will handle the rest.

Conclusion

The RC522 RFID module is a perfect entry point for exploring AI-driven hardware integration. Its low cost and broad compatibility make it ideal for prototyping, while ASI Biont’s natural language interface removes the programming barrier. Whether you’re tracking tools in a makerspace, managing access to a server room, or automating inventory in a small warehouse, combining RFID with an AI agent gives you a flexible, scalable solution without writing boilerplate code.

Try it today — connect your RC522 to ASI Biont and see how fast you can automate your next project.

← All posts

Comments