Difference between revisions of "MQTT Interface"

From Hubitat Documentation
Jump to: navigation, search
(parseMessage)
(Hubitat Provided Methods (Since 2.1.2))
Line 23: Line 23:
 
:::''lastWillMessage'' - The message to publish if connection is lost (Since 2.1.2)
 
:::''lastWillMessage'' - The message to publish if connection is lost (Since 2.1.2)
 
:::''lastWillRetain'' - Option to set last will message to retained, defaults to false (Since 2.1.2)
 
:::''lastWillRetain'' - Option to set last will message to retained, defaults to false (Since 2.1.2)
 +
:::''cleanSession'' - Option to set the clean_session flag for the connection (Since 2.2.2)
 +
:::''tlsVersion'' - The TLS version to use when connecting to broker, possible values are 1.0, 1.1, 1.2 (Since 2.2.2)
 +
:::''caCertificate'' - The CA certificate for the TLS connection (Since 2.2.2)
 +
:::''clientCertificate'' - The client certificate (X.509) for the TLS connection (Since 2.2.2)
 +
:::''privateKey'' - The private key for the TLS connection (Since 2.2.2)
  
 
:;Examples
 
:;Examples

Revision as of 14:14, 10 July 2020

Overview

Hubitat allows for opening and maintaining a connection to an MQTT broker from the hub. All code needs to be contained in a driver, there is no option to open an MQTT client connection from an app. Hubitat provides methods to connect and disconnect to the broker, subscribe and publish messages to topics. In addition it is required to create a method in the driver that accepts incoming messages (parse) and another method that will be called with any status updates for the client connection (mqttClientStatus).

Methods

Hubitat Provided Methods (Since 2.1.2)

connect

Signature
void connect(String broker, String clientId, String username, String password)
void connect(String broker, String clientId, String username, String password, options (name/value pairs))
Parameters
broker - The url of the broker to connect to.
clientId - The client id to use when connecting to the broker.
username - The username to use when connecting to the broker, use null if no username is necessary.
password - The password to use when connecting to the broker, use null if no password is necessary.
options - Optional parameters to configure the MQTT connection. Possible values:
byteInterface - defaults to 'false'. If set to true, messages are handled as byte arrays and will be hex string encoded.
lastWillTopic - The topic to publish to if connection is lost (Since 2.1.2)
lastWillQos - The qos to use for publishing last will message, possible values are 0,1 or 2 (Since 2.1.2)
lastWillMessage - The message to publish if connection is lost (Since 2.1.2)
lastWillRetain - Option to set last will message to retained, defaults to false (Since 2.1.2)
cleanSession - Option to set the clean_session flag for the connection (Since 2.2.2)
tlsVersion - The TLS version to use when connecting to broker, possible values are 1.0, 1.1, 1.2 (Since 2.2.2)
caCertificate - The CA certificate for the TLS connection (Since 2.2.2)
clientCertificate - The client certificate (X.509) for the TLS connection (Since 2.2.2)
privateKey - The private key for the TLS connection (Since 2.2.2)
Examples
// Connect to MQTT broker
interfaces.mqtt.connect("tcp://test.mosquitto.org:1883", "mqtttest123", null, null)
// Connect to MQTT broker with last will
interfaces.mqtt.connect("tcp://test.mosquitto.org:1883", "mqtttest456", null, null, lastWillTopic: "/my/last/will", lastWillQos: 0, lastWillMessage: "I died")

disconnect

Signature
void disconnect()

subscribe

Signature
void subscribe(String topicFilter, int qos = 1)
Parameters
topicFilter - The topic / filter to subscribe to.
qos - (optional, defaults to 1) The qos to use when subscribing.

isConnected

Signature
boolean isConnected()
Returns
boolean - true if client is connected, false otherwise.

unsubscribe

Signature
void unsubscribe(String topicFilter)
Parameters
topicFilter - The topic filter to unsubscribe from.

publish

Signature
void publish(String topic, String payload, int qos = 1, boolean retained = false)
Parameters
topic - The topic to publish the message to.
payload - The body of the message.
qos - (optional, defaults to 1) The qos to use when publishing the message.
retained - (optional, defaults to false) Retain message in queue if subscriber is not listening.

parseMessage

Signature
Map parseMessage(String stringToParse)
Parameters
stringToParse - The string coming from the MQTT brokers subscribed topic.
Returns
Map - the string passed as a parameter is split into a Map and returned.
Examples
// parse message
interfaces.mqtt.parseMessage(description)

Hubitat Provided Methods (Deprecated)

mqttConnect

Signature
void hubitat.helper.InterfaceUtils.alphaV1mqttConnect(DeviceWrapper device, String broker, String clientId, String username, String password)
void hubitat.helper.InterfaceUtils.alphaV1mqttConnect(DeviceWrapper device, String broker, String clientId, String username, String password, options (name/value pairs))
Parameters
device - The current device that is calling the method.
broker - The url of the broker to connect to.
clientId - The client id to use when connecting to the broker.
username - The username to use when connecting to the broker, use null if no username is necessary.
password - The password to use when connecting to the broker, use null if no password is necessary.
options - Optional parameters to configure the MQTT connection. Possible values:
byteInterface - defaults to 'false'. If set to true, messages are handled as byte arrays and will be hex string encoded.
lastWillTopic - The topic to publish to if connection is lost (Since 2.1.2)
lastWillQos - The qos to use for publishing last will message, possible values are 0,1 or 2 (Since 2.1.2)
lastWillMessage - The message to publish if connection is lost (Since 2.1.2)
lastWillRetain - Option to set last will message to retained, defaults to false (Since 2.1.2)
Examples
// Connect to MQTT broker
hubitat.helper.InterfaceUtils.alphaV1mqttConnect(device, "tcp://test.mosquitto.org:1883", "mqtttest123", null, null)
// Connect to MQTT broker with last will
hubitat.helper.InterfaceUtils.alphaV1mqttConnect(device, "tcp://test.mosquitto.org:1883", "mqtttest456", null, null, lastWillTopic: "/my/last/will", lastWillQos: 0, lastWillMessage: "I died")


mqttDisconnect

Signature
void hubitat.helper.InterfaceUtils.alphaV1mqttDisconnect(DeviceWrapper device)
Parameters
device - The current device that is calling the method.

mqttSubscribe

Signature
void hubitat.helper.InterfaceUtils.alphaV1mqttSubscribe(DeviceWrapper device, String topicFilter, int qos = 1)
Parameters
device - The current device that is calling the method.
topicFilter - The topic / filter to subscribe to.
qos - (optional, defaults to 1) The qos to use when subscribing.

mqttUnsubscribe

Signature
void hubitat.helper.InterfaceUtils.alphaV1mqttUnsubscribe(DeviceWrapper device, String topicFilter)
Parameters
device - The current device that is calling the method.
topicFilter - The topic filter to unsubscribe from.

mqttPublish

Signature
void hubitat.helper.InterfaceUtils.alphaV1mqttPublish(DeviceWrapper device, String topic, String payload, int qos = 1, boolean retained = false)
Parameters
device - The current device that is calling the method.
topic - The topic to publish the message to.
payload - The body of the message.
qos - (optional, defaults to 1) The qos to use when publishing the message.
retained - (optional, defaults to false) Retain message in queue if subscriber is not listening.

parseMqttMessage

Signature
Map hubitat.helper.InterfaceUtils.alphaV1parseMqttMessage(String stringToParse)
Parameters
stringToParse - The string coming from the MQTT brokers subscribed topic.
Returns
Map - the string passed as a parameter is split into a Map and returned.

User defined methods

parse(String message)

This method is called with any incoming messages from the subscribed topics of the MQTT broker. This is a standard method for drivers.

mqttClientStatus(String message)

This method is called with any status messages from the MQTT client connection (disconnections, errors during connect, etc) The string that is passed to this method with start with "Error" if an error occurred or "Status" if this is just a status message.