vendredi 24 juin 2016

Paho MQTT C Client connection with azure IoT-Hub

I am trying to connect with Azure IoT-Hub with MQTT and send and receive messages using Paho C Client. I am following the official documentation given here. I'm getting the error "Failed to connect, return code -1" during connection. Below is my code. (I have masked out my IoT Hub host name intentionally). Please help me out.

FYI: I have tried the same using Paho GUI Client for Windows & Linux. Both are connecting perfectly and I'm able to send and receive data.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "MQTTClient.h"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L
#define TOPIC1       "devices/testdevice/messages/events/"
#define ADDRESS     "ssl://xxxxxxxx.azure-devices.net:8883"
#define CLIENTID1    "Manoj_Test"

int main(void)
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc1;

MQTTClient_create(&client, ADDRESS, CLIENTID1, 1, NULL);
conn_opts.cleansession = 1;
conn_opts.username = "xxxxxxxx.azure-devices.net/Manoj_Test";
conn_opts.password = "SharedAccessSignature sr=xxxxxxxx.azure-devices.net%2fdevices%2fManoj_Test&sig=GyizT%2b7uyIpOkMJjTfN%2fpOZh9CnuQedNB%2bre2NrL1Kg%3d&se=1496395529";


 MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);


if ((rc1 = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
    printf("Failed to connect, return code %dn", rc1);
    exit(-1);
}

MQTTClient_subscribe(client, TOPIC, QOS);
while(1)
{
    pubmsg.payload = PAYLOAD;
    pubmsg.payloadlen = strlen(PAYLOAD);
    pubmsg.qos = 1;
    pubmsg.retained = 0;
    MQTTClient_publishMessage(client, TOPIC1, &pubmsg, &token);
    printf("Waiting for up to %d seconds for publication of %snon topic %s for client with ClientID: %sn", (int)(TIMEOUT/1000), PAYLOAD, TOPIC1, CLIENTID1);
    rc1 = MQTTClient_waitForCompletion(client, token, TIMEOUT);
    printf("Message with delivery token %d deliveredn", token);
    usleep(100000);
}
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc1;
}

Aucun commentaire:

Enregistrer un commentaire