esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
290 stars 35 forks source link

Cannot register multiple RFID readers (RC522) on SPI bus - "SPI device already registered" #4961

Closed raphipaffi closed 12 months ago

raphipaffi commented 12 months ago

The problem

I connected two RFID readers (RC522) to the SPI bus of a NodeMCU and use the rc522_spi component in the yaml to configure them. One reader uses D8 for chip select, the other D4. The yaml compiles without any warnings but in the runtime logs I see:

[08:19:09][I][rc522_spi:013]: SPI Setup [08:19:09][D][spi_device:354]: mode 0, data_rate 4000kHz [08:19:09][E][spi:021]: SPI device already registered

The RC522 component keeps soft-resetting and repeats the SPI setup always ending in the same error message. When I only configure a single device in the yaml, everything works perfectly fine. So, the issue is only visible, when I have multiple rc522_spi devices configured.

Despite using different ids for both readers, it seems like the code that registers SPIDevices to the SPI bus references the same object for both readers. (Here is the line that logs the error in the spi component.)

I am not familiar with esphome code generation, yet, but it seems, the ids of the readers are used here to generate handles for the different instances (var), run the setup of rc522, and register them as spi devices. (Does cg.new_Pvariable(config[CONF_ID]) generate a new pointer to a RC522Spi object? I couldn't find any documentation for the code generation so I am just guessing here.)

I compared this code to the code generation for a max6675 which is also an SPIDevice but I don't see anything suspicious in the rc522_spi code. I have no idea why this doesn't work.

Any help is highly appreciated!

Which version of ESPHome has the issue?

2023.9.1

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

2023.9.1

What platform are you using?

ESP8266

Board

nodemcu

Component causing the issue

rc522_spi

Example YAML snippet

esphome:
  name: rc522test
  friendly_name: rc522test

esp8266:
  board: nodemcuv2

logger:

api:
  encryption:
    key: "***"

ota:
  password: "***"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: "rc522test Fallback Hotspot"
    password: "***"

captive_portal:

spi:
  clk_pin: D5
  miso_pin: D6
  mosi_pin: D7

rc522_spi:
  - id: "rfidreader1"
    cs_pin: D8
    update_interval: 1s
    on_tag:
      then:
        - homeassistant.tag_scanned: !lambda 'return x;'
  - id: "rfidreader2"
    cs_pin: D4
    update_interval: 1s
    on_tag:
      then:
        - homeassistant.tag_scanned: !lambda 'return x;'

Anything in the logs that might be useful for us?

...
[08:27:12][I][rc522:303]: Device online.
[08:27:13][W][rc522:398]: Communication with the MFRC522 might be down, reset in -229
[08:27:13][I][rc522_spi:013]: SPI Setup
[08:27:13][D][spi_device:354]: mode 0, data_rate 4000kHz
[08:27:13][E][spi:021]: SPI device already registered
[08:27:13][I][rc522:295]: Soft reset...
[08:27:13][I][rc522:303]: Device online.
[08:27:14][W][rc522:398]: Communication with the MFRC522 might be down, reset in -230
[08:27:14][I][rc522_spi:013]: SPI Setup
[08:27:14][D][spi_device:354]: mode 0, data_rate 4000kHz
[08:27:14][E][spi:021]: SPI device already registered
[08:27:14][I][rc522:295]: Soft reset...
...

Additional information

No response

raphipaffi commented 12 months ago

Ok, I finally found the issue: One of the connecting wires had a lose contact 😂 Everything is working as expected now. No issue with esphome.