mqtt_connectiondetails

$array = mqtt_connectiondetails();


Checks if the MQTT Gateway plugin is installed, and returns the configured host and authentication data from the MQTT Gateway plugin.

LoxBerry Compatibility

This feature first is available with LoxBerry 2.0. Set your LB_MIN version in your plugin.cfg accordingly.

Return value

The functions returns an named array with the following information from the MQTT Gateway plugin for the broker.

The function returns null, if the MQTT Gateway plugin is not installed, or the configuration could not be read.


With the broker details, you can establish a full MQTT connection to the configured broker. Use the PHP library phpMQTT.php (shipped with LoxBerry) to connect, publish and subscribe.

With the UDP Inport (udpinport) you can establish an udp connection to the MQTT Gateway (not the broker directly), to easily publish messages. See the udp part of MQTT Gateway - HTTP- und UDP-Interface.

Returning data in a named array

To access the values, use the syntax e.g. $mqttcred['brokeraddress'].

KeyDescription
brokeraddressThe full address of the broker, e.g. localhost:1883.
brokerhostThe hostname part of the broker address, e.g. localhost.
brokerportThe port part of the broker address, e.g. 1883.
websocketportThe websocket port of the broker, e.g. 9001 (from LB 2.2.1)
brokeruserThe username for the broker. The field may be empty if authentication is disabled on the broker.
brokerpassThe passwort for the broker. The field may be empty, if authentication is disabled or no password is set.
udpinportThis is the UDP input port of the MQTT Gateway on the local installation. See MQTT Gateway - HTTP- und UDP-Interface


Usage

use "loxberry_io.php";

$mqttcred = mqtt_connectiondetails();
echo $mqttcred['brokerhost'].':'.$mqttcred['brokerport'];

You can check for existance of the function, to let your plugin run under lower LoxBerry versions, and add the feature only for current Loxberry versions:

if ( !function_exists('mqtt_connectiondetails') ) {
    error_log( "Feature not available in this LoxBerry version" );
	return;
} 

See also

  • mqtt_connect- Establishes a ready connection to the MQTT Broker configured by the user in MQTT Gateway. It wraps the connection process using the mqtt_connectiondetails settings.
  • mqtt_publish/mqtt_retain/mqtt_set - "Shoot-and-forget" functions to directly publish messages, or fetch retained messages from the broker. Connection processing is done implicitely by the functions.