Publish MQTT message in fiware via MQTT Paho library

Question:

I am working on this Fiware flow:

Fiware-Flow

I have used efrecon/mqtt-client as MQTT client based on mosquitto_pub so that the data can be consumed from Grafana.

docker run -it --rm --name mqtt-publisher efrecon/mqtt-client pub -h 172.31.85.246 -p 31624 -m "AGV_Th|25" -t "/ul/5jggokgpepnvsb2uv4s40d59ov/agv001/attrs"

In the following example, a key-value pair is sent to populate a persistence with Crate DB and then consumed with Grafana.

Grafana

I am testing with a new python mqtt-paho based client without achieving the same result as with the mosquitto_pub based docker client. For instance:

import paho.mqtt.client as mqtt
client = mqtt.Client()
client.connect("172.31.85.246", 31624)
client.publish("/ul/5jggokgpepnvsb2uv4s40d59ov/agv001/attrs","{"agv_th":65}")

Any ideas?

Asked By: sfl0r3nz05

||

Answers:

As per the comments there were two issues:

publish.single("/ul/5jggokgpepnvsb2uv4s40d59ov/agv001/attrs", "AGV_Th|25", hostname="172.31.85.246", port=31624) worked.

single and multiple are utility functions that connect, send message(s) and then disconnect. These are useful where you don’t want to keep the connection active (or just want to simplify your code). As per the docs you can use multiple like this:

msgs = [{'topic':"/ul/5jggokgpepnvsb2uv4s40d59ov/agv001/attrs", 'payload':"AGV_Th|25"},
        {'topic':"/ul/5jggokgpepnvsb2uv4s40d59ov/agv00X/attrs", 'payload':"AGV_Th|XX"}]
publish.multiple(msgs, hostname="172.31.85.246", port=31624)
Answered By: Brits
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.