This project runs on an ESP32 and communicates with an external RFID reader over a UART (Serial1). When a known tag (EPC) is read the ESP32 publishes a message to an MQTT broker.
- Send inventory requests to an RFID module and parse its frame-based responses
- Matches detected tags against configured EPCs and publishes MQTT messages
- Uses WiFi and PubSubClient (MQTT) for reporting
- MCU: ESP32 (any board with Serial1 and GPIOs available)
- RFID reader module with UART interface (frame-based protocol)
- Connections: power, ground, UART TX/RX, optional enable pin
The code initializes Serial1 as:
Serial1.begin(115200, SERIAL_8N1, 16, 17);That means Serial1 RX is GPIO16 and TX is GPIO17. Wire the RFID module like this:
- RFID TX -> ESP32 GPIO16 (Serial1 RX)
- RFID RX -> ESP32 GPIO17 (Serial1 TX)
- RFID VCC -> 3.3V (confirm module voltage)
- RFID GND -> ESP32 GND
- RFID ENABLE (if present) -> ESP32 GPIO4 (named
PIN_ENin code) — the code drives this HIGH to enable the module
Adjust wiring if your module or board uses different pins.
src/main.cpp:- Builds and sends an inventory request frame (
singleRead) to the RFID module - Waits for a response and parses a custom frame format (header, type, length, RSSI, PC, EPC, CRC, end)
- Compares the EPC payload against three example EPCs (
epc_book1,epc_book2,epc_book3) and publishes a message to MQTT topicilham/testwith a human-readable message - Connects to WiFi using
ssidandpasswordconstants and to an MQTT broker atmqtt_server
- Builds and sends an inventory request frame (
Edit the following variables in src/main.cpp to match your environment:
ssid/password: WiFi credentialsmqtt_server: MQTT broker IP or hostnameepc_book1,epc_book2,epc_book3: example EPC arrays (replace with your tags)PIN_EN(default 4): GPIO used to enable the RFID module- Timing and frame constants near the top of the file
- Open the project folder
RMT01in VS Code with the PlatformIO extension. - Build:
pio run- Upload (connect your ESP32 via USB):
pio run -t upload- Monitor serial output (the sketch prints on
Serialat 9600 baud):
pio device monitor -b 9600The code uses Serial1 to communicate with the RFID module and Serial for logging.
By default the sketch publishes to topic ilham/test.
- To see messages with mosquitto client (on another machine in the network):
mosquitto_sub -h 192.168.137.1 -t ilham/test -vReplace 192.168.137.1 with the value of mqtt_server in the code.
Starting RFID reader...
Sending power setting frame...
Connecting to WiFi...
...Connected to WiFi
Received frame:
- Header: BB
- Frame type: 02
- Command: 22
- Parameter length: 00 0B
- RSSI: 5A
- Tag EPC: 21 05 00 00 00 00 00 00 00 00 00 01 00 00
Book detected: The Great Gatsby
- Add or remove EPCs and mapped strings by editing the arrays and
ifchecks inloop(). - The code contains commented examples for getting/setting transmit power frames. Use them only after verifying your module's command set.
- Timeout: If
Timeout: No data received.appears, check wiring, module power and that the module uses the same baud/frame format. - CRC errors: The parser computes and checks CRC; mismatches may indicate the wrong framing, corrupted bytes, or different protocol variant.
- WiFi/MQTT: Verify broker IP, network reachability, and credentials.
MIT — feel free to reuse and adapt. Please verify licensing if you incorporate third-party libraries.
- Electron.id