ebaauw / homebridge-rpi

Homebridge plugin for Raspberry Pi.
Apache License 2.0
305 stars 18 forks source link

Reversed State? #161

Closed bigbucketboy closed 1 year ago

bigbucketboy commented 1 year ago

Hello. I love this plug in first off. Thanks so much for your help. I did have a question though. I have a relay board that is a Raspberry Pi HAT that activates on low. So I checked the reversed option as a lock device and it works great. But for a quick second while it's setting it's state it drives the pin low first then switches it to high. During this time it fires the relay. I was wondering if there is anyway to skip this option and have it set it straight to high first? Is this possible without changing what GPIO pin I'm using or changing the boot-config.txt file? I like that these relays are driven by a low state so they don't trigger on a fresh raspberry pi boot or shutdown. I'm using GPIO pin 26. Here is the screenshot. Many thanks!!image

ebaauw commented 1 year ago

I don’t understand your question. Who is “it”? When does your problem occur, on booting the RPi, om starting Homebridge RPi, or when changing the lock state from HomeKit.

Note that GPIO 26 defaults to low when the RPi boots.

Your log lines just show Homebridge RPi updating the state in HomeKit, after the GPIO pin has changed, five seconds apart. No driving, no quick second.

Please set Log Level on the RPi accessory to 2, turn on Homebridge’s DEBUG mode, and capture and attach the Homebridge log file, from Homebridge startup to controlling the lock from HomeKit.

bigbucketboy commented 1 year ago

Sorry for not being thorough enough. I'm new to this platform so trying to get used to supplying the right info. "It" seems to be the Homebridge-RPi plug-in. The problem seems to occur after the pi boots and the Homebridge software loads. When it starts activating all the plugins. Once the Homebridge-RPi plug-in starts it then triggers the state change without any interaction whatsoever. I do not activate it in HomeKit. The plug-in seems to set it to low then change it to high and it stays high until I activate it in HomeKit. Behavior is normal after that. It is just during initial boot up sequence. Correct me if I'm wrong but the pin is low during boot but I thought all pins on the header are inputs until you change it otherwise. It is when Homebridge-RPi sets that pin as an output that it triggers this. It just triggers it low first and then when the reverse option is checked it changes it high. I was curious if Homebridge-RPi can set the pin straight to high so it doesn't trigger the relay on Homebridge startup. I will post logs next.

bigbucketboy commented 1 year ago

Here is the log you requested. Sorry for the delay had to get the info you needed. homebridge.log.txt

This just happens on a physical server shutdown. If I restart the homebridge server itself this action does not happen. Thanks

ebaauw commented 1 year ago

Once the Homebridge-RPi plug-in starts it then triggers the state change

No, it does not:

[15/05/2023, 11:04:37] [RPi] Server Room Strike: gpio 26: low
[15/05/2023, 11:04:37] [RPi] Server Room Strike: Lock Target State: set to 0 (from 1)
[15/05/2023, 11:04:38] [RPi] Server Room Strike: Lock Current State: set to 0 (from 1)

Not surprisingly, the GPIO is low when Homebridge RPi starts (as that's the default state for GPIO 26). Homebridge RPi does not set the pin low, it merely notices the state is low and updates HomeKit accordingly. The target state is set immediately, the current state is updated after 500ms.

This just happens on a physical server shutdown. If I restart the homebridge server itself this action does not happen.

Indeed, this is not an issue with Homebridge RPi.
See the Raspberry Pi documentation (linked from the Wiki) how to change the GPIO value on boot, or what GPIOs default at high on boot.

15/05/2023, 11:04:35] [RPi] config.json: {"name":"RPi","hosts":[{"host":"localhost:8888","name":"homebridge","hidden":false,"noPowerLed":true,"noSmokeSensor":true,"devices":[{"device":"lock","name":"Strike","gpio":26,"reversed":true,"pulse":5000}]}],"platform":"RPi"}

You have set pulse to 5000, so Homebridge RPi changes the state after 5 seconds.


15/05/2023, 11:04:42] [RPi] homebridge: command WRITE (4) 26 1 ""
[15/05/2023, 11:04:42] [RPi] homebridge: command WRITE (4) => 0
[15/05/2023, 11:04:42] [RPi] Server Room Strike: gpio 26: high
[15/05/2023, 11:04:42] [RPi] Server Room Strike: Lock Target State: set to 1 (from 0)
[15/05/2023, 11:04:43] [RPi] Server Room Strike: Lock Current State: set to 1 (from 0)
[```
bigbucketboy commented 1 year ago

Okay so I have two more comments then. My understanding is probably wrong. This relay board is by Waveshare. It has 3 relays on board. How come the other two relays don't activate at all? They use GPIO pins 20 and 21. I have nothing programmed to them in Homebridge or any other application but they never engage either? The only one that does is the one assigned in Homebridge-RPi. Is it because they all start as inputs until something tells them they need to be outputs? And secondly if I change the state of pin 26 at boot will it engage the relay once the pi powers all the way down then? Thanks again so much for your help.

ebaauw commented 1 year ago

Is it because they all start as inputs until something tells them they need to be outputs?

Probably. Homebridge RPi does change the GPIO mode to output on startup:

[15/05/2023, 11:04:37] [RPi] Server Room Strike: initialising GPIO 26: mode 1
[15/05/2023, 11:04:37] [RPi] homebridge: command MODES (0) 26 1 ""
[15/05/2023, 11:04:37] [RPi] homebridge: command MODES (0) => 0

They use GPIO pins 20 and 21.

According to the data sheet, GPIO 20 and 21 also default to low on power up.

How come the other two relays don't activate at all?

Apparently, the relay doesn’t mind the (default) input mode (with an internal pull-down resistor). I think the GPIO output defaults to low, when changing the mode. By design, Homebridge RPi doesn’t change the state on startup. In your case, it might make sense to do so, but generally you don’t want restarting Homebridge to change the state. Homebridge RPi doesn’t know whether the system just powered up, or whether Homebridge was simply restarted.

And secondly if I change the state of pin 26 at boot will it engage the relay once the pi powers all the way down then?

No, in config.txt, you would set the state to high, in addition to changing the mode.

bigbucketboy commented 1 year ago

This problem is solved. Sorry again for the confusion as I am new to learning this stuff. For anyone with this particular problem in the future, adding this one liner to the end/bottom of the /boot/config.txt file will do the trick.

"gpio=26=op,dh"

This sets the gpio pin 26 as an output high early in the boot process.

Thanks again @ebaauw !