#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ADDRESS "tcp://localhost:1883"
#define CLIENTID "ExampleClientSub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
int disc_finished = 0;
int subscribed = 0;
int finished = 0;
void connlost(void *context, char *cause)
{
int rc;
printf("\nConnection lost\n");
printf(" cause: %s\n", cause);
printf("Reconnecting\n");
{
printf("Failed to start connect, return code %d\n", rc);
finished = 1;
}
}
int msgarrvd(
void *context,
char *topicName,
int topicLen,
MQTTAsync_message *message)
{
int i;
char* payloadptr;
printf("Message arrived\n");
printf(" topic: %s\n", topicName);
printf(" message: ");
{
putchar(*payloadptr++);
}
putchar('\n');
return 1;
}
{
printf("Successful disconnection\n");
disc_finished = 1;
}
{
printf("Subscribe succeeded\n");
subscribed = 1;
}
{
printf(
"Subscribe failed, rc %d\n", response ? response->
code : 0);
finished = 1;
}
{
printf(
"Connect failed, rc %d\n", response ? response->
code : 0);
finished = 1;
}
{
int rc;
printf("Successful connection\n");
printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
"Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
deliveredtoken = 0;
{
printf("Failed to start subscribe, return code %d\n", rc);
exit(EXIT_FAILURE);
}
}
int main(int argc, char* argv[])
{
int rc;
int ch;
{
printf("Failed to start connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
while (!subscribed)
#if defined(WIN32) || defined(WIN64)
Sleep(100);
#else
usleep(10000L);
#endif
if (finished)
goto exit;
do
{
ch = getchar();
} while (ch!='Q' && ch != 'q');
{
printf("Failed to start disconnect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
while (!disc_finished)
#if defined(WIN32) || defined(WIN64)
Sleep(100);
#else
usleep(10000L);
#endif
exit:
return rc;
}