mqtt_connect

$mqttobj = mqtt_connect();


Connects to the MQTT broker defined in MQTT Gateway. Returns a $mqttobj object of the shipped class Bluerhinos/phpMQTT (MQTT library phpMQTT.php)

LoxBerry Compatibility

This feature first is available with LoxBerry 2.2.1. Set your LB_MINIMUM version in your plugin.cfg accordingly.

The MQTT connection details are used from the MQTT Gateway settings, therefore the MQTT Gateway plugin needs to be installed.  

After the function call, the MQTT connection of $mqttobj is already connected and can directly be used with phpMQTT.php class functions. The phpMQTT.php source is automatically loaded.

The function returns null, if the MQTT Gateway isn't reachable. 

Example Publish

require_once "loxberry_io.php";

// $mqtt is the mqtt connection with Bluerhinos/phpMQTT
$mqtt = mqtt_connect();

# Therefore, the publish is a Bluerhinos/phpMQTT function
$mqtt->publish( "to/my/topic", "Using phpMQTT for publishing", 0, true );

Example Subscription

require_once "loxberry_io.php";

// $mqtt is the mqtt connection with Bluerhinos/phpMQTT
$mqtt = mqtt_connect();

$topics['my/topic/#'] = array('qos' => 0, 'function' => 'processMsg');
$mqtt->subscribe($topics, 0);

while($mqtt->proc()) {
	// Do what you like in here
}

$mqtt->close();

// Callback function that is called if a message arrives
function processMsg($topic, $msg){
		echo "Received Topic: {$topic} -> $msg\n";
}