| Paho Asynchronous MQTT C Client Library

Loading...
Searching...
No Matches
Publication example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
#if !defined(_WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif
#if defined(_WRS_KERNEL)
#include <OsWrapper.h>
#endif
#define ADDRESS "tcp://test.mosquitto.org:1883"
#define CLIENTID "ExampleClientPub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
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 onDisconnectFailure(void* context, MQTTAsync_failureData* response)
{
printf("Disconnect failed\n");
finished = 1;
}
void onDisconnect(void* context, MQTTAsync_successData* response)
{
printf("Successful disconnection\n");
finished = 1;
}
void onSendFailure(void* context, MQTTAsync_failureData* response)
{
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
int rc;
printf("Message send failed token %d error code %d\n", response->token, response->code);
opts.onSuccess = onDisconnect;
opts.onFailure = onDisconnectFailure;
opts.context = client;
if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start disconnect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
}
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.onFailure = onDisconnectFailure;
opts.context = client;
if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start disconnect, 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");
opts.onSuccess = onSend;
opts.onFailure = onSendFailure;
opts.context = client;
pubmsg.payload = PAYLOAD;
pubmsg.payloadlen = (int)strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start sendMessage, return code %d\n", rc);
exit(EXIT_FAILURE);
}
}
int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* m)
{
// not expecting any messages
return 1;
}
int main(int argc, char* argv[])
{
MQTTAsync client;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
int rc;
if ((rc = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTASYNC_SUCCESS)
{
printf("Failed to create client object, return code %d\n", rc);
exit(EXIT_FAILURE);
}
if ((rc = MQTTAsync_setCallbacks(client, NULL, connlost, messageArrived, NULL)) != MQTTASYNC_SUCCESS)
{
printf("Failed to set callback, return code %d\n", rc);
exit(EXIT_FAILURE);
}
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;
}
MQTTAsync.h
MQTTAsync_connect
int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions *options)
MQTTAsync
void * MQTTAsync
Definition MQTTAsync.h:258
MQTTAsync_disconnectOptions_initializer
#define MQTTAsync_disconnectOptions_initializer
Definition MQTTAsync.h:1484
MQTTAsync_responseOptions_initializer
#define MQTTAsync_responseOptions_initializer
Definition MQTTAsync.h:776
MQTTAsync_create
int MQTTAsync_create(MQTTAsync *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
MQTTAsync_sendMessage
int MQTTAsync_sendMessage(MQTTAsync handle, const char *destinationName, const MQTTAsync_message *msg, MQTTAsync_responseOptions *response)
MQTTAsync_message_initializer
#define MQTTAsync_message_initializer
Definition MQTTAsync.h:339
MQTTAsync_destroy
void MQTTAsync_destroy(MQTTAsync *handle)
MQTTAsync_disconnect
int MQTTAsync_disconnect(MQTTAsync handle, const MQTTAsync_disconnectOptions *options)
MQTTAsync_connectOptions_initializer
#define MQTTAsync_connectOptions_initializer
Definition MQTTAsync.h:1390
MQTTAsync_setCallbacks
int MQTTAsync_setCallbacks(MQTTAsync handle, void *context, MQTTAsync_connectionLost *cl, MQTTAsync_messageArrived *ma, MQTTAsync_deliveryComplete *dc)
MQTTASYNC_SUCCESS
#define MQTTASYNC_SUCCESS
Definition MQTTAsync.h:118
MQTTCLIENT_PERSISTENCE_NONE
#define MQTTCLIENT_PERSISTENCE_NONE
Definition MQTTClientPersistence.h:74
MQTTAsync_connectOptions
Definition MQTTAsync.h:1203
MQTTAsync_connectOptions::cleansession
int cleansession
Definition MQTTAsync.h:1249
MQTTAsync_connectOptions::onFailure
MQTTAsync_onFailure * onFailure
Definition MQTTAsync.h:1300
MQTTAsync_connectOptions::onSuccess
MQTTAsync_onSuccess * onSuccess
Definition MQTTAsync.h:1294
MQTTAsync_connectOptions::keepAliveInterval
int keepAliveInterval
Definition MQTTAsync.h:1227
MQTTAsync_connectOptions::context
void * context
Definition MQTTAsync.h:1306
MQTTAsync_disconnectOptions
Definition MQTTAsync.h:1434
MQTTAsync_disconnectOptions::onFailure
MQTTAsync_onFailure * onFailure
Definition MQTTAsync.h:1455
MQTTAsync_disconnectOptions::onSuccess
MQTTAsync_onSuccess * onSuccess
Definition MQTTAsync.h:1449
MQTTAsync_disconnectOptions::context
void * context
Definition MQTTAsync.h:1461
MQTTAsync_failureData
Definition MQTTAsync.h:541
MQTTAsync_failureData::code
int code
Definition MQTTAsync.h:545
MQTTAsync_failureData::token
MQTTAsync_token token
Definition MQTTAsync.h:543
MQTTAsync_message
Definition MQTTAsync.h:277
MQTTAsync_message::qos
int qos
Definition MQTTAsync.h:300
MQTTAsync_message::retained
int retained
Definition MQTTAsync.h:319
MQTTAsync_message::payload
void * payload
Definition MQTTAsync.h:286
MQTTAsync_message::payloadlen
int payloadlen
Definition MQTTAsync.h:284
MQTTAsync_responseOptions
Definition MQTTAsync.h:714
MQTTAsync_responseOptions::onFailure
MQTTAsync_onFailure * onFailure
Definition MQTTAsync.h:731
MQTTAsync_responseOptions::onSuccess
MQTTAsync_onSuccess * onSuccess
Definition MQTTAsync.h:725
MQTTAsync_responseOptions::context
void * context
Definition MQTTAsync.h:737
MQTTAsync_successData
Definition MQTTAsync.h:576
MQTTAsync_successData::token
MQTTAsync_token token
Definition MQTTAsync.h:578