Difference between revisions of "Linux - Screen Control"
Jump to navigation
Jump to search
| Line 1: | Line 1: | ||
{{{!}} class="wikitable" style="float: right; width: 322px;" border="2" | |||
{{!}}+ Proven on: | |||
{{!}}- <!-- Debian --> | |||
{{!}} style="text-align: center; width: 60px;" {{!}} [[File:Logo Debian.png{{!}}60px{{!}}link=https://www.debian.org/{{!}}center{{!}}middle{{!}}frameless]] | |||
{{!}} style="text-align: center; width: 40px;" {{!}} 12 (bookworm) | |||
{{!}} | |||
<br> | |||
{{!}}} | |||
{{{!}} class="wikitable" style="border-collapse: collapse; width: 33%; left;" | |||
{{!}}- style="text-align: center;" | |||
! style="width: 50%;" colspan="2" {{!}} As always... | |||
{{!}}- | |||
{{!}} {{!}} | |||
Start with: | |||
{{!}} {{!}} | |||
* <code>sudo apt install vbetool</code> | * <code>sudo apt install vbetool</code> | ||
The following script needs to run as root... | The following script needs to run as root... | ||
Revision as of 15:03, 24 June 2024
| 12 (bookworm) |
|
| As always... | |
|---|---|
|
Start with: |
The following script needs to run as root...
#!/usr/bin/env python
import paho.mqtt.client as mqttClient
import time
import os
import subprocess
import socket
###################################################################################################
hostname = socket.gethostname()
broker_address = "skynet"
port = 1883
topic = hostname + "/#"
user = "user"
password = "password"
client = mqttClient.Client(hostname + "control")
client.username_pw_set(user, password=password)
###################################################################################################
## Broker Connection ##
###################################################################################################
Connected = False
def on_connect(client, userdata, flags, rc):
global Connected
if rc == 0:
print(" HostName: ", hostname)
print("Connected to broker: ", broker_address)
print(" Subscribed to: ", topic)
print()
Connected =True
else:
print("Connection failed")
Connected =False
###################################################################################################
## Message Handling ##
###################################################################################################
def on_message(client, userdata, message):
print( " Received message: " + str(message.payload.decode("utf-8")))
print( " on topic: " + message.topic)
#print( " with QoS: " + str(message.qos))
time.sleep(1)
### Display control (turn the screen on or off...)
if message.topic == hostname + "/display":
command = "/usr/sbin/vbetool dpms " + str(message.payload.decode("utf-8"))
print( command )
os.system(command)
### Audio control (something for th future...)
elif message.topic == hostname + "/audio":
print("audio stuff")
### Not any sort of valid topic...
else:
print("Not Important to us...")
print()
###################################################################################################
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker_address, port=port)
client.loop_start()
while Connected != True:
time.sleep(0.1)
client.subscribe(topic)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print( "exiting" )
client.disconnect()
client.loop_stop()
This will respond to MQTT messages:
& works as a systemd service
[Unit]
Description=Watch for MQTT messages to turn screen on/off
After=multi-user.target
[Service]
ExecStart=
|