What is RC522 and why connect it to an AI agent
The RC522 RFID module has long become the standard for low-cost access control, time tracking, and inventory systems. But for this data to turn into smart actions—such as automatically opening a door, recording an employee's arrival, or writing off goods from a warehouse—integration with logic is needed. ASI Biont, an AI agent for automation, can connect to the RC522 directly (via COM port, MQTT, or Python scripts) and take on the role of the "brain": analyzing events, making decisions, and sending commands to other systems. In this article, we'll look at practical scenarios, wiring diagrams, and code examples.
Ways to integrate RC522 with ASI Biont
Depending on where the RC522 is located and how your system is structured, there are three main options:
| Method | Intermediate device | Speed | Complexity |
|---|---|---|---|
| COM-port (Hardware Bridge) | Arduino/STM32 | 10–50 ms | Low |
| MQTT | ESP32 + Wi-Fi | 20–100 ms | Medium |
| execute_python (direct) | Raspberry Pi where ASI Biont runs | 1–5 ms | High (requires GPIO access) |
1. COM port via Hardware Bridge
Hardware Bridge (bridge.py) is a utility from ASI Biont that is downloaded from the dashboard and run on a computer connected to the COM port. In our case, it's an Arduino with firmware that reads the RC522 and sends the card number to the port. The Bridge transmits this data to ASI Biont over a secure connection.
Advantages: no need to configure Wi-Fi, works with any microcontroller, minimal latency.
2. MQTT via ESP32
If the RC522 is connected to an ESP32, it can publish data to an MQTT broker itself. ASI Biont supports MQTT via the paho-mqtt library and can subscribe to topics. This method is convenient for distributed systems where access points are spread across different rooms.
3. execute_python — a universal method
ASI Biont can execute Python code in a sandbox. You simply describe in chat that you have an RC522 on a Raspberry Pi connected via SPI, and the AI itself will write a script using spidev and mfrc522. This method is suitable for prototyping and for integrating with any non-standard device.
Step-by-step scenario: access control via COM port
Imagine you need to set up a checkpoint in an office. An employee holds a card to the reader, ASI Biont checks it against the database, and if access is granted, sends a signal to open the door (via a relay). Here's how to implement it.
Step 1. Connecting RC522 to Arduino
Wiring diagram: RC522 → Arduino:
- SDA (SS) → D10
- SCK → D13
- MOSI → D11
- MISO → D12
- IRQ → not used
- GND → GND
- RST → D9
- 3.3V → 3.3V
Note: The RC522 operates at 3.3V; do not connect it to 5V.
Step 2. Arduino firmware
The sketch below reads the card UID and sends it to the serial port in the format CARD:<UID>. If necessary, you can add an exit button.
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup() {
Serial.begin(115200);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Ready");
}
void loop() {
if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
return;
}
String uid = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
uid += String(mfrc522.uid.uidByte[i], HEX);
if (i < mfrc522.uid.size - 1) uid += ":";
}
Serial.println("CARD:" + uid);
mfrc522.PICC_HaltA();
delay(500);
}
Step 3. Setting up Hardware Bridge
Download bridge.py from the ASI Biont dashboard (section "Hardware Bridge"). Run it with the following parameters:
python bridge.py --token=YOUR_TOKEN --ports=COM3 --baud 115200 --rate=10
Here, --ports is the port to which the Arduino is connected, and --rate is the polling frequency in Hz. The Bridge will send all lines received from the COM port to ASI Biont.
Step 4. Creating a command in ASI Biont
Now, in the ASI Biont chat, you need to describe the task. For example:
Connect to my Hardware Bridge, listen to the COM port. If a line CARD:... arrives, check this card against the access table (file access.csv). If the card is in the list, send the command "OPEN" to the Arduino (via the same COM port) to open the relay.
ASI Biont will write and execute the script itself. It will look something like this (approximate code):
```python
import serial
import time
import requests
Read access file
try:
with open('access.csv') as f:
allowed = [line.strip() for line in f]
except:
allowed = []
ser = serial.Serial('COM3', 115200, timeout=0.1)
Main loop — but ASI Biont uses an event-driven model through industrial_command()
So we use a command to read data
def on_card(uid):
if uid
in allowed:
ser.write(b'OPEN')
return
Comments