MQTT stupid beginner question (react on subscription)
Hi out there,
I have a very basic question but i am unable to find an answer that is helping me. Most likely I am thinking in a wrong direction, or messing something up.
But I am sure you are able to help me out.
I want to use the MQTT technology to react to events on the Onion Omega.
Everything is set up and seems to work just fine.
Mosquitto is installed, the broker is running on a different device and I can use this command:
mosquitto_sub -h MyServer -t 'omega2/'
to subscribe to the topic. If done so I see all the data that is published in my terminal connection. So far so good. I will send 2 messages to the device "on" and "off"
I want to react to a subscription (for example running a batch script)
How can I create the connection between "subscription" and execute script ?
Message ON received, start file A
Message OFF received, start file B
I hope this is not to confusing :)
Thanks to all
MQTT broker (e.g., mosquito) is just a simple broker/message queue manager.
To enrich the broker side functionality (such as scripting, timed response, etc.)
consider running Node Red (the suite: IDE + run-time) on that server.
E.g., search Pete Scargill MQTT
Thanks for your reply,
I have a node RED installation on a different device running.
All I am trying to do is to react to a message received by the MQTT Client
Message ON is received --> switch Relay to ON.
same thing for the OFF message.
If not using well-polished Node RED approach but DIY...
On a separate client device such as an or another Omega 2, or
run it directly on the MQTT server itself:
to continuously run a process you designed, which will
(a) run a copy of MQTT_client
This process will receive the topics it subscribed to,
take in the new event it received to
(b) trigger actions you want your programming logic to do.
One example of such action is to
(c) publish a new MQTT event (topic_xyz/light_1/ON) to the MQTT broker.
I would be happy to use a well posilded approach, but installing node red on an omega ( plus the then needed dock + an separate USB device) feels like a lot.
Especially because all I am trying to do is to set the relay extension from one state to an other.
And I am very surprised that nobody has created a "out of the box" solution with a small footprint for this kind of problem
Best wishes, and thanks for your support.
So here is my solution:
First I write incoming command to a file called task:
mosquitto_sub -h SERVER -t '/TOPIC/' >> task
then I read the file, and execute the last command:
NUMOFLINES=$(wc -l < task) while : do NEWLINE=$(wc -l < task) if [ "$NEWLINE" -gt "$NUMOFLINES" ] then NUMOFLINES=$(wc -l < task) LASTCOMMAND=$(tail -n 1 task) eval ./$LASTCOMMAND fi done
the Message of the topic equals the file name to execute.
This is working just fine for me
I've just gotten my Omega 2 board today, and I'm thinking along very similar lines — I want my Onion boards to either send MQTT messages when something happens to their connected hardware, or do something with that hardware according to MQTT messages received.
Like you (Sebastian), I see no point in running an MQTT broker on the device — the broker will be elsewhere; these are edge devices that should merely be MQTT clients.
I see you solved it by using some clever files and shell scripting. But did you consider using Python? From other threads, it looks like paho-mqtt can be installed for python, which should make it pretty easy (and possibly more efficient) to subscribe to topics and do stuff in response (or conversely, to publish MQTT messages in reaction to anything you can sense in Python).
Just wondering if I am somehow barking up the wrong tree, or whether this is a sensible way to do it.