Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #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 "ssl://xxxxx.azure-devices.net:8883" //tcp://localhost:1883"
- #define CLIENTID "EnergyMeter" //"ExampleClientPub"
- #define TOPIC "devices/EnergyMeter/messages/events/" //"MQTT Examples"
- #define PAYLOAD "Hello World!"
- #define QOS 1
- #define TIMEOUT 10000L
- volatile MQTTAsync_token deliveredtoken;
- int finished = 0;
- void connlost(void *context, char *cause)
- {
- MQTTAsync client = (MQTTAsync)context;
- MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
- int rc;
- printf("nConnection lostn");
- printf(" cause: %sn", cause);
- printf("Reconnectingn");
- conn_opts.keepAliveInterval = 20;
- conn_opts.cleansession = 1;
- if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
- {
- printf("Failed to start connect, return code %dn", rc);
- finished = 1;
- }
- }
- void onDisconnect(void* context, MQTTAsync_successData* response)
- {
- printf("Successful disconnectionn");
- finished = 1;
- }
- 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 confirmedn", response->token);
- opts.onSuccess = onDisconnect;
- opts.context = client;
- if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
- {
- printf("Failed to start sendMessage, return code %dn", rc);
- exit(EXIT_FAILURE);
- }
- }
- void onConnectFailure(void* context, MQTTAsync_failureData* response)
- {
- printf("Connect failed, rc %dn", response ? response->code : 0);
- finished = 1;
- }
- void onConnect(void* context, MQTTAsync_successData* response)
- {
- printf("hello");
- MQTTAsync client = (MQTTAsync)context;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- int rc;
- printf("Successful connectionn");
- opts.onSuccess = onSend;
- opts.context = client;
- pubmsg.payload = PAYLOAD;
- pubmsg.payloadlen = strlen(PAYLOAD);
- pubmsg.qos = QOS;
- pubmsg.retained = 0;
- deliveredtoken = 0;
- if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
- {
- printf("Failed to start sendMessage, return code %dn", rc);
- exit(EXIT_FAILURE);
- }
- }
- int main(int argc, char* argv[])
- {
- MQTTAsync client;
- MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
- int rc = 5;
- int a;
- a = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
- printf("a is %d", a);
- printf("Hey");
- MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL);
- printf("check1n");
- conn_opts.keepAliveInterval = 5;
- //conn_opts.username = "ssl://Ironman.azure-devices.net/stark";
- //conn_opts.password = "SharedAccessSignature sr=Ironman.azure-devices.net%2Fdevices%2Fstark&sig=eoAum3Hl2gWWiFVJT%2FhmUMwYH4NFAT%2B5fG8tRTm%2BbaA%3D&se=1558677763";
- conn_opts.cleansession = 1;
- printf("check2n");
- conn_opts.onSuccess = onConnect;
- conn_opts.username = "CCMSIoTHub.azure-devices.net/EnergyMeter/api-version=2016-11-14";
- conn_opts.password = "SharedAccessSignature sr=xxxxxxx.azure-devices.net%2Fdevices%2FEnergyMeter&sig=undNm%xxxxxxxxz87I%3D&se=xxxxxxxxx";
- printf("check3n");
- conn_opts.onFailure = onConnectFailure;
- conn_opts.context = client;
- if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
- {
- printf("Failed to start connect, return code %dn", rc);
- exit(EXIT_FAILURE);
- }
- else
- {
- printf("rc is %d ", rc);
- }
- printf("Waiting for publication of %sn"
- "on topic %s for client with ClientID: %sn",
- PAYLOAD, TOPIC, CLIENTID);
- while (!finished)
- #if defined(WIN32) || defined(WIN64)
- Sleep(100);
- #else
- usleep(10000L);
- #endif
- MQTTAsync_destroy(&client);
- return rc;
- }
Add Comment
Please, Sign In to add comment