mqtt.publish() function
The mqtt.publish()
function is experimental and subject to change at any time.
By using this function, you accept the risks of experimental functions.
The mqtt.publish()
function outputs data to an MQTT broker using MQTT protocol.
import "experimental/mqtt"
mqtt.publish(
broker: "tcp://localhost:8883",
topic: "example-topic",
message: "Example message",
qos: 0,
retain: false,
clientid: "flux-mqtt",
username: "username",
password: "password",
timeout: 1s,
)
Parameters
broker
The MQTT broker connection string.
topic
The MQTT topic to send data to.
message
The message to send to the MQTT broker.
qos
The MQTT Quality of Service (QoS) level.
Values range from [0-2]
.
Default is 0
.
retain
The MQTT retain flag.
Default is false
.
clientid
The MQTT client ID.
username
The username to send to the MQTT broker. Username is only required if the broker requires authentication. If you provide a username, you must provide a password.
password
The password to send to the MQTT broker. Password is only required if the broker requires authentication. If you provide a password, you must provide a username.
timeout
The MQTT connection timeout.
Default is 1s
.
Examples
Send a message to an MQTT endpoint
import "experimental/mqtt"
mqtt.publish(
broker: "tcp://localhost:8883",
topic: "alerts",
message: "wake up",
clientid: "alert-watcher",
retain: true,
)
Send a message to an MQTT endpoint using input data
import "experimental/mqtt"
import "influxdata/influxdb/sample"
sample.data(set: "airSensor")
|> range(start: -20m)
|> last()
|> map(fn: (r) => ({
r with
sent: mqtt.publish(
broker: "tcp://localhost:8883",
topic: "air-sensors/last/${r.sensorID}",
message: string(v: r._value),
clientid: "sensor-12a4",
)
})
)
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for Flux and this documentation. To find support, use the following resources:
InfluxDB Cloud customers can contact InfluxData Support.