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

Figure 3-37. Cloud Service Configuration

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:
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:
Table 3-19. MQTT Services
ServiceInputDescription
SYS_RNWF_MQTT_CONFIGBroker URL, Port, Client ID, Username, TLS configurationConfigures the MQTT server details along with the corresponding TLS configurations
SYS_RNWF_MQTT_CONNECTNoneInitiates the MQTT connection to the configured MQTT broker
SYS_RNWF_MQTT_RECONNECTNoneTriggers the re-connection to the configured MQTT broker
SYS_RNWF_MQTT_DISCONNECTNoneDisconnects from the connected MQTT broker
SYS_RNWF_MQTT_SUBSCRIBE_QOS0Subscribe topic (String)Subscribes to the given subscribe topic with QoS0
SYS_RNWF_MQTT_SUBSCRIBE_QOS1Subscribe topic (String)Subscribes to the given subscribe topic with QoS1
SYS_RNWF_MQTT_SUBSCRIBE_QOS2Subscribe topic (String)Subscribes to the given subscribe topic with QoS2
SYS_RNWF_MQTT_PUBLISHNew, QOS, Retain, topic, messagePublish the message on given publish topic and configuration
SYS_RNWF_MQTT_SET_CALLBACKCallback Function HandlerRegisters the MQTT callback to report the status to user application
The following list captures the MQTT callback event codes and their arguments
Table 3-20. Callback Event Codes
EventResponse ComponentsComments
SYS_RNWF_MQTT_CONNECTEDNoneReported once connected to MQTT broker
SYS_RNWF_MQTT_DISCONNECTEDNoneEvent to report the MQTT broker disconnection
SYS_RNWF_MQTT_SUBCRIBE_MSGdup, QoS, retain, topic, payloadReports the received payload for the subscribed topic
SYS_RNWF_MQTT_SUBCRIBE_ACKInteger string Subscribe ack return code
SYS_RNWF_MQTT_DPS_STATUSInteger Azure DPS status:-
  • 1 for success
  • 0 for failure

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)
Figure 3-38. MQTT Publish Sequence

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) 
Figure 3-39. MQTT Subscribe Sequence