Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 6018

General programming discussion • Re: How to process MQTT data as they arrive?

$
0
0
Have a look at this:
https://stackoverflow.com/questions/735 ... s-received

They use the pipe command to process output from MQTT.
So in order to protect from disappearing URL:s I am showing the selected stackoverflow reply here:
You should be able to use a little bash script like this:

Code:

#!/usr/bin/env bashmosquitto_sub ... |   while read payload ; do      echo Received $payload      ... process and produce response      mosquitto_pub ... $response   done
Save as responder.sh, and make it executable with:

chmod +x responder.sh
Then run it with:
./responder
I asked about use of pipe ( | ) in my first reply since I did not understand how it could be used here...

In the stackoverflow solution above the mosquitto_sub command line ends in the pipe char followed by a while section of bash so this is apparently how to channel the mosquitto_sub output to something other than a logfile (in this case a while loop).

What I don't yet understand is how it works...
Does the while xxx; do...done part of the command consume every output item of the mosquitto_sub command?
I.e. will it run for every incoming message?

In my case the mosquitto_sub command should be running until killed and the script consumes every incoming message.

Another question:
The command "while read payload; " is preceding the processing do..done section, does this mean that $payload will contain the entire message including spaces and newlines etc? Or does read only get the first line of the message?

And it will be repeated for each incoming message?

Statistics: Posted by Bosse_B — Fri Sep 13, 2024 7:56 am



Viewing all articles
Browse latest Browse all 6018

Latest Images

Trending Articles



Latest Images