Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] problem In MQTT Paho C

Hello,
      I am new to MQTT. i am trying to publish and subscribe some data. but i am facing some problem. I am using eclipse paho C library. In Ubuntu, i have use following commands.

git clone https://github.com/eclipse/paho.mqtt.c.git
cd org.eclipse.paho.mqtt.c.git
make
sudo make install

it create a repository of name paho.mqtt.c. i import this repository as project in eclipse.it has directory sample, in which i create sample_publisher.c, sample_subscriber.c and sample_subscriber.h.
i have attached code of these files. when i run exe of sample_publisher, it is publishing message "Hello". but in case of sample_subscribe, sometimes it show connection lost, and sometimes it show message string "CRASHED".

I have some doubts about it. i just execute both publisher and subscriber exe. Do i need to do something to start or to connect with server. In my code, i am using Address "tcp://m2m.eclipse.org:1883". Or should i need to install server/broker in my system? 

Please have a look in to my sample code and help me to find my mistakes.

Thank You.
Shakti Gupta

#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;
}



/*
 * 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;
}


#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_ */


Back to the top