How can I programmatically detect if the Raspberry Pi's HDMI cable is removed? I've tried tools like xrandr, but they give the same output regardless of whether the HDMI cable is plugged in or not.
Asked
Active
Viewed 1,529 times
1 Answers
8
Try verifying the output of the file:
/sys/class/drm/card0-HDMI-A-1/status
The part card0-HDMI-A-1 may be a little different (verify the parent directory for the correct file) and the content should be connected or disconnected. Also works for another connections, like VGA, LVDS, etc.
After this, simply make a script like:
while [ 1 ]
do
STATUS=`cat /sys/class/drm/card0-HDMI-A-1/status`
if [ "$STATUS" == "disconnected" ]; then
echo "turning off"
sudo shutdown now
fi
#sleep for 10 seconds and check again
sleep 10
done
To run it automatically on background, follow this link.