Publish Message to IOT Hub - Eclypse Paho MQTT

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Publish Message to IOT Hub - Eclypse Paho MQTT



Below is my code for connecting and sending data from device to cloud using Eclypse Paho MQTT Library. I am able to connect with IOT Hub as rc results 0 in my case but It doesn't send message to my IOT Hub device. Can anyone please guide me where I have made mistake or what I am doing wrong ?
I am using Visual studio 2015 with .Net framework 4.0 and eclipse-paho-mqtt-c-windows-1.0.3 libraries


#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"

#if !defined(WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif

#if defined(_WRS_KERNEL)
#include <OsWrapper.h>
#endif

#define ADDRESS "ssl://xxxxx.azure-devices.net:8883" //tcp://localhost:1883"
#define CLIENTID "EnergyMeter" //"ExampleClientPub"
#define TOPIC "devices/EnergyMeter/messages/events/" //"MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L

volatile MQTTAsync_token deliveredtoken;

int finished = 0;

void connlost(void *context, char *cause)

MQTTAsync client = (MQTTAsync)context;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
int rc;

printf("nConnection lostn");
printf(" cause: %sn", cause);

printf("Reconnectingn");
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)

printf("Failed to start connect, return code %dn", rc);
finished = 1;




void onDisconnect(void* context, MQTTAsync_successData* response)

printf("Successful disconnectionn");
finished = 1;



void onSend(void* context, MQTTAsync_successData* response)

MQTTAsync client = (MQTTAsync)context;
MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
int rc;

printf("Message with token value %d delivery confirmedn", response->token);

opts.onSuccess = onDisconnect;
opts.context = client;

if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)

printf("Failed to start sendMessage, return code %dn", rc);
exit(EXIT_FAILURE);




void onConnectFailure(void* context, MQTTAsync_failureData* response)

printf("Connect failed, rc %dn", response ? response->code : 0);
finished = 1;



void onConnect(void* context, MQTTAsync_successData* response)

printf("hello");
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
int rc;

printf("Successful connectionn");

opts.onSuccess = onSend;
opts.context = client;

pubmsg.payload = PAYLOAD;
pubmsg.payloadlen = strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
deliveredtoken = 0;

if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)

printf("Failed to start sendMessage, return code %dn", rc);
exit(EXIT_FAILURE);




int main(int argc, char* argv)
defined(WIN64)
Sleep(100);
#else
usleep(10000L);
#endif


MQTTAsync_destroy(&client);
return rc;





Is this C or C++? It looks like C.
– NathanOliver
Jun 26 at 14:30





Yes, It is C...
– Parth Desai
Jun 26 at 14:31





@ParthDesai Did you find any solution? Even i got stucked because of same issue
– Candy
Aug 7 at 14:09





@Candy yes I have solved you have to use Baltimore as Root CA Certificate. if wanted I can mail you sample code for reference
– Parth Desai
Aug 7 at 18:58





@ParthDesai If you could answer your question that will be helpful or can you please mail me some reference.
– Candy
Aug 8 at 4:49




1 Answer
1



Below Code works fine In my case


#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
#define ADDRESS "ssl://xxxxxxxxxx.azure-devices.net:8883" //tcp://localhost:1883"
#define CLIENTID "xxxxxxxxxx" //"ExampleClientPub"
#define TOPIC "devices/xxxxxxxxxx/messages/events/" //"MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
volatile MQTTClient_deliveryToken deliveredtoken;
void delivered(void *context, MQTTClient_deliveryToken dt)

printf("Message with token value %d delivery confirmedn", dt);
deliveredtoken = dt;

int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)

int i;
char* payloadptr;
printf("Message arrivedn");
printf(" topic: %sn", topicName);
printf(" message: ");
payloadptr = (char *)message->payload;
for (i = 0; i<message->payloadlen; i++)

putchar(*payloadptr++);

putchar('n');
MQTTClient_freeMessage(&message);
MQTTClient_free(topicName);
return 1;

void connlost(void *context, char *cause)

printf("nConnection lostn");
printf(" cause: %sn", cause);

#define TEST_SSL
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;
rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
1, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = "xxxxxxxxxx.azure-devices.net/xxxxxxxxxx/api-version=2016-11-14";
conn_opts.password = "SharedAccessSignature sr=xxxxxxxxxx.azure-devices.net%2Fdevices%2Fxxxxxxxxxx&sig=undNm%2Fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=15xxxxxx";

#ifdef TEST_SSL
MQTTClient_SSLOptions sslOptions = MQTTClient_SSLOptions_initializer;
sslOptions.enableServerCertAuth = 1;
sslOptions.trustStore = "D:/Parth/Projects/MQTT/MQTT Sample D2C Azure IOT Hub Tested Program/rootCA.crt";
conn_opts.ssl = &sslOptions;
#endif

//conn_opts.ssl->trustStore = "D:ParthProjectsMQTTpaho.mqtt.c-mastertestssltest - alt - ca.crt";

rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)

printf("Failed to connect, return code %dn", rc);
exit(EXIT_FAILURE);

pubmsg.payload = PAYLOAD;
pubmsg.payloadlen = strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
deliveredtoken = 0;
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
printf("Waiting for publication of %sn"
"on topic %s for client with ClientID: %sn",
PAYLOAD, TOPIC, CLIENTID);
while (deliveredtoken != token);
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc;





Thanks for solution. It helped me in solving my issue
– Candy
Aug 8 at 8:03






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard