kormax / apple-home-key-reader

Apple Home Key Reader Implementation
Apache License 2.0
470 stars 32 forks source link

Trouble Setting Up #1

Closed tomkinane closed 7 months ago

tomkinane commented 7 months ago

I'm struggling to set it up :/

I am using a raspberry pi 4 with the PN532 NFC module with GPIO: GND -> 6 VCC -> 4 SDA -> 3 SCL -> 5

I have followed your steps, but when I run python3 main.py I get back:

(venv) tom@HomeAssistant:~/apple-home-key-reader $ python3 main.py
[2023-11-26 22:54:20,449] [    INFO] __init__          :148  searching for reader on path tty:/dev/i2c-1:pn532
[2023-11-26 22:54:20,449] [   ERROR] __init__          :153  no reader available on path tty:/dev/i2c-1:pn532
Traceback (most recent call last):
  File "/home/tom/apple-home-key-reader/main.py", line 81, in <module>
    main()
  File "/home/tom/apple-home-key-reader/main.py", line 62, in main
    nfc_device = configure_nfc_device(config["nfc"])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tom/apple-home-key-reader/main.py", line 41, in configure_nfc_device
    clf = BroadcastFrameContactlessFrontend(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tom/apple-home-key-reader/util/bfclf.py", line 51, in __init__
    super().__init__(path)
  File "/home/tom/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/clf/__init__.py", line 76, in __init__
    raise IOError(errno.ENODEV, os.strerror(errno.ENODEV))
OSError: [Errno 19] No such device`

Then I tried sudo python3 main.py I get back

(venv) tom@HomeAssistant:~/apple-home-key-reader $ sudo python3 main.py
Traceback (most recent call last):
  File "/home/tom/apple-home-key-reader/main.py", line 6, in <module>
    from pyhap.accessory_driver import AccessoryDriver
ModuleNotFoundError: No module named 'pyhap

When I run nfc-scan-device -v I get

nfc-scan-device uses libnfc 1.8.0
1 NFC device(s) found:
- PN532 over I2C:
    pn532_i2c:/dev/i2c-1
chip: PN532 v1.6
initator mode modulations: ISO/IEC 14443A (424 kbps, 212 kbps, 106 kbps), FeliCa (424 kbps, 212 kbps), ISO/IEC 14443-4B (106 kbps), ISO/IEC 14443-4B' (106 kbps), ISO/IEC 14443-2B ST SRx (106 kbps), ISO/IEC 14443-2B ASK CTx (106 kbps), ISO/IEC 14443-2B-3B iClass (Picopass) (106 kbps), Innovision Jewel (106 kbps), Thinfilm NFC Barcode (106 kbps), D.E.P. (424 kbps, 212 kbps, 106 kbps)
target mode modulations: ISO/IEC 14443A (424 kbps, 212 kbps, 106 kbps), FeliCa (424 kbps, 212 kbps), D.E.P. (424 kbps, 212 kbps, 106 kbps)

My config file is

{
    "logging": {
        "level": 20
    },

    "nfc": {
        "port": "/dev/i2c-1",
        "driver": "pn532",
        "broadcast": true
    },

    "hap": {
        "port": 51926,
        "persist": "hap.state"
    },

    "homekey": {
        "persist": "homekey.json",
        "finish": "silver",
        "flow": "fast"
    }
}

I wondering is it me doing something wrong (More than likely me I'm doing this late at night - bad idea)

I'm thinking its the pn532 as when I run /dev/i2c-1 i get back

-bash: /dev/i2c-1: Permission denied
kormax commented 7 months ago

Port should be without the /dev/ prefix. Also, i2c is not supported by nfcpy.

For best compatibility, i advise using uart mode.

Also, libnfc could interfere with nfcpy if it's running at the same time.

In case you gonna use UART, remember to turn it on in raspi-config, and turn off serial console.

MohammadAG commented 7 months ago

I also had to turn off Bluetooth on my Pi Zero W. You can do that and enable UART by adding these lines to the end of your /boot/config.txt:

enable_uart=1
dtoverlay=pi3-disable-bt

and then restarting your Pi. If you get permission denied accessing the dev node (ttyAMA0 for me, AMA0 in configuration.json) then you might want to add your user to the dialout group.

tomkinane commented 7 months ago

I switched over to UART, turned off Bluetooth and added user to the dialout group all worked great, Could you point me in the direction to the file where I need test up the functionality of the lock?

kormax commented 7 months ago

I don't seem to understand the question. Can you clarify it for me?

tomkinane commented 7 months ago

Where would I add functionality like when the door opens (by taping the card) it runs a python code that would trigger my door system. So my question is where would I code this on to the lock code.

kormax commented 7 months ago

I would add this logic inside of on_endpoint_authenticated method inside of accessory.py. It's present in the most recent commit.

Beware that this is the code that nfc reader loop blocks on, so if I were you, i'd schedule a task to send an http/other request in asynchronous manner via threads or asyncio.