zorruno wikki: Controlling a Computer Remotely with MQTT

Controlling a Computer Remotely with MQTT


I was looking for various ways to control my computers via home automation, and my method of choice MQTT. Thinks like locking screens, rebooting etc. There are various python scripts around, and you can install node-red or other automation systems on each machine, but I wanted something simple and lightweight.

Software - mqcontrol

I found this to be the best option and simplest for my use case

Install (on a linux machine)

The documentation is good on the github page. As a summary, you can install with the go tools (it is written in go), but I just:
a) downloaded an appropriate binary (linux_amd64 in my case) from here https://github.com/albertnis/mqcontrol/releases
b) and put it in /usr/local/bin
c) renamed it mqcontrol
d) chmod 755 to make it executable

Making it run as a daemon with systemd (on a linux machine)

Again the documentation is good. There are a number of locations to put the systemd file, but recommendations I found were to put it in /etc/systemd/system/mqcontrol.service

This is the systemd unit file I used to have the screen lock on a machine. When mqcontrol is running, it listens to MQTT and awaits anything posted to the topic computercommands/lounge/lockscreen and will run the command we want.

-i is the MQTT client's ID (you can use anything)
-h is the MQTT server and port
-t is the topic you want to watch (anything at all posted to this topic will run the command, including)
-u and -p are the MQTT username/pass
dave is the local pc user that the command will be run as (see quirks below)

Description=mqcontrol remote control

ExecStart=/usr/local/bin/mqcontrol -i mqcloungepc -c "su - dave -c\"gnome-screensaver-command -l\"" -h 192.168.x.x:1883 -t computercommands/lounge/lockscreen -u mqttuser -p mqttpassword



When you activate systemctl start mqcontrol the first time, it will place a symlink of the systemd file in

you need to make it active with systemctl enable mqcontrol

If you change the unit file you need to run systemctl daemon-reload it will warn you if you haven't.

Start and stop the daemon with systemctl start mqcontrol and systemctl stop mqcontrol

Check to see if the daemon is still running with ps aux |grep mqcontrol. If the EXEC command fails, the daemon will die.

You can check the systemd log for that particular daemon with journalctl -u mqcontrol

Quirks of running as EXEC

The usual EXEC quirks come up, and I gave up trying to lock the user's screen as root. I tried many ways to export the display ID, use DBUS commands etc with no success. The easiest way in the end was to lock the screen of the user I know was logged in (ie me in this case). Therefore run the command as the user itself (no password needed as it is run as root).

I'd be interested in suggestions anyone may have to lock the gnome screens of all users logged in, but in most cases I'll know who the users are.

Any other suggestions here would be useful... maybe I should just run the entire daemon as myself as the user?

Use cases

Currently, I use it for locking screens on machines when I leave the room or turn out the lights. I have it integrated with google assistant and amazon alexa voice commands. I could equally hibernate or shut them down.

Another planned use will be to shut down some raspberry pis then have them boot up when needed with external power control (I have some that only need to run when other actions are occurring, and power is controlled via tasmota).

Other Notes

1) A docker setup is provided on the github site, and so are windows & mac binaries and details.

2) Just watch when you are sending MQTT commands to the topic... anything at all to the topic will activate the script (including NULL). You will need to to do some rerouting if you are trying to tie it in with other on/off commands

Last edited by ZorrUno
Thu, 27 May 2021 00:49 UTC [diff]