Hello all. I am building a smart mirror and one of the requirements is that the display turns on very quickly but is otherwise off. To accomplish this, I have connected my RPI4 (4GB) to the monitor via HDMI and I have a small script that allows me to turn it off and on via `cec-client`. This works flawlessly (code is below).
Unfortunately, there is a random issue: specifically when the system has been left alone for a bit, it will randomly turn the screen back on.
I don't know why. Here are the clues:
So in general, does anyone know what's going on here? My primary two goals are:
Here is my code for the cec-control (via mqtt) if it is relevant:
Unfortunately, there is a random issue: specifically when the system has been left alone for a bit, it will randomly turn the screen back on.
I don't know why. Here are the clues:
- When the screen turns itself back on, the (chromium) browser window is no longer open in full screen
- When the screen turns itself back on, the mouse cursor is positioned directly in the center of the display
- This behavior only seems to happen after it has been left alone for a while
- It does not turn itself back on if I set the screen to standby using the remote rather than the `cec-client` command, but I can still turn it on via the command
- Disabling screen blanking by adding `xset s off`, `xset -dpms`, and `xset s noblank` in the `/etc/xdg/lxsession/LXDE-pi/autostart` file.
- Disabling screen blanking in the GUI display config
So in general, does anyone know what's going on here? My primary two goals are:
- Make the screen stay off and disable any chance at waking when it shouldn't be on
- Have the graphics be in the same state (fullscreen, mouse position, etc) when it turns on
Here is my code for the cec-control (via mqtt) if it is relevant:
Code:
import paho.mqtt.client as mqttimport subprocessimport timeimport os# MQTT ConfigurationMQTT_BROKER = "my-mqtt-broker"MQTT_PORT = 1883MQTT_COMMAND_TOPIC = "smartmirror/command"# CEC ConfigurationCEC_DEVICE_ADDRESS = "0.0.0.0"# Function to initialize CEC connectiondef initialize_cec_connection(): try: print("Initializing CEC connection...") cec_process = subprocess.Popen( ['cec-client', '-d', '1'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) # Sleep to give cec-client some time to initialize time.sleep(2) return cec_process except Exception as e: print(f"Error initializing CEC connection: {e}") return None# Function to send power command via CECdef send_cec_command(cec_process, command): if cec_process: try: if command.lower() == "on": cec_process.stdin.write(f'on {CEC_DEVICE_ADDRESS}\n') elif command.lower() == "off": cec_process.stdin.write(f'standby {CEC_DEVICE_ADDRESS}\n') else: print(f"Unknown command received: {command}") return cec_process.stdin.flush() print(f"Sent command: {command}") except Exception as e: print(f"Error sending CEC command '{command}': {e}")# MQTT Callbacksdef on_connect(client, userdata, flags, rc): print(f"Connected to MQTT Broker with result code {rc}") client.subscribe(MQTT_COMMAND_TOPIC)def on_message(client, userdata, msg): command = msg.payload.decode() print(f"Received MQTT message: {command}") send_cec_command(userdata['cec_process'], command)# Main functiondef main(): # Initialize CEC connection cec_process = initialize_cec_connection() if not cec_process: print("Failed to initialize CEC connection. Exiting.") return # Initialize MQTT client client = mqtt.Client(userdata={'cec_process': cec_process}) client.on_connect = on_connect client.on_message = on_message print("Connecting to MQTT broker...") client.connect(MQTT_BROKER, MQTT_PORT, 60) try: print("Starting MQTT loop...") client.loop_forever() except KeyboardInterrupt: print("Script interrupted by user.") finally: print("Disconnecting MQTT client...") client.disconnect() if cec_process: print("Terminating CEC process...") cec_process.stdin.close() cec_process.terminate() cec_process.wait() print("CEC process terminated.")if __name__ == "__main__": print("Starting CEC MQTT Control Script...") main()
Statistics: Posted by Marmoset_Threat — Tue Aug 27, 2024 5:17 pm — Replies 2 — Views 50