I'm trying to send and receive messages using azure cloud but directly on paho-mqtt and not on azure-mqtt SDK in a Raspberry-pi 3. I have followed all the instructions mentioned here. I have generated the SSL certs and have placed in the /etc/certs file path also. I have made necessary changes in the code like ClientID,Address,etc. but when I try to run the code, I get an error.
error : Failed to connect, return code -1
Here is the code,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
#define ADDRESS "xxxxxxxx.azure-devices.net:8883"
#define CLIENTID "Device-Id"
#define TOPIC "devices/Device-Id/messages/events/"
#define PAYLOAD "Hello World!"
#define QOS 0
#define TIMEOUT 10000L
int main(int argc, char* argv[])
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;
MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = "xxxxxx.azure-devices.net/Device-ID/api-version=2017-11-25";
conn_opts.password = "SharedAccessSignature sr=xxxxx.azure-devices.net&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5&skn=Device-ID";
conn_opts.ssl->truststore = "/etc/certs/ca.crt";
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
pubmsg.payload = PAYLOAD;
pubmsg.payloadlen = strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
printf("Waiting for up to %d seconds for publication of %s\n"
"on topic %s for client with ClientID: %s\n",
(int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message with delivery token %d delivered\n", token);
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc;
}
I'm not sure what is wrong here. Help me out. Thanks in advance.