Control remote pc from Openhab using MQTT
To control remote computer from openhab we need following software:
MQTT broker (my choise is Mosquitto)
WinThing
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_ID="8" VERSION="8 (jessie)" ID=raspbian ID_LIKE=debian HOME_URL="http://www.raspbian.org/" SUPPORT_URL="http://www.raspbian.org/RaspbianForums" BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
Getting newest version directly from Mosquitto reposittory
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key sudo apt-key add mosquitto-repo.gpg.key cd /etc/apt/sources.list.d/
choose right repository
for debian release jessie
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
for debian release stretch
sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
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
WinThing
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 Send("{\}") Else EndIf Else EndIf ShellExecute("https://www.youtube.com/watch?v=PUdyuKaGQd4&index=50&list=PLojFsYksYgmR3FlfyC25mF91qlmR8elD9") Sleep(4000) Send("{f}")
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 )
items:
String PCControl "Computer Control" (living_group)
sitemap:
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" when Item PCControl received command then 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 Thread::sleep(5000) actions.publishMQTT("winthing/keyboard/commands/press_keys", "[\"LWIN\",\"LSHIFT\",\"ENTER\"]") } } end
This rule will start the files which are inside C:\Winthing on the remote computer
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?
Kaloschke