MQTT library phpMQTT.php

LoxBerry includes an external PHP library for MQTT, called Bluerhinos\phpMQTT (repo https://github.com/bluerhinos/phpMQTT). This class provides easy access to subscribe to topics, and to publish messages.

LoxBerry Compatibility

This library is included from LoxBerry 2.0. Use the LB_MINIMUM parameter in your plugin.cfg.

Inclusion example

require_once "loxberry_io.php";
require_once "phpMQTT/phpMQTT.php";

// Get the MQTT Gateway connection details from LoxBerry
$creds = mqtt_connectiondetails();

// MQTT requires a unique client id
$client_id = uniqid(gethostname()."_client");

// Value we'd like to publish
$value = 12345;

// Be careful about the required namespace on inctancing new objects:
$mqtt = new Bluerhinos\phpMQTT($creds['brokerhost'],  $creds['brokerport'], $client_id);
	if( $mqtt->connect(true, NULL, $creds['brokeruser'], $creds['brokerpass'] ) ) {
		$mqtt->publish("this/is/my/topic", $value, 0, 1);
		$mqtt->close();
	} else {
		echo "MQTT connection failed";
	}

See also 

MQTT Gateway for plugin developers

Examples from the library