[SOLVED] Controlling Linux client with MQTT (or other possibilities)

Dear Everyone!

I have Linux clients (Debian, Raspbian) and I want to turn their screen on and off from OH rule (maybe later shutdown, restart and other things can useful). How is this possible with the easiest approach?
I saw the there is a script for Windows to work as an MQTT client. Are there any similar thing for Linux? Or should I do it with the exec binding? If yes, how? The clients are in the same network, but every client is a seperate client, so it has to be done through network.

Thanks!

Yes use python:

For example:


import paho.mqtt.client as mqttClient
import time
import os
import subprocess

def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to broker")
        global Connected
        Connected =True
    else:
        print("Connection failed")

def on_message(client, userdata, message):
    print "Message received: " + message.topic + " : " + message.payload
    fileName = "/home/pi/share/Adhan" + message.payload + ".mp4"
    print fileName
    time.sleep(1)
    subprocess.Popen(["omxplayer", fileName, "--aspect-mode", "fill"])

Connected = False

broker_address = "192.168.0.34"
port = 1883
user = "user"
password = "password"
client = mqttClient.Client("Python2")
client.username_pw_set(user, password=password)
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('Misc/Prayer')

try:
    while True:
        time.sleep(1)

except KeyboardInterrupt:
    print "exiting"
    client.disconnect()
    client.loop_stop()

I do this to control my little fleet of raspberry piā€™s around the house.

My solution is MQTT running on the openhab pi then install node-red on the other computers. With just a bit of node-red you can take any MQTT message and convert it to a command line exec command.

Could run basically any command-line code you like, I use it for curl, ā€œsudo rebootā€, and playing audio on remote piā€™s.

1 Like

Now thatā€™s what I call a very good ideaā€¦

Thanks! I will look into it. Maybe if you have time, you can do a tutorial on how you use itā€¦ :slight_smile:

I just realized you could take it one step further and donā€™t even need MQTT.

Just install the openhab component of node-red and run your triggers right through the REST-API (I think thatā€™s how node-red connects?)

Iā€™m trying to go for a unified way in IoT and lots of my switches, etc use MQTT, so it will be great if almost everything can use the same protocol/message control.

Step 1:
Install node red on the ā€œremoteā€ computer. Use instructions for windows, linux, mac, debian, raspberry pi - you name it from nodered.org.

Step 2:
Put an MQTT node and an exec node together. Youā€™ll have to do a bit of configuring on the MQTT node - double click it to enter the IP address, authentication, etc. Default settings on the exec node should work for simple commands but you may have to tinker with the exec node configuration to get it just right. Looks like this:
32%20AM

Step 3:
Any string sent to that topic will be run in the command line.

Step 4:
Profit?

I have another command line program (atvremote) that I use this way with a bit more code to watch the output of the exec node (runs in spawn mode) - then while the CLI program is running I get push updates from the apple TV to send back to openhab for all sorts of fun. More on that by special request.

(obviously this is a very simple example - you can add change nodes and switch nodes and javascript functions to your heartā€™s content. Iā€™ll leave it to you to go through the node-red documentation but needless to say itā€™s a great tool)

1 Like

Thanks!
Havenā€™t used Node-RED before, thought it is not that easy to setup. I think this is the easiest way to do.
I will get into, thanks!!

I just clicked on your profile and saw you also use HomeKitā€¦

If you come to like node red, it has a homekit bridge node that has now become my go-to for all devices homekit. I have my light switches, dimmers, outlets, curtains, fans, and sprinklers all linked between openhab and homekit with node red. It works MUCH better than anything Iā€™ve ever tried with openhabā€™s homekit functionality.

As a bonus - all of these items show as their proper device type in homekit! I used to call my window a ā€œswitchā€ā€¦

Iā€™m happy to share my setup, feel free to flag me in another topic if youā€™d like to see any of it.

Thanks, this seems really cool! I have had several problems with HomeKit (you can see it in my profile, I was the one who started the ā€œNo responseā€ error). However it now works okay, all devices are light switches. I donā€™t have any specific devices yet (like roller shutter), but it would be great to have specific device types.
So you expose your items to Node-RED and that takes care of HomeKit?

Itā€™s a more complicated setup than your exec node above. The ā€œflowā€ in node red goes like this:

-Starts with an openhab node (locally or on local network)
-Goes to a javascript function node to ā€œtranslateā€ commands from openhab terms to homekit homes
-Goes into a homekit node
-Next is another function to ā€œtranslateā€ from homekit back to openhab
-Finally goes back to openhab (in the case you command an item from homekit)

Then youā€™ll add a homekit bridge on your home app pointed at node red and be running. My bridge has about 50 devices right now (oh I forgot my smoke detector earlier! Itā€™s wired to a pi GPIO pin). Iā€™ve had no errors or issues with this setup in the past few months.

I went to this setup because I had issues with my dimmers sending ā€œdouble commandsā€ which is another common complaint of the openhab/homekit integration.

Honestly node red has made many of my rules much easier for me to set up. I still have tons of rules and items in openhab - but node red is becoming my choice more and more oftenā€¦

I would like to learn more. Getting a atv 4k will it work ?

Yeah letā€™s not hijack this thread too much - start a new one and Iā€™ll show you how I use my ATV (4 and 4k) like this.

Thanks, Iā€™m also interested in it.

Thank you. Then back to node red controll. If i have a ads1115 that gets anolg from sensors can i also do it this way aswell.to send the info to openHAB?

Whatā€™s the ADS1115 plugged in to?

If itā€™s plugged into a pi then yes thereā€™s a node-red plugin for it (under ā€œmanage palletā€).

If itā€™s connected to something that runs MQTT (publish the ADS1115 data to MQTT) then again, yes - just pull the data off MQTT topics and send it into OpenHAB.

I donā€™t have that hardware but it appears to be supported in node-red.

Thanks!

I have already tried out the mqtt with execā€¦ it seems working I can send commands as string and it will execute!

1 Like

If you have time, Iā€™m more interested in the HomeKit flow you have :slight_smile: How you can choose which elements are should be exposed to HomeKit, how you transform the item from openHab into HomeKit, you you deploy inputs from HomeKit to openHab, etcā€¦

Ps.: I have already installed the OH and HomeKit extension for Node-RED, I will migrate everything if I have time (thus I will have more item, because I can include things which I was unable before). You have this flow for every item you have exposed to HomeKit or can you make it ā€˜generalā€™ for similar items?

Youā€™re hooked. I like it. Iā€™ll to put together some examples for you.

The way homekit works is you connect a bridge to your phone then add devices in node red that all point to that bridge. Similar to how your mqtt nodes point to your mqtt broker.

Give me a bit, I need to add comments to some code. Iā€™ll pick one of each item type I have - should be a good addition to the forums.