1.2.3.15 SYS_MQTT_Connect Function

C

SYS_MODULE_OBJSYS_MODULE_OBJ* SYS_MQTT_Connect(SYS_MQTT_Config *cfg,
SYS_MQTT_CALLBACK fn MqttFn,
void *cookie);

Summary

Connects to the configured MQTT Broker.

Description

This function opens a new instance and connects to the configured MQTT Broker.

Parameters

cfg - Configuration based on which the Cloud Service needs to Open MqttFn - Function pointer to the Callback to be called in case of an event cookie - Cookie passed as one of the params in the Callback for the user to identify the service instance

Returns

If successful, returns a valid handle to an object. Otherwise, it returns SYS_MODULE_OBJ_INVALID.

Example

SYS_MQTT_Config 	g_sMqttSrvcCfg;
SYS_MODULE_OBJ 			g_MqttSrvcHandle;

memset(&g_sMqttSrvcCfg, 0, sizeof(g_sMqttSrvcCfg));

g_sMattSrvcCfg.configBitmask |= SYS_MQTT_CONFIG_MASK_MQTT;

strcpy(g_sMqttSrvcCfg.mqttConfig.brokerConfig.brokerName,
"test.mosquitto.org", strlen("test.mosquitto.org"));

g_sMqttSrvcCfg.mqttConfig.brokerConfig.serverPort = 1883;

strcpy(g_sMqttSrvcCfg.mqttConfig.brokerConfig.clientId,
"pic32mzw1", strlen("pic32maw1"));

g_sMqttSrvcCfg.mqttConfig.brokerConfig.autoConnect = 1;

g_sMqttSrvcCfg.mqttConfig.brokerConfig.tlsEnabled = 0;

g_sMqttSrvcCfg.mqttConfig.subscribeCount = 1;

strcpy(g_sMqttSrvcCfg.mqttConfig.subscribeConfig[0].topicName,
"house/temperature/first_floor/kitchen",
strlen("house/temperature/first_floor/kitchen"));

g_sMqttSrvcCfg.mqttConfig.subscribeConfig[0].qos = 1;

g_MqttSrvcHandle = SYS_MQTT_Connect(&g_sMqttSrvcCfg, MqttSrvcCallback, 0);
if (g_MqttSrvcHandle == SYS_MODULE_OBJ_INVALID)
{
    // Handle error
}

Remarks

This routine should be called only once when the user is configuring the Mqtt service