canonical / matter-pi-gpio-commander

Matter Raspberry Pi GPIO Commander - Turn your Pi into a Matter lighting device!
Apache License 2.0
90 stars 3 forks source link

Sync GPIO output with the known state #14

Closed farshidtz closed 11 months ago

farshidtz commented 1 year ago

Sometimes it takes a few toggle commands to turn on the LED. The actual GPIO state is not recorded by the application upon startup.

To manually bring the device state with the stored state in sync: sudo chip-tool onoff on <node-id> 1 && sudo chip-tool onoff off <node-id> 1

farshidtz commented 1 year ago

The app initializes the stored state to on however the actual state is often off by default. On Pi 3 with 64bits Ubuntu Core 22, the GPIO state is off on boot.

https://github.com/canonical/matter-pi-gpio-commander/blob/e1b32b9365acc05bcb1057d1e2a217bc5cf0d86e/app/src/LightingManager.cpp#L35

This prevents turning the GPIO to on after startup because the actuation code ignores a command that matches the stored state. It takes an off followed by an on command to work around it.

Need to check if this is consistent across different platforms. If yes, changing the default stored state to off make it better since the stored state will be synchronized after boot. But it will go out of sync again if the application is restarted when the output is set to on.

Another solution is to initialize both the actual and stored states upon startup. This may have undesired output changes in production upon application restarts.

A better solution may be to read the actual state upon startup and synchronize the stored state accordingly. See int digitalRead(int pin).

farshidtz commented 1 year ago

There may be something else going on because an initial off request also doesn't get passed through:

2023-04-21T11:02:13Z matter-pi-gpio-commander.lighting[6906]: [1682074933.334696][6906:6906] CHIP:ZCL: On/Off set value: 1 0
2023-04-21T11:02:13Z matter-pi-gpio-commander.lighting[6906]: [1682074933.334757][6906:6906] CHIP:ZCL: On/off already set to new value
farshidtz commented 11 months ago

PR #15 made the change to initialize both the actual and stored state to high/on. This is the typical behavior on smart lamps. However, it doesn't seem to work as expected. Upon startup, the toggle or off commands do not turn off the turned-on device; see https://github.com/canonical/matter-pi-gpio-commander/pull/15#pullrequestreview-1406036347

MonicaisHer commented 11 months ago

The current behavior is that the initial run turns the GPIO to ON, but the record from matter-pi-gpio-commander's log is OFF. Here is the log:

matter-pi-gpio-commander.lighting[2880]: [1690302940.282970][2880:2880] CHIP:ZCL: On/Off set value: 1 0
matter-pi-gpio-commander.lighting[2880]: [1690302940.283016][2880:2880] CHIP:ZCL: On/off already set to new value

This log (On/Off set value: <Endpoint> <Command>) indicates that the application received a initial command for Endpoint=1, Command=0x0000_0000 (OFF), recording this file, the initial command is equivalent to sending an OFF command.

This mismatch between GPIO being ON and the initial command being OFF should be the reason the following first toggle doesn't seem to work, but in reality, it does work because it performs an ON action, as the initial record is OFF.

A potential solution could be to send an ON command upon initialization to the application as Endpoint=1 Cluster=0x0000_0006(On/Off) Command=0x0000_0001.

farshidtz commented 11 months ago

A default value of 0 is set in the matter file here for the onOff attribute. This may be the reason the initial state is set to off.