Control remote pc from Openhab using MQTT

Last Updated on

To control remote computer from openhab we need following software:
MQTT broker (my choise is Mosquitto)
Openhab addons – MQTT binding and MQTT translation
optional – openhab cloud connector to use this with voice control as Google Home.

Mosquitto MQTT broker

Can be on any pc in the network. Whatever is better for your configuration.
In my case i am running it on the Raspberry Pi with Openhabian

install instructions:
get correct version of your debian:

cat /etc/*release*

Example output (here you can see jessie is the version of this debian):

PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
NAME="Raspbian GNU/Linux"
VERSION="8 (jessie)"

Getting newest version directly from Mosquitto reposittory

sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/

choose right repository
for debian release jessie

sudo wget

for debian release stretch

sudo wget

update repositories and install the mosquito

sudo apt-get update && sudo apt-get install mosquitto

Now create authentication for the Mosquitto. insert user name you want to use.

sudo mosquitto_passwd -c /etc/mosquitto/passwd USER_NAME

After issuing this command it will ask you to set Password.

Now edit moquitto configuration file

sudo vim /etc/mosquitto/mosquitto.conf

add following lines

password_file /etc/mosquitto/passwd
allow_anonymous false

and finally restart the mosquitto service

sudo systemctl restart mosquitto

external link
download link
Download the jar file under C:\WinThing (offcourse you can anywhere else just dont forget to change the path where needed)
create winthing.bat under C:\Winthing with following content. Change the IP of the broker server (where you installed the MQTT broker) username and password.

cd C:\WinThing\
java -Dwinthing.brokerUrl=tcp://IP_OF_MQTT_BROKER:1883 -Dwinthing.brokerUsername=USERNAME -Dwinthing.brokerPassword=PASSWORD -jar winthing-1.1.0.jar

To Start WinThing automatically with Windows create file StartWinthing.vbs with following content under %appdata%\Microsoft\Windows\Start Menu\Programs\Startup

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """" & "C:\Winthing\winthing.bat" & """" & sargs, 0, False
Set WshShell = Nothing

Inside the Folder i am also storing links or scripts which i want to run.
I am using AutoIT for scripting which i call from this folder by vbs scripts.
example script C:\Winthing\youtube.vbs

Set oShell = CreateObject ("Wscript.Shell") 
Dim strArgs
strArgs = """C:\Program Files (x86)\AutoIt3\AutoIt3.exe"" ""C:\Users\rhape\Dropbox\HomeAutomation\AutoIt scripts\WatchYoutube.au3"""
oShell.Run strArgs, 0, false

and the coresponding AutoIT script WatchYoutube.au3

If ProcessExists("kodi.exe") Then ; Check if the kodi process is running.
	;if kodi fullscreen minimize
    $aWin_Pos = WinGetPos("[ACTIVE]")
	If $aWin_Pos[2]&$aWin_Pos[3] = @DesktopWidth&@DesktopHeight And WinGetTitle("[ACTIVE]") <> "Program Manager" Then

Openhab addons

MQTT2 binding – install from Openhab PaperUI

Setup new MQTT broker in the PaperUI

Inbox -> Manually Add Thing -> MQTT binding -> MQTT Broker

And fill in settings. My example (localhost to IP if you installed the mqtt broker somewhere else then the Openhab installation):

Broker Hostname: localhost
Broker Port: 1883
Username: openhabian
Password: ******

Now the Broker should show online as Thing

copy the ID of MQTT broker Thing you just created ( mqtt:broker:12345678 )


String PCControl "Computer Control" (living_group)


Switch item=PCControl label="PC Control[]" icon="mediacontrol" mappings=[nhk="NHK", youtube="Youtube playlist", netflix="Netflix"]

in the following the mqtt:broker:81c98b85 is id of my broker in the PaperUI

rule (pccontrol.rules):

rule "rPCControl"
	Item PCControl received command
    logInfo("rule rPCControl", "command "+receivedCommand)
	val actions = getActions("mqtt","mqtt:broker:81c98b85")
	switch (receivedCommand) {
		case "nhk" : {
			actions.publishMQTT("winthing/system/commands/run", "[\"nhk.vbs\",\"\",\"\"]")
  		case "youtube" : {
			actions.publishMQTT("winthing/system/commands/run", "[\"youtube.vbs\",\"\",\"\"]")
		case "netflix" : {
			actions.publishMQTT("winthing/system/commands/run", "[\"Netflix.lnk\",\"\",\"\"]")
            // Wait 5 seconds and send WIN+SHIFT+ENTER to go Fullscreen
            actions.publishMQTT("winthing/keyboard/commands/press_keys", "[\"LWIN\",\"LSHIFT\",\"ENTER\"]")

This rule will start the files which are inside C:\Winthing on the remote computer

  1. Hi,
    thank you very very much for your tutorial. This is what I*m looking for to bring the stream of my doorbell to all my devices.
    But one problem:
    I use openhab 2.4 with the embedded broker. It seems as if only one device reacts to the mqtt command. One time this, next time another.
    Do you have an idea to solve this?

Leave a Reply

Your email address will not be published. Required fields are marked *