Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Client is not subscribing in paho C(Client is not subscribing in paho C)
Client is not subscribing in paho C [message #1744661] Thu, 29 September 2016 10:58
shakti gupta is currently offline shakti guptaFriend
Messages: 4
Registered: September 2016
Junior Member
Hi,
I am beginner in MQTT. i am using eclipse paho C. i am facing some problem in a sample program. Please have a look into code. i have pasted code and output of publisher and subscriber below. In subscriber, it show some corrupted string and then it lost connection. Please help me to find mistake.

sample_publisher.c

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "MQTTClient.h"

#define ADDRESS "tcp://m2m.eclipse.org:1883"
#define CLIENTID "ExampleClientPub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hellooooooo"
#define QOS 1
#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;
int status;
char* str="HELLO";

MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;

if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(-1);
}
pubmsg.payload = (void*)str;
pubmsg.payloadlen = strlen(str);
pubmsg.qos = QOS;
pubmsg.retained = 0;
printf("lenght is %d", pubmsg.payloadlen);
status=MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
printf("status is %d \n",status);
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;
}

Output of Sample_publish

./sample_publish
lenght is 5status is 0
Waiting for up to 10 seconds for publication of Hellooooooo
on topic MQTT Examples for client with ClientID: ExampleClientPub
Message with delivery token 1 delivered



Sample_subscribe.c

/*
* sample_subscribe.c
*
* Created on: 22-Sep-2016
* Author: shakti
*/

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "MQTTClient.h"
#include "sample_subscribe.h"

#define ADDRESS "tcp://m2m.eclipse.org:1883"
#define CLIENTID "ExampleClientPub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hellooooooo"
#define QOS 1
#define TIMEOUT 10000L


int main(int argc, char* argv[])
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
int rc;
int ch;
//a. Create an instance of MQTT client
MQTTClient_create(&client, ADDRESS, CLIENTID,MQTTCLIENT_PERSISTENCE_NONE, NULL);
//b. Prepare connection options
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
//c. Connect to broker with the connection options
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(-1);
}
//d. Subscribe interested topics.
printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
"Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
MQTTClient_subscribe(client, TOPIC, QOS);
do
{
ch = getchar();
} while(ch!='Q' && ch != 'q');
//MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
//e. Disconnect to broker
MQTTClient_disconnect(client, 10000);
//f. Release resources
MQTTClient_destroy(&client);
return rc;
}


Sample_subscribe.h


#ifndef SAMPLES_SAMPLE_SUBSCRIBE_H_
#define SAMPLES_SAMPLE_SUBSCRIBE_H_

volatile MQTTClient_deliveryToken deliveredtoken;
void delivered(void *context, MQTTClient_deliveryToken dt)
{
printf("Message with token value %d delivery confirmed\n", dt);
deliveredtoken = dt;
}

int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message* message)
{
int i;
char* payloadptr;
printf("Message arrived\n");
printf(" topic: %s\n", topicName);
printf(" message: ");
printf("address of message is %u \n",message);
payloadptr = (char*)message->payload;
printf("11111 \n");
printf("message is.... %s",payloadptr);
for(i=0; i<message->payloadlen; i++)
{
printf("2222 \n");
putchar(*payloadptr++);
printf("33333 \n");
}
putchar('\n');
if(message!=NULL)
{
printf("555 \n");
MQTTClient_freeMessage(&message);
printf("666 \n");
}
//free(topicName);
printf("4444 \n");
return 1;
}

void connlost(void *context, char *cause)
{
printf("\nConnection lost\n");
printf(" cause: %s\n", cause);
}

#endif /* SAMPLES_SAMPLE_SUBSCRIBE_H_ */


Output of sample_subscribe

./sample_subscribe
Subscribing to topic MQTT Examples
for client ExampleClientPub using QoS1

Press Q<Enter> to quit

Message arrived
topic: MQTT Examples
message: address of message is 2281704372
11111
message is.... crashedket.c2222
c33333
2222
r33333
2222
a33333
2222
s33333
2222
h33333
2222
e33333
2222
d33333

555
666
4444

Connection lost

cause (null)



Thank You.
Shakti Gupta





Previous Topic:How to NOT acknowledge a message
Next Topic:Mqtt websocket using c++ release plan
Goto Forum:
  


Current Time: Tue Nov 20 05:25:12 GMT 2018

Powered by FUDForum. Page generated in 0.02252 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top