Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Client is not publishing in paho C(Client is not publishing in paho C)
Client is not publishing in paho C [message #1745282] Fri, 07 October 2016 09:20
shakti gupta is currently offline shakti guptaFriend
Messages: 4
Registered: September 2016
Junior Member
Hello,
i am using a sample code for Mqtt publisher and subscriber. Sometimes
Mqtt Publisher not publish the data. Please have a lokkn into code and help me to resolve.

Thank You.
Shakti Gupta

/*******************************************************************************
* Copyright (c) 2012, 2013 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial contribution
*******************************************************************************/

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "MQTTAsync.h"
#include "Mqtt_AsyncSample.h"


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

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


//struct Sample_data *data;


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 lost\n");
printf(" cause: %s\n", cause);

printf("Reconnecting\n");
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start connect, return code %d\n", rc);
finished = 1;
}
}


void onDisconnect(void* context, MQTTAsync_successData* response)
{
printf("Successful disconnection\n");
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 confirmed\n", response->token);

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

if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start sendMessage, return code %d\n", rc);
exit(EXIT_FAILURE);
}
}


void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
printf("Connect failed, rc %d\n", response ? response->code : 0);
finished = 1;
}


void onConnect(void* context, MQTTAsync_successData* response)
{

MQTTAsync client = (MQTTAsync)context;
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
int rc;
printf("Successful connection\n");
int ch;
int EmpId;
char name[20];
float Val;
char Msg[50];
int len;
// data=(struct Sample_data*)malloc(sizeof(struct Sample_data));
opts.onSuccess = onSend;
opts.context = client;
printf("Please enter your employeeId \n");
scanf("%d",&EmpId);
ch=getchar();
printf("Please enter your name \n");
fgets(name,sizeof(name),stdin);
len=strlen(name)-1;
if(name[len]=='\n')
name[len]='\0';
// scanf("%s",name);
printf("please enter the value \n");
scanf("%f",&Val);
sprintf(Msg,"%d %s %f",EmpId,name,Val);
printf("message is.... \n");
puts(Msg);
/*data->Empid=10;
data->val=20;

strcpy(data->str,"Hello");*/
// printf("data_name is %s",data->name);
pubmsg.payload = (void *)Msg;
pubmsg.payloadlen = sizeof(Msg);
pubmsg.qos = QOS;
pubmsg.retained = 0;
deliveredtoken = 0;


// data1=(struct Sample_data *)pubmsg.payload;
// printf("2222 \n");
// printf("data_emp is %d",data1->Empid);
// printf("data_val is %d",data1->val);
// printf("data_str is %s",data1->str);
if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start sendMessage, return code %d\n", rc);
exit(EXIT_FAILURE);
}
else
printf("return value is %d",rc);
}


int main(int argc, char* argv[])
{
MQTTAsync client;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
MQTTAsync_token token;
int rc;
MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);

MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL);

conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.onSuccess = onConnect;
conn_opts.onFailure = onConnectFailure;
conn_opts.context = client;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}

printf("Waiting for publication of %s\n"
"on topic %s for client with ClientID: %s\n",
PAYLOAD, TOPIC, CLIENTID);
while (!finished)
#if defined(WIN32)
Sleep(100);
#else
usleep(10000L);
#endif

MQTTAsync_destroy(&client);
return rc;
}



Previous Topic:MQTT(mosquitto-paho) does not published temperature data
Next Topic:how to connect Mqtt paho C application to Bluemix
Goto Forum:
  


Current Time: Thu Apr 18 14:30:40 GMT 2024

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

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

Back to the top