0

I can use mosquitto_sub to subscribe to broker for a specific topic, say test_topic,

mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic

I can also pipe grep for a specific keyword, say valve_closed,

mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic | grep "valve_closed"

This would only output a newline with "valve_closed", every time that particular topic broadcasts "valve closed" message.

Input,

Line 1: mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic | grep "valve_open"

Line 2: mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic | grep "valve_closed"

Output,

Line 1: valve_closed


Now, my question is, how can I pipe a command such that if grep "valve_closed" is true, then execute another command.

I've tried chaining a command like echo "Okay so now Valve is Closed" with another | pipe command,

mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic | grep "valve_closed" | echo "Okay, so now Valve is Closed"

But this simply runs the echo command with output "Okay, so now Valve is Closed", even when I group grep+echo within parentheses.

I also tried using AND operator &&,

mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic | grep "valve_closed" && echo "Okay, so now Valve is Closed"

While this waits for grep to match string valve_closed as soon as it finds a match, it would execute echo "Okay, so now Valve is Closed".

I've also tried using if statement, however any inclusion of conditional statement seems to break the polling nature of message_sub which constantly monitors a given topic and prints out its output.

I'm kind of lost as to how I can use mosquitto_sub to retrieve messages and execute shell commands/scripts based on message content. Thanks for reading.

1
  • 1
    xargs should work for you for this. Research using xargs with grep. Come back and ask if you were not able to figure it out. – kalyanswaroop May 29 at 22:23
0

The problem here is that mosquitto_sub never returns (by default) so it's output never ends so can't be used to test against.

You will do MUCH better not trying to do this in a shell script and use something like python.

Edit:

If you REALLY MUST do this without writing something (trust me it is worth it) then you can use mqtt-exec (though it would require nodejs installing)

https://github.com/denschu/mqtt-exec

3
  • If you design testtopic to only have trigger messages (ie. messages that you want to act on), then use -C 1 to exit on any message received. Else, if you still insist on mosquitto_sub, then put it in a loop, at the risk of missing messages... – Gambit Support May 29 at 14:25
  • Thanks. I tried it in one of my earlier iterations and guess what - there's an inherent delay between the time a message is received and mosquitto_sub realizing it's fulfilled -C 1 condition. Sometimes the order of that delay is upwards of a second, which defeats purpose of low latency nature of MQTT broadcast messages. – user14252 May 29 at 16:07
  • Well, looks like user error because when I tried the minimal reproduction against a local broker, the mosquitto_sub exited immediately after a mosquitto_pub, unless you have long latency between your clients and your broker, then that's an entirely new problem. You will not solve your problem unless you are meticulous and precise. – Gambit Support May 30 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.