Reputation: 13
How do I receive an MQTT and pass it through to bash script that I have setup? I receive values that are increasing between the message that I am sending and when I receive my message, I can't tell if it has executed the bash script and it doesn't print the lines following it to check, so I suspect something is going wrong prior.
The last line it prints is:
voltage = "voltage = %s" %volts
print(voltage)
I have tried putting the code to make the bash script with the MQTT message in the on_message function.
I have run the bash script in the terminal to make sure its function works. I have created a python script that I manually input the values for when calling the script and it worked. I have essentially put this script in the on_message function.
#! /usr/bin/python
import os, time, sys, datetime, paho.mqtt.client as mqtt
print("Starting script")
#while True:
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("$SYS/#")
def on_message(client, userdata,message):
print("message received")
split = str(message.payload.decode("utf-8"))
value = str(datetime.datetime.now())
value = """ "%s" """ %value
split = split.split(',')
print(split)
num = str(982123456)
RFID = "RFID = %s" %num
start = "Super secret start sequence"
username = """ "Secret name" """
password = """ "Secret password" """
volts = split[0]
voltage = "voltage = %s" %volts
print(voltage)
if split[2] == "None":
lat = 22
long = 141
print("IF NONE")
else:
lat = split[2]
long = split[3]
print("IF SOMEThING")
os.system(start + RFID + "," + "DateTime = %s" %value + "," + "lat =" + lat + "," + "long =" + long + "," + voltage + "," + "username = " + username + "," + "password = " + password + ")'" )
print(start + RFID + "," + "DateTime = %s" %value + "," + "lat =" + lat + "," + "long =" + long + "," + voltage + "," + "username = " + username + "," + "password = " + password + ")'" )
print(split)
client= mqtt.Client("demo",clean_session=True, userdata=None, transport="tcp")
# print("Connecting...")
client.connect("localhost")
#client.loop_start()
client.subscribe("test")
# print("subscribed")
print("Checker")
client.on_message=on_message
print("Client Message:")
print(client.on_message)
client.on_connect=on_connect
# print("loop")
client.loop_forever()
# time.sleep(0.1)
I expect:
message received
['4.69172', 'None', 'None', 'None']
voltage = 4.69172
*response from bash script*
But I am getting:
Connected with result code 0
message received
['mosquitto version 1.4.15']
voltage = mosquitto version 1.4.15
message received
['Wed', ' 13 Feb 2019 00:27:01 +0000']
voltage = Wed
message received
['3626777 seconds']
voltage = 3626777 seconds
message received
['4']
voltage = 4
message received
['6']
voltage = 6
message received
['2']
voltage = 2
message received
['2']
voltage = 2
message received
['2']
voltage = 2
message received
['2']
voltage = 2
message received
['0']
voltage = 0
message received
['49']
voltage = 49
message received
['6762187']
voltage = 6762187
message received
['10594382']
voltage = 10594382
Upvotes: 1
Views: 480
Reputation: 59608
You have subscribed to multiple topics.
Firstly you subscribe to the topic test
which I assume has the data you are expecting.
But in the on_connect
function you are also subscribing to $SYS/#
which will return lots of different messages based on the internal state of the MQTT broker such as uptime, number of connected clients, number of messages sent or received. If you remove this line you should only get the messages you expect.
Upvotes: 1