$senttopic = mqtt_publish( $topic, $value );

$senttopic = mqtt_retain( $topic, $value );

$senttopic = mqtt_set( $topic, $value, $retain );

Publishes a value to a MQTT topic with or without retain flag. The MQTT connection is implicitely opened to call the publish. (MQTT Gateway plugin required)

LoxBerry Compatibility

This functions first are available with LoxBerry 2.2.1. Set your LB_MINIMUM version in your plugin.cfg accordingly.

The MQTT connection is implicitely opened to the MQTT broker defined in MQTT Gateway plugin, therefore the MQTT Gateway plugin needs to be installed.  

Publishes the $value to the specified $topic

With mqtt_publish and mqtt_retain, the messages are published without and with retain flag.

With mqtt_set, the retain flag is the third parameter of the function call:

  • If the $retain parameter is omitted or false, a publish is used.
  • If the $retain parameter is true, the message is published with retain flag set.


The functions return the $topic as string, if the publish was successful.

The functions return null on any errors.


This functions do not require to first call mqtt_connect, as the connection to the mqtt broker is implicitely opened.


require_once "loxberry_io.php";

mqtt_set( "hello/you", "publishing content with retain", true );
mqtt_set( "hello/you", "publishing content without retain", false );
mqtt_publish ("hello/you", "publishing content" );
mqtt_retain ("hello/you", "publishing with retain flag" );