I want to integrate the [Paho MQTT C Client Library][1] to one of my C programs. Since I am from EE, I find it difficult achieve this task. However my efforts are described below along with my problem.
The purpose is to simply use username, password and subscribe to perticular MQTT topic using a C program. If this is done, I can proceed to do the rest of things such as saving the data to a .txt file and etc which I am completely familiar with in C.
Since I am from an electronics background, my familiarity with compiling complex projects like these and making them work is not really at best but I really want to get there.
I do know how to compile small projects using "make all" and use the binary executable produced to use such software. I simply do, make clean and make all.
However, the idea of compiling a library doesn't make sense to me. Why do I need to compile any library in the first place? Usually, when I write C program, I integrate someone else's library by uisng #include "library2.h". From this method, I am able to call functions in that library and get things done. Why cant we use Paho the same way? I do not understand why Paho MQTT C library requires compiling. I would like to know the technical reason for this.
Secondly, and most importantly, I would really appreciate if you can provide me step by step guidance to write a simple C program that can subscribe to my MQTT server to printout the messages published in that topic. I use Ubuntu 14.10 LTS.
I understand that this question may be a very basic question. I have asked answers to this question from other people in the lab and also tried fiddling with the example available on [1]
When I do so, I am bombarded with many error messages and I just cant seem to get this to work. May be one of you can shred some light with proper guiding steps for me to get my client to work.
I would really appreciate your efforts on this. It would help me a lot.
update: As per request of Gaurav Pathak I post my erros below.
Step 1: I downloaded the coursecode of Paho MQTT libray from github dot com/eclipse/paho.mqtt.c
Step 2: I went into the /home/user/paho.mqtt.c/ and ran Make Clean and Make All
Step 3: Then, I made a copy of the following example code provided on http://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/subasync.html in side /home/user/paho.mqtt.c/src . This example code is given below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
#define ADDRESS "tcp://localhost:1883"
#define CLIENTID "ExampleClientSub"
#define TOPIC "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 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: ");
payloadptr = 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 lost\n");
printf(" cause: %s\n", cause);
}
int main(int argc, char* argv[])
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
int rc;
int ch;
MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
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_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc;
}
Step 3: Then I ran GCC client.c -o client
user@userpc:~/paho.mqtt.c/src$ gcc client.c -o client
/tmp/ccEkSjap.o: In function `msgarrvd':
client.c:(.text+0xc5): undefined reference to `MQTTClient_freeMessage'
client.c:(.text+0xd1): undefined reference to `MQTTClient_free'
/tmp/ccEkSjap.o: In function `main':
client.c:(.text+0x1eb): undefined reference to `MQTTClient_create'
client.c:(.text+0x21d): undefined reference to `MQTTClient_setCallbacks'
client.c:(.text+0x233): undefined reference to `MQTTClient_connect'
client.c:(.text+0x29a): undefined reference to `MQTTClient_subscribe'
client.c:(.text+0x2cb): undefined reference to `MQTTClient_disconnect'
client.c:(.text+0x2da): undefined reference to `MQTTClient_destroy'
collect2: error: ld returned 1 exit status
[1]: eclipse dot org/paho/files/mqttdoc/MQTTClient/html/index.html