3.2.6.2 Cloud Service
The Cloud service provides an Application Programming Interface (API) to manage MQTT functionalities. These functionalities include, configuring the MQTT settings, connecting, disconnecting and reconnecting to the MQTT broker, publishing, subscribing and setting callbacks.
MQTT System Service Configuration in MCC
This section allows MQTT service basic configuration as mentioned below:
- Cloud URL: Configure Cloud provider endpoint / MQTT Broker URL.
- Cloud Port : Configure Cloud/MQTT port.
- Azure DPS: Select to enable Azure DPS option (applicable only when Azure
endpoint is used)
- Scope ID: Provide Scope ID from Azure IoT Central portal for registered device
- Device Template: Configure Azure DPS specific device template
- User Name and Password: Configure cloud client credentials.
- Client ID: Device ID registered with cloud provider.
-
Publish: Select to enable/ MQTT Publish option. If enabled, it offers related configurations such as Publish Topic Name, Pub QoS, Retain Flag.
- Keep Alive: Select to enable Keep Alive MQTT specific option.
- Keep Alive Interval: Configure the field in the range of 1-1000 (in seconds)
- Subscribe: Select to enable MQTT Subscribe option. If enabled, it provides subscribe specific configurations such as Total Subscribe Topics, Table for Subscribe Topics, Sub. QoS
- TLS: Select to enable TLS
Configuration option. If enabled, it will further prompt to enter details as
below:
- Server Certificate
- Device Certificate
- Device Key
- Device Key Password
- Server Name
- Domain Name
Note: The Device Certificate, Device Key and Device Key Password are not used in the RNWF11.
The MQTT service API example is as follows:
The following list captures the MQTT callback event codes and their
arguments
SYS_RNWF_RESULT_t SYS_RNWF_MQTT_SrvCtrl( SYS_RNWF_MQTT_SERVICE_t request, void *input)
It
handles following services and reports the result to application over the return code or
through the registered callback:Service | Input | Description |
---|---|---|
SYS_RNWF_MQTT_CONFIG | Broker URL, Port, Client ID, Username, TLS configuration | Configures the MQTT server details along with the corresponding TLS configurations |
SYS_RNWF_MQTT_CONNECT | None | Initiates the MQTT connection to the configured MQTT broker |
SYS_RNWF_MQTT_RECONNECT | None | Triggers the re-connection to the configured MQTT broker |
SYS_RNWF_MQTT_DISCONNECT | None | Disconnects from the connected MQTT broker |
SYS_RNWF_MQTT_SUBSCRIBE_QOS0 | Subscribe topic (String) | Subscribes to the given subscribe topic with QoS0 |
SYS_RNWF_MQTT_SUBSCRIBE_QOS1 | Subscribe topic (String) | Subscribes to the given subscribe topic with QoS1 |
SYS_RNWF_MQTT_SUBSCRIBE_QOS2 | Subscribe topic (String) | Subscribes to the given subscribe topic with QoS2 |
SYS_RNWF_MQTT_PUBLISH | New, QOS, Retain, topic, message | Publish the message on given publish topic and configuration |
SYS_RNWF_MQTT_SET_CALLBACK | Callback Function Handler | Registers the MQTT callback to report the status to user application |
Event | Response Components | Comments |
---|---|---|
SYS_RNWF_MQTT_CONNECTED | None | Reported once connected to MQTT broker |
SYS_RNWF_MQTT_DISCONNECTED | None | Event to report the MQTT broker disconnection |
SYS_RNWF_MQTT_SUBCRIBE_MSG | dup, QoS, retain, topic, payload | Reports the received payload for the subscribed topic |
SYS_RNWF_MQTT_SUBCRIBE_ACK | Integer string | Subscribe ack return code |
SYS_RNWF_MQTT_DPS_STATUS | Integer | Azure DPS status:-
|
MQTT Publish
User application can publish to the MQTT broker by creating the MQTT frame and then
sending the frame using the API. The sequence chart is illustrated
below.
SYS_RNWF_MQTT_SrvCtrl(SYS_RNWF_MQTT_PUBLISH, (void *)&mqtt_pub)
MQTT Subscribe
The sequence for subscribing to a topic from the MQTT Broker is illustrated below.
The user application needs to use the API to subscribe to the topic with the
appropriate QoS
value.
SYS_RNWF_MQTT_SrvCtrl(SYS_RNWF_MQTT_SUBSCRIBE_QOS0, buffer)