2

Hi I want to try and convert a message receieved from MQTT into a variable so I can do an "IF script" using messages sent from a certain topic on mqtt.

I tried this at top of script but didn't work:

Message = mosquitto_sub -d -t "test/test"

Thank you

Gundam0079
  • 31
  • 4
  • 1
    The `mosquitto_sub` command runs indefinitely, right? Are you okay with using an option like `-C 5` to disconnect after receiving 5 messages? – Haxiel Jan 03 '19 at 14:24
  • I want it to run in background constantly. I want the script to play a sound if the message is the correct one or if something is received via that topic. – Gundam0079 Jan 03 '19 at 16:03

2 Answers2

1

Simply execute:

mosquitto_sub -h localhost -t "#"  -C 1  > /var/spool/mosquitto/value
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
fr3d mobile
  • 71
  • 1
  • 3
0

If you want the result of the command in a variable you've to write the line between cote character like this :

Message=`mosquitto_sub -d -t "test/test"`

If you want to use the variable in place of the command you should write the line like this :

Message="mosquitto_sub -d -t 'test/test'"

You've to look forward the special character in the bash script

dubis
  • 1,430
  • 2
  • 18
  • 31