# Esp8266 Rc522


# Tested ESP8266 with RC522 NFC/RFID Reader

RC522 is NFC/RFID reader, and it can be integrated into ESPHOME

- esphome configuration
  ```yaml
  # Example configuration entry
  http_request:

  output:
  - platform: esp8266_pwm
    pin: 5
    id: my_buzzer

  rtttl:
    output: my_buzzer
    gain: 0.1

  spi:
    clk_pin: 14
    mosi_pin: 13
    miso_pin: 12

  rc522_spi:
    cs_pin: 15
    on_tag:
      then:
        - http_request.send:
            method: POST
            url: !secret nfc_tag_call_url
            verify_ssl: false
            headers:
              Content-Type: application/json
            json:
              tag: !lambda
                return x;
        - rtttl.play:
            # rtttl: 'siren:d=8,o=5,b=100:d,e,d,e,d,e,d,e'
            rtttl: 'two_short:d=4,o=5,b=100:16e6,16e6'

  binary_sensor:
    - platform: rc522
      uid: !secret my_room_card
      name: "My Room Card"
  ```

- Two separate function
  - `binary_sensor` is used by `Home Assistant` (see below)
  - `rc522_spi.on_tag` : when nfc card is tagged, we call two services
    - `http_request.send` : send to the server for additional automation
    - `rtttl.play` : will play buzzer sound for user to recognize the touch has been read

## Home automation

- Security on
  ```yaml
  alias: my-room-card-tagged-securityon
  description: ""
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.my_room_card
      from: "off"
      to: "on"
  condition:
    - condition: state
      entity_id: automation.body_detected
      state: "off"
  action:
    - service: automation.turn_on
      target:
        entity_id:
          - automation.body_detected
      data: {}
    - service: tts.google_translate_say
      data:
        cache: false
        entity_id: media_player.living_room_speaker
        message: Turned on security
  mode: single
- Security off
  ```yaml
  alias: my-room-card-tagged-securityoff
  description: ""
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.my_room_card
      from: "off"
      to: "on"
  condition:
    - condition: state
      entity_id: automation.body_detected
      state: "on"
  action:
    - service: automation.turn_off
      target:
        entity_id:
          - automation.body_detected
      data:
        stop_actions: true
    - service: tts.google_translate_say
      data:
        cache: false
        entity_id: media_player.living_room_speaker
        message: Turned off security
  mode: single
  ```

## Look

![D1 Mini Pro and RC522 Sensor](/uploads/2024-esp8266-rc522/IMG_6397.jpg)
![D1 Mini Pro and RC522 Sensor](/uploads/2024-esp8266-rc522/IMG_6595.jpg)

## Reference

- https://esphome.io/components/binary_sensor/rc522.html

