Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] client is not subscribing in paho C

Hi Shakti,

I would suggest that you use the sample codes here:  https://github.com/eclipse/paho.mqtt.c/tree/master/src/samples

Assuming that you are running on Linux (e.g. Ubuntu distribution), I would do these steps:

Clone from github:
   cd ~
   mkdir github
   git clone https://github.com/eclipse/paho.mqtt.c-b develop

Build
   cd paho.mqtt.c
   make
   (note that the output of the build will be in the build/output subdirectory
   
Run samples (open 2 terminals)
   Terminal 1: (Run as a subscriber)
       export LD_LIBRARY_PATH=/home/osboxes/github/paho.mqtt.c/build/output
       cd /home/osboxes/github/paho.mqtt.c/build/output/samples/
       ./paho_c_sub /testing/paho/c1 --host m2m.eclipse.org --qos 1 --clientid paho-mqtt-c-sub1
       
   Terminal 2: (Run as a publisher)
       export LD_LIBRARY_PATH=/home/osboxes/github/paho.mqtt.c/build/output
       cd /home/osboxes/github/paho.mqtt.c/build/output/samples/
       ./paho_c_pub /testing/paho/c1 --host m2m.eclipse.org --qos 1 --clientid paho-mqtt-c-
       
       You will see "Connecting" and "Connected",
   
   Type some messages [ENTER] in the publisher terminal, you will see those messages in the subscriber terminal
   
   

--
Regards,
Mike Tran




From:        shakti gupta <shakti.gupta07@xxxxxxxxx>
To:        paho-dev@xxxxxxxxxxx
Date:        09/29/2016 06:08 AM
Subject:        [paho-dev] client is not subscribing in paho C
Sent by:        paho-dev-bounces@xxxxxxxxxxx







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





mqtt
Add to circles


Show details

_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/paho-dev



Back to the top