Step 4 : Connect to MQTT broker¶
Once successfully registered to CL Cloud, you are granted to connect to CL MQTT Broker to send informations and receive commands.
You will have to use informations received during Registration step to connect to MQTT Broker
Example¶
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <cl.h>
#define VERSION "0.1.0"
#define REGION "dev"
#define MQTT_USERNAME "vdp_874707ce2b8c0f0c187f143ee9a0dc14"
#define MQTT_PASSWORD "70dde408c087991673d9"
#define DEVICE_ID "fa8652f4c7569b946db446f51c4502ac"
#define PLANT_ID "plant_db"
void help(const char *app_name);
void mqtt_connection_status_handler(const cl_mqtt_conn_event_t status)
{
switch (status)
{
case CL_MQTT_CONN_CONNECTED:
printf("Connection etablished.\n");
break;
case CL_MQTT_CONN_DISCONNECTED:
printf("Connection stopped.\n");
break;
}
}
void mqtt_data_handler(const unsigned char *str_json, size_t length)
{
printf("Data received: %s\n", str_json);
/*
* Example informations json to send to the server
* You can send informations any time
{
"event": "alert",
"version": "0.1.0",
"uid": "[COMMAND_UNIQUE_ID]",
"modules": [{
"id": "[DEVICE_ID]",
"status": [
{
"capability": "call",
"value": "pending"
}
]
}]
}*/
static int command_id = 0;
char command_id_str[10] = {0};
sprintf(command_id_str, "%d", ++command_id);
char data_to_send[1024];
sprintf(data_to_send,
"{\"event\":\"%s\",\"version\":\"%s\",\"uid\":\"%s\",\"modules\":[{\"id\":\"%s\",\"status\":[{\"capability\":\"call\",\"value\":\"pending\"}]}]}",
"alert",
VERSION,
command_id_str,
DEVICE_ID);
int result = cl_mqtt_send_data(data_to_send);
switch (result)
{
case CL_SUCCESS:
printf("Data sent.\n");
break;
case CL_FAILURE:
default:
printf("Data not sent.\n");
break;
}
}
int main(int argc, char **argv)
{
int c;
while ((c = getopt (argc, argv, "vh?")) != -1)
{
switch(c)
{
case 'v':
printf("%s", VERSION);
exit(1);
break;
case 'h':
case '?':
default:
help(argv[0]);
}
}
cl_return_t result;
// INIT
cl_mqtt_conn_t mqtt_conn;
strcpy(mqtt_conn.region, REGION);
strcpy(mqtt_conn.username, MQTT_USERNAME);
strcpy(mqtt_conn.password, MQTT_PASSWORD);
strcpy(mqtt_conn.device_id, DEVICE_ID);
strcpy(mqtt_conn.plant_id, PLANT_ID);
printf("[.]. Initialize context...\n");
strcpy(cl_handle.developer_key, DEVELOPER_KEY);
strcpy(cl_handle.iot_brand_name, IOT_BRAND_NAME);
strcpy(cl_handle.iot_model_name, IOT_MODEL_NAME);
strcpy(cl_handle.iot_submodel_name, IOT_SUBMODEL_NAME);
strcpy(cl_handle.iot_system, IOT_SYSTEM);
result = cl_mqtt_init(&cl_handle);
switch (result) {
case CL_SUCCESS:
printf("[+]. Mqtt initialized.\n");
break;
case CL_DEVICE_NOT_FOUND:
case CL_CONNECTION_FAILED:
case CL_FAILURE:
default:
printf("[-]. Failed during initializing mqtt.\n");
break;
}
cl_mqtt_get_connection_status(mqtt_connection_status_handler);
cl_mqtt_get_data(mqtt_data_handler);
#if 1
cl_mqtt_verbose();
#endif
// CONNECT
result = cl_mqtt_connect(mqtt_conn);
switch (result) {
case CL_SUCCESS:
printf("[.]. Connection ended.\n");
break;
case CL_DEVICE_NOT_FOUND:
case CL_CONNECTION_FAILED:
case CL_FAILURE:
printf("[-]. Connection failed.\n");
break;
}
// deinit
cl_mqtt_deinit();
return 0;
}
// ******************************************
void help(const char *app_name)
{
printf("%s v%s.\n\n", app_name, VERSION);
printf("Daemon for the communication with the server.\n");
printf("This application listens the commands coming from the server.\n\n");
printf("Usage: %s [-h|-?] [-v]\n\n", app_name);
printf(" -h This help\n");
printf(" -v Get the version\n\n");
exit(1);
}