-
Notifications
You must be signed in to change notification settings - Fork 60
API
MQTT Client Interface
MQTT Data Structures
MQTT Client Interface represents the object that handles all communication with MQTT broker. It provides a set of methods that allow performing basic operations defined in MQTT protocol.
To create MQTT client call CreateMqttClient
function. It takes MqttClientConfig structure as a parameter which specifies client settings like host URL, port number, client ID.
To establish connection with MQTT broker call Connect
function on MQTT Client. Client connects to broker which was specified by MqttClientConfig structure during its creation. The function takes MqttConnectionData structure as a parameter which specifies users login and password.
To disconnect from MQTT broker call Disconnect
function on MQTT Client.
To subscribe to a topic that the client is interested in call Subscribe
function on MQTT Client. It takes topic name (as string) and quality of service (int value) as parameters.
To usubscribe from a topic call Unsubscribe
function on MQTT Client. It takes topic name (as string) as a parameter.
To publish message to MQTT broker call Publish
function on MQTT Client. It takes MqttMessage structure as a parameter which specifies message content, topic, quality of service and whether message should be retained.
MQTT Client can listen to several events triggered by MQTT broker during their communication.
Events are triggered when:
- Client was connected to the broker
- Client was disconnected from the broker
- Client subscribed for a certain topic
- Client unsubscribed from a certain topic
- Client message was published
- Client received message of certain topic he previously subscribed to
- Error in client-broker communication occurred
Those events can be processed by setting corresponding handlers for MQTT Client.
MqttClientConfig
structure stores MQTT client configuration required to identify particular MQTT broker.
Field | Type | Description |
---|---|---|
HostUrl | FString | Host URL |
Port | int | Host port |
ClientId | FString | Unique client identifier |
MqttConnectionData
structure stores user credentials required to establish a connection with MQTT broker.
Field | Type | Description |
---|---|---|
Login | FString | User login |
Password | FString | User password |
MqttMessage
structure stores data that client publishes to MQTT broker or receives from it in case corrsponding subscription exists.
Field | Type | Description |
---|---|---|
Message | FString | Message content |
Topic | FString | Message topic |
Retain | bool | Retain flag |
Qos | int | Quality of service |