$value = mqtt_get( $topic, [$timeout_msecs]);
Aquires a MQTT topic from the broker. The MQTT connection is implicitely opened to get the value. (MQTT Gateway plugin required)
This function first is 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.
$topic is the topic you want to get from the broker
$timeout_msecs is the timeout in milliseconds to wait for a response. Default is 250ms if the parameter is omitted.
As soon as the first value is returned from the broker, the result is retured by the function. If the broker sends no result, the function will wait for
$timeout_msecs milliseconds until it gives up.
By design of MQTT, the function cannot request a value from a device on demand, but creates a subscription to fetch the specified topic. Therefore, only retained topics, stored on the broker, can be fetched.
If you require to monitor for publishes of your device, you need to create a daemon and wait for incoming messages. mqtt_connect has an easy example for this.
The function returns the $value as string, if the subscription was successful.
The functions return null if no result could be fetched, or on any errors.
If the provided topic includes mqtt joker (+ and #) with multiple results, the function will return the alphabetical first value of the resultset.
This functions do not require to first call mqtt_connect, as the connection to the mqtt broker is implicitely opened.
// Default wait time 250ms
echo mqtt_get("hello/you") . "\n";
// Wait up to 2 seconds
echo mqtt_get("hello/you", 2000) . "\n";