4

I am trying to publish JSON data on MQTT broker topic. this is the source code, I tried-

import paho.mqtt.client as mqtt
import json
# Define Variables
MQTT_HOST = "localhost"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "irisPayload"

MQTT_MSG=json.dumps({"sepalLength": "6.4","sepalWidth":  "3.2","petalLength": "4.5","petalWidth":  "1.5"});
# Define on_publish event function
def on_publish(client, userdata, mid):
    print "Message Published..."
# Initiate MQTT Client
mqttc = mqtt.Client()

# Register publish callback function
mqttc.on_publish = on_publish

# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)        

# Publish message to MQTT Broker    
mqttc.publish(MQTT_TOPIC,MQTT_MSG)

# Disconnect from MQTT_Broker
mqttc.disconnect()

I just want to publish JSON data without payload={jsondata}format, how to remove payload as it comes every time when I am publishing my data on particular topic?

In the log it shows msg like this--- [payload={ "sepalLength": "6.4", "sepalWidth": "3.2", "petalLength": "4.5", "petalWidth": "1.5" }]

3
  • Where did you encounter data with 'payload={jsondata}format'? Commented Mar 11, 2017 at 6:24
  • In the log it is showing me data as payload={"sepalLength": "6.4","sepalWidth": "3.2","petalLength": "4.5","petalWidth": "1.5"}. Commented Mar 11, 2017 at 6:37
  • I'm pretty sure that 'problem' is on the broker side of your setup and the normal behaviour of your broker (whichever you use) Commented Mar 11, 2017 at 11:09

2 Answers 2

6

I add some modification for your code, then you can publish json as string, then receive string and convert to json.

Sample code:

import paho.mqtt.client as mqtt
import json
# Define Variables
MQTT_HOST = "localhost"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "irisPayload"

MQTT_MSG=json.dumps({"sepalLength": "6.4","sepalWidth":  "3.2","petalLength": "4.5","petalWidth":  "1.5"});
# Define on_publish event function
def on_publish(client, userdata, mid):
    print "Message Published..."

def on_connect(client, userdata, flags, rc):
    client.subscribe(MQTT_TOPIC)
    client.publish(MQTT_TOPIC, MQTT_MSG)

def on_message(client, userdata, msg):
    print(msg.topic)
    print(msg.payload) # <- do you mean this payload = {...} ?
    payload = json.loads(msg.payload) # you can use json.loads to convert string to json
    print(payload['sepalWidth']) # then you can check the value
    client.disconnect() # Got message then disconnect

# Initiate MQTT Client
mqttc = mqtt.Client()

# Register publish callback function
mqttc.on_publish = on_publish
mqttc.on_connect = on_connect
mqttc.on_message = on_message

# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)

# Loop forever
mqttc.loop_forever()
Sign up to request clarification or add additional context in comments.

Comments

0
data = {"sepalLength": "6.4","sepalWidth":  "3.2","petalLength": "4.5","petalWidth":  "1.5"}
print json.dumps(data)

prints:

{"sepalLength": "6.4","sepalWidth": "3.2","petalLength": "4.5","petalWidth": "1.5"}

not:

payload={"sepalLength": "6.4","sepalWidth": "3.2","petalLength": "4.5","petalWidth": "1.5"}

you have somewhere else a manipulated string that is describing that payload adding that unwanted key to the msg

1 Comment

@Asoul, yes I am talking about this. print(msg.payload) # <- do you mean this payload = {...} ? still, it comes in the log it shows msg like this--- [payload={ "sepalLength": "6.4", "sepalWidth": "3.2", "petalLength": "4.5", "petalWidth": "1.5" }]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.