【Homeassistant 与RC522 RFID握手】

【Homeassistant 与RC522 RFID握手】

  • 1. RC522 RFID
  • 2. 组件/集线器
    • 2.1 通过 SPI
    • 2.2 通过 I²C
    • 2.3 on_tag行动
    • 2.4 接线
  • 3. SPI例程
    • 3.1 配置变量:
    • 3.2 设置标签
  • 4. 总结

1. RC522 RFID

该rc522组件允许您将 RC522 RFID 控制器(数据表,Ali Express)与 ESPHome 一起使用。ESPHome 可以从中读取标签 UID,每个 RFID 标签都有一个唯一的 UID 值。每个已知标签都可以关联到一个二进制传感器,或者您可以直接使用标签信息。有关如何为此组件设置单个二进制传感器的信息,请参阅设置标签。RC522 支持 SPI、I²C 和 UART 通信协议,ESPHome 可以使用 SPI 或 I²C。

2. 组件/集线器

如果你有一个像上图这样的模块,它只能在 SPI 模式下使用(除非被黑客入侵),你需要在你的配置中有一个SPI 总线,同时设置miso_pin和mosi_pin。

如果您有一个通过 I²C 进行通信的 RC522,就像在 M5 堆栈中一样,那么您需要配置一个I²C总线。

【Homeassistant 与RC522 RFID握手】_第1张图片

2.1 通过 SPI

该rc522_spi组件允许您将 RC522 RFID 控制器与 ESPHome 一起使用。该组件是一个全局集线器,通过SPI(也可通过 I²C 获得)建立与 RC522 的连接。使用 RC522 二进制传感器,您可以创建单独的二进制传感器来跟踪 RC522 当前是否检测到 RFID 标签。

spi:

rc522_spi:
  cs_pin: GPIO15

binary_sensor:
  - platform: rc522
    uid: 74-10-37-94
    name: "RC522 RFID Tag"

配置变量:
cs_pin (必需, Pin Schema ): 芯片选择线连接到的 ESP 上的引脚。

spi_id ( Optional , ID ):如果要使用多个 SPI 总线,手动指定SPI 组件的 ID。

on_tag(可选,自动化):读取标签时执行的自动化。请参阅 on_tag 操作。

reset_pin ( Optional , Pin Schema ): 连接到 RST 线的引脚。一些测试表明没有这个 RC522 工作正常。

update_interval ( Optional , Time ): RC522 上每次扫描的持续时间。这会影响单个二进制传感器在被发现时保持活动的持续时间。如果在此时间窗口内未找到设备,则将其标记为不存在。默认为1s.

id ( Optional , ID ):手动指定该组件的 ID。

2.2 通过 I²C

该rc522_i2c组件允许您将 RC522 RFID 控制器与 ESPHome 一起使用。该组件是一个全局集线器,通过I²C(也可通过 SPI 获得)建立与 RC522 的连接。使用 RC522 二进制传感器,您可以创建单独的二进制传感器来跟踪 RC522 当前是否检测到 RFID 标签。

i2c:

rc522_i2c:

binary_sensor:
  - platform: rc522
    uid: 74-10-37-94
    name: "RC522 RFID Tag"

配置变量:
address ( Optional , int):手动指定传感器的 I²C 地址。默认为0x28.

i2c_id ( Optional , ID ):如果要使用多个 I²C 总线,请手动指定I²C 组件的 ID。

on_tag(可选,自动化):读取标签时执行的自动化。请参阅 on_tag 操作。

reset_pin ( Optional , Pin Schema ): 连接到 RST 线的引脚。一些测试表明没有这个 RC522 工作正常。

update_interval ( Optional , Time ): RC522 上每次扫描的持续时间。这会影响单个二进制传感器在被发现时保持活动的持续时间。如果在此时间窗口内未找到设备,则将其标记为不存在。默认为1s.

id ( Optional , ID ):手动指定该组件的 ID。

2.3 on_tag行动

当 RC522 模块以标签响应时,将触发此自动化。请注意,这可以经常调用(间隔为update_interval),因为如果标签被多次重新读取,它会被重复触发。

x此触发器提供的参数是类型std::string,是格式中的标签 UID 74-10-37-94。例如,下面的配置将在 MQTT 主题上发布标签 ID rc522/tag。

rc522_spi: # or rc522_i2c
  # ...
  on_tag:
    then:
      - mqtt.publish:
          topic: rc522/tag
          payload: !lambda 'return x;'

标签扫描事件也可以使用homeassistant.tag_scanned Action发送到 Home Assistant 标签组件。

rc522_spi: # or rc522_i2c
  # ...
  on_tag:
    then:
      - homeassistant.tag_scanned: !lambda 'return x;'

rc522二进制传感器
rc522二进制传感器平台可让您跟踪具有给定唯一 ID ( ) 的 RFID 标签当前uid是否被 RC522 检测到。

2.4 接线

采用esp8266

clk_pin: D0
miso_pin: D1
mosi_pin: D2
cs_pin: D3
reset_pin: D4

在这里插入图片描述

3. SPI例程

spi:
  clk_pin: D0
  miso_pin: D1
  mosi_pin: D2

rc522_spi: # or rc522_i2c
  cs_pin: D3
  update_interval: 1s

binary_sensor:
  - platform: rc522
    uid: 74-10-37-94
    name: "RC522 RFID Tag"

3.1 配置变量:

uid(必需,字符串):RFID 标签的唯一 ID。这是一个以连字符分隔的十六进制值列表。例如74-10-37-94.

name(必需,字符串):二进制传感器的名称。

id ( Optional , ID ):手动指定用于代码生成的ID。

二进制传感器的所有其他选项。

3.2 设置标签

要为特定的 RFID 标签设置二进制传感器,您首先必须知道它们的唯一 ID。要获得这个 id,首先设置一个简单的 RC522 配置,没有像上面那样的任何二进制传感器。

esphome:
  name: nodemcu-test

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "ced33e5545b140bfcb5e681179b9bf33"

wifi:
  ssid: "J09 502"
  password: "qwertyuiop111" 

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Nodemcu-Test Fallback Hotspot"
    password: "xzlKdVsXRfIy"

captive_portal:
# Example configuration entry
spi:
  clk_pin: D0
  miso_pin: D1
  mosi_pin: D2

rc522_spi: # or rc522_i2c
  cs_pin: D3
  reset_pin: D4
  on_tag:
    then:
      - homeassistant.tag_scanned: !lambda 'return x;'
  update_interval: 1s



编译:

Linking /data/nodemcu-test/.pioenvs/nodemcu-test/firmware.elf
RAM:   [====      ]  39.9% (used 32688 bytes from 81920 bytes)
Flash: [====      ]  39.8% (used 415429 bytes from 1044464 bytes)
Building /data/nodemcu-test/.pioenvs/nodemcu-test/firmware.bin
esp8266_copy_factory_bin(["/data/nodemcu-test/.pioenvs/nodemcu-test/firmware.bin"], ["/data/nodemcu-test/.pioenvs/nodemcu-test/firmware.elf"])
======================== [SUCCESS] Took 486.77 seconds ========================
INFO Successfully compiled program.
INFO Resolving IP address of nodemcu-test.local
INFO  -> 192.168.1.115
INFO Uploading /data/nodemcu-test/.pioenvs/nodemcu-test/firmware.bin (419584 bytes)
INFO Compressed to 292932 bytes
Uploading: [============================================================] 100% Done...

INFO Waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from nodemcu-test.local using esphome API
INFO Successfully connected to nodemcu-test.local
[21:17:42][I][app:102]: ESPHome version 2022.9.4 compiled on Oct  9 2022, 21:12:03
[21:17:42][C][wifi:502]: WiFi:
[21:17:42][C][wifi:360]:   Local MAC: 48:55:19:C8:BC:85
[21:17:42][C][wifi:361]:   SSID: [redacted]
[21:17:42][C][wifi:362]:   IP Address: 192.168.1.115
[21:17:42][C][wifi:363]:   BSSID: [redacted]
[21:17:42][C][wifi:365]:   Hostname: 'nodemcu-test'
[21:17:42][C][wifi:367]:   Signal strength: -49 dB ▂▄▆█
[21:17:42][C][wifi:371]:   Channel: 6
[21:17:42][C][wifi:372]:   Subnet: 255.255.255.0
[21:17:42][C][wifi:373]:   Gateway: 192.168.1.1
[21:17:42][C][wifi:374]:   DNS1: 114.114.114.114
[21:17:42][C][wifi:375]:   DNS2: 1.2.4.8
[21:17:42][C][logger:275]: Logger:
[21:17:42][C][logger:276]:   Level: DEBUG
[21:17:42][C][logger:277]:   Log Baud Rate: 115200
[21:17:42][C][logger:278]:   Hardware UART: UART0
[21:17:42][C][spi:101]: SPI bus:
[21:17:42][C][spi:102]:   CLK Pin: GPIO16
[21:17:42][C][spi:103]:   MISO Pin: GPIO5
[21:17:42][C][spi:104]:   MOSI Pin: GPIO4
[21:17:42][C][spi:106]:   Using HW SPI: NO
[21:17:42][C][rc522:099]: RC522:
[21:17:42][C][rc522:108]:   RESET Pin: GPIO2
[21:17:42][C][rc522:110]:   Update Interval: 1.0s
[21:17:42][C][rc522:113]:   Tag 'RC522 RFID Tag'
[21:17:42][C][rc522_spi:021]:   CS Pin: GPIO0
[21:17:42][C][captive_portal:088]: Captive Portal:
[21:17:42][C][mdns:100]: mDNS:
[21:17:42][C][mdns:101]:   Hostname: nodemcu-test
[21:17:42][C][ota:089]: Over-The-Air Updates:
[21:17:42][C][ota:090]:   Address: nodemcu-test.local:8266
[21:17:42][C][ota:093]:   Using Password.
[21:17:42][C][api:138]: API Server:
[21:17:42][C][api:139]:   Address: nodemcu-test.local:6053
[21:17:42][C][api:143]:   Using noise encryption: NO

【Homeassistant 与RC522 RFID握手】_第2张图片

当您的代码正在运行并且您使用 RFID 标签接近 RC522 时,您应该会看到如下消息:

Found new tag ‘74-10-37-94’
然后复制此 id 并创建一个binary_sensor条目,如配置示例中所示。对每个标签重复此过程。

```cpp
esphome:
  name: nodemcu-test

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "ced33e5545b140bfcb5e681179b9bf33"

wifi:
  ssid: "J09 502"
  password: "qwertyuiop111" 

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Nodemcu-Test Fallback Hotspot"
    password: "xzlKdVsXRfIy"

captive_portal:
# Example configuration entry
spi:
  clk_pin: D0
  miso_pin: D1
  mosi_pin: D2

rc522_spi: # or rc522_i2c
  cs_pin: D3
  reset_pin: D4
  on_tag:
    then:
      - homeassistant.tag_scanned: !lambda 'return x;'
  update_interval: 1s

binary_sensor:
  - platform: rc522
    uid: D3-87-5C-1E
    name: "RC522 RFID Tag1"

  - platform: rc522
    uid: 21-3A-A4-26
    name: "RC522 RFID Tag2"

  - platform: rc522
    uid: E9-86-BC-6D
    name: "RC522 RFID Tag3"
  
  #update_interval: 2s

编译

Linking /data/nodemcu-test/.pioenvs/nodemcu-test/firmware.elf
RAM:   [====      ]  39.9% (used 32688 bytes from 81920 bytes)
Flash: [====      ]  39.8% (used 415429 bytes from 1044464 bytes)
Building /data/nodemcu-test/.pioenvs/nodemcu-test/firmware.bin
esp8266_copy_factory_bin(["/data/nodemcu-test/.pioenvs/nodemcu-test/firmware.bin"], ["/data/nodemcu-test/.pioenvs/nodemcu-test/firmware.elf"])
======================== [SUCCESS] Took 486.77 seconds ========================
INFO Successfully compiled program.
INFO Resolving IP address of nodemcu-test.local
INFO  -> 192.168.1.115
INFO Uploading /data/nodemcu-test/.pioenvs/nodemcu-test/firmware.bin (419584 bytes)
INFO Compressed to 292932 bytes
Uploading: [============================================================] 100% Done...

INFO Waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from nodemcu-test.local using esphome API
INFO Successfully connected to nodemcu-test.local
[21:17:42][I][app:102]: ESPHome version 2022.9.4 compiled on Oct  9 2022, 21:12:03
[21:17:42][C][wifi:502]: WiFi:
[21:17:42][C][wifi:360]:   Local MAC: 48:55:19:C8:BC:85
[21:17:42][C][wifi:361]:   SSID: [redacted]
[21:17:42][C][wifi:362]:   IP Address: 192.168.1.115
[21:17:42][C][wifi:363]:   BSSID: [redacted]
[21:17:42][C][wifi:365]:   Hostname: 'nodemcu-test'
[21:17:42][C][wifi:367]:   Signal strength: -49 dB ▂▄▆█
[21:17:42][C][wifi:371]:   Channel: 6
[21:17:42][C][wifi:372]:   Subnet: 255.255.255.0
[21:17:42][C][wifi:373]:   Gateway: 192.168.1.1
[21:17:42][C][wifi:374]:   DNS1: 114.114.114.114
[21:17:42][C][wifi:375]:   DNS2: 1.2.4.8
[21:17:42][C][logger:275]: Logger:
[21:17:42][C][logger:276]:   Level: DEBUG
[21:17:42][C][logger:277]:   Log Baud Rate: 115200
[21:17:42][C][logger:278]:   Hardware UART: UART0
[21:17:42][C][spi:101]: SPI bus:
[21:17:42][C][spi:102]:   CLK Pin: GPIO16
[21:17:42][C][spi:103]:   MISO Pin: GPIO5
[21:17:42][C][spi:104]:   MOSI Pin: GPIO4
[21:17:42][C][spi:106]:   Using HW SPI: NO
[21:17:42][C][rc522:099]: RC522:
[21:17:42][C][rc522:108]:   RESET Pin: GPIO2
[21:17:42][C][rc522:110]:   Update Interval: 1.0s
[21:17:42][C][rc522:113]:   Tag 'RC522 RFID Tag'
[21:17:42][C][rc522_spi:021]:   CS Pin: GPIO0
[21:17:42][C][captive_portal:088]: Captive Portal:
[21:17:42][C][mdns:100]: mDNS:
[21:17:42][C][mdns:101]:   Hostname: nodemcu-test
[21:17:42][C][ota:089]: Over-The-Air Updates:
[21:17:42][C][ota:090]:   Address: nodemcu-test.local:8266
[21:17:42][C][ota:093]:   Using Password.
[21:17:42][C][api:138]: API Server:
[21:17:42][C][api:139]:   Address: nodemcu-test.local:6053
[21:17:42][C][api:143]:   Using noise encryption: NO
[21:17:47][D][rc522:263]: Found new tag 'D3-87-5C-1E'
[21:17:51][W][rc522:427]: only 2 valid bits received, tag distance to high? Error code is 0x0
[21:17:51][W][rc522:217]: Unexpected response. Read status is 2. Read bytes: 1 (93-70-D3-87-5C-1E-16-5E-03)
[21:17:51][D][api:102]: Accepted 192.168.1.109
[21:17:51][D][api.connection:917]: Home Assistant 2022.10.1 (192.168.1.109): Connected successfully
[21:18:50][D][rc522:263]: Found new tag 'D3-87-5C-1E'
[21:18:59][D][rc522:263]: Found new tag '21-3A-A4-26'
[21:19:04][D][rc522:263]: Found new tag 'E9-86-BC-6D'
[21:19:55][W][rc522:427]: only 2 valid bits received, tag distance to high? Error code is 0x0
[21:19:55][W][rc522:163]: CMD_REQA -> Not OK 2
[21:19:56][D][rc522:263]: Found new tag 'D3-87-5C-1E'
[21:20:42][D][rc522:263]: Found new tag 'D3-87-5C-1E'
[21:22:29][I][ota:109]: Boot seems successful, resetting boot loop counter.
[21:26:02][D][rc522:263]: Found new tag 'D3-87-5C-1E'
[21:26:06][D][rc522:263]: Found new tag '21-3A-A4-26'
[21:26:09][W][rc522:427]: only 1 valid bits received, tag distance to high? Error code is 0x0
[21:26:09][W][rc522:163]: CMD_REQA -> Not OK 2
[21:26:56][D][rc522:263]: Found new tag 'E9-86-BC-6D'
[21:26:58][W][rc522:427]: only 2 valid bits received, tag distance to high? Error code is 0x0
[21:26:58][W][rc522:163]: CMD_REQA -> Not OK 2
[21:27:00][D][rc522:263]: Found new tag 'E9-86-BC-6D'
[21:32:41][D][ota:143]: Starting OTA Update from 192.168.1.109...
[21:32:41][D][ota:312]: OTA in progress: 0.3%
[21:32:42][D][ota:312]: OTA in progress: 26.8%
[21:32:43][D][ota:312]: OTA in progress: 53.0%
[21:32:44][D][ota:312]: OTA in progress: 77.0%
[21:32:45][I][ota:345]: OTA update finished!
[21:32:46][I][app:134]: Rebooting safely...
INFO Disconnected from ESPHome API for nodemcu-test.local
WARNING Disconnected from API
INFO nodemcu-test.local: Ping timed out!
INFO Successfully connected to nodemcu-test.local
[21:33:06][D][api:102]: Accepted 192.168.1.109
[21:33:06][D][api.connection:917]: ESPHome Logs 2022.9.4 (192.168.1.109): Connected successfully
[21:33:06][I][app:102]: ESPHome version 2022.9.4 compiled on Oct  9 2022, 21:32:01
[21:33:06][C][wifi:502]: WiFi:
[21:33:06][C][wifi:360]:   Local MAC: 48:55:19:C8:BC:85
[21:33:06][C][wifi:361]:   SSID: [redacted]
[21:33:06][C][wifi:362]:   IP Address: 192.168.1.115
[21:33:06][C][wifi:363]:   BSSID: [redacted]
[21:33:06][C][wifi:365]:   Hostname: 'nodemcu-test'
[21:33:06][C][wifi:367]:   Signal strength: -51 dB ▂▄▆█
[21:33:06][C][wifi:371]:   Channel: 6
[21:33:06][C][wifi:372]:   Subnet: 255.255.255.0
[21:33:06][C][wifi:373]:   Gateway: 192.168.1.1
[21:33:06][C][wifi:374]:   DNS1: 114.114.114.114
[21:33:06][C][wifi:375]:   DNS2: 1.2.4.8
[21:33:06][C][logger:275]: Logger:
[21:33:06][C][logger:276]:   Level: DEBUG
[21:33:06][C][logger:277]:   Log Baud Rate: 115200
[21:33:06][C][logger:278]:   Hardware UART: UART0
[21:33:06][C][spi:101]: SPI bus:
[21:33:06][C][spi:102]:   CLK Pin: GPIO16
[21:33:06][C][spi:103]:   MISO Pin: GPIO5
[21:33:06][C][spi:104]:   MOSI Pin: GPIO4
[21:33:06][C][spi:106]:   Using HW SPI: NO
[21:33:06][C][rc522:099]: RC522:
[21:33:07][C][rc522:108]:   RESET Pin: GPIO2
[21:33:07][C][rc522:110]:   Update Interval: 1.0s
[21:33:07][C][rc522:113]:   Tag 'RC522 RFID Tag1'
[21:33:07][C][rc522:113]:   Tag 'RC522 RFID Tag2'
[21:33:07][C][rc522:113]:   Tag 'RC522 RFID Tag3'
[21:33:07][C][rc522_spi:021]:   CS Pin: GPIO0
[21:33:07][C][captive_portal:088]: Captive Portal:
[21:33:07][C][mdns:100]: mDNS:
[21:33:07][C][mdns:101]:   Hostname: nodemcu-test
[21:33:07][C][ota:089]: Over-The-Air Updates:
[21:33:07][C][ota:090]:   Address: nodemcu-test.local:8266
[21:33:07][C][ota:093]:   Using Password.
[21:33:07][C][api:138]: API Server:
[21:33:07][C][api:139]:   Address: nodemcu-test.local:6053
[21:33:07][C][api:143]:   Using noise encryption: NO

【Homeassistant 与RC522 RFID握手】_第3张图片

4. 总结

非常感谢各位大佬的支持,特别是ESPhome,到这里Homeassistant 与RC522 RFID握手就算完成了。大家快去探索读卡的乐趣吧!我们实现对外部世界进行感知,充分认识这个有机与无机的环境,科学地合理地进行创作和发挥效益,然后为人类社会发展贡献一点微薄之力。‍♂️‍♂️‍♂️

参考文献:
RC522 RFID
PN532 NFC/RFID

你可能感兴趣的:(HomeAssistant,homeassistant,RC522,RFID)