#include "kit_mqtt.h" #define ADDRESS "tcp://47.120.14.45:3011" #define CLIENTID "ExampleClientPub" #define TOPIC "aa" #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"); if (cause) 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; int rc; printf("Successful connection\n"); cJSON *json_object = cJSON_CreateObject(); cJSON_AddStringToObject(json_object, "manage", "xudex"); cJSON_AddStringToObject(json_object, "employee", "wangk"); cJSON_AddBoolToObject(json_object, "ok or not", 0); char *json_string = cJSON_Print(json_object); if (json_string == NULL) { fprintf(stderr, "Failed to print cJSON.\n"); exit(EXIT_FAILURE); } MQTTAsync_message pubmsg = MQTTAsync_message_initializer; MQTTAsync_responseOptions resp_opts = MQTTAsync_responseOptions_initializer; resp_opts.onSuccess = onSend; resp_opts.onFailure = onSendFailure; resp_opts.context = client; pubmsg.payload = (void *)json_string; pubmsg.payloadlen = strlen(json_string); pubmsg.qos = QOS; pubmsg.retained = 0; rc = MQTTAsync_sendMessage(client, "aa", &pubmsg, &resp_opts); if (rc != MQTTASYNC_SUCCESS) { fprintf(stderr, "Failed to send message, return code %d\n", rc); exit(EXIT_FAILURE); } free(json_string); cJSON_Delete(json_object); } int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* m) { /* not expecting any messages */ return 1; } int pubClient() { 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, client, 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\n" "on topic %s for client with ClientID: %s\n", TOPIC, CLIENTID); while (!finished) { sleep(1); } MQTTAsync_destroy(&client); return rc; }