向物联网中心发布消息-Eclypse Paho MQTT
下面是我使用Eclypse Paho MQTT库将数据从设备连接并发送到云的代码。在我的情况下,当rc结果为0时,我能够连接IOT Hub,但它不会向我的IOT Hub设备发送消息。有谁能告诉我哪里出了错或者我做错了什么? 我正在将Visual studio 2015与.Net framework 4.0和eclipse-paho-mqtt-c-windows-1.0.3库一起使用向物联网中心发布消息-Eclypse Paho MQTT,c,azure,mqtt,C,Azure,Mqtt,下面是我使用Eclypse Paho MQTT库将数据从设备连接并发送到云的代码。在我的情况下,当rc结果为0时,我能够连接IOT Hub,但它不会向我的IOT Hub设备发送消息。有谁能告诉我哪里出了错或者我做错了什么? 我正在将Visual studio 2015与.Net framework 4.0和eclipse-paho-mqtt-c-windows-1.0.3库一起使用 #include "stdafx.h" #include <stdio.h> #include <
#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 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 onDisconnect(void* context, MQTTAsync_successData* response)
{
printf("Successful disconnection\n");
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 confirmed\n", response->token);
opts.onSuccess = onDisconnect;
opts.context = client;
if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start sendMessage, 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)
{
printf("hello");
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.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 %d\n", 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("check1\n");
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("check2\n");
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("check3\n");
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);
}
else
{
printf("rc is %d ", rc);
}
printf("Waiting for publication of %s\n"
"on topic %s for client with ClientID: %s\n",
PAYLOAD, TOPIC, CLIENTID);
while (!finished)
#if defined(WIN32) || defined(WIN64)
Sleep(100);
#else
usleep(10000L);
#endif
MQTTAsync_destroy(&client);
return rc;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括“MQTTAsync.h”
#如果!已定义(WIN32)
#包括
#否则
#包括
#恩迪夫
#如果已定义(_WRS_内核)
#包括
#恩迪夫
#“定义地址”ssl://xxxxx.azure-devices.net:8883" //tcp://localhost:1883"
#定义客户端ID“EnergyMeter”/“ExampleClientPub”
#定义主题“设备/能量计/消息/事件/”/“MQTT示例”
#定义有效载荷“你好,世界!"
#定义QOS 1
#定义超时10000L
易失性MQTTAsync_令牌deliveredtoken;
int=0;
void connlost(void*上下文,char*原因)
{
MQTTAsync客户端=(MQTTAsync)上下文;
MQTTAsync_connectOptions conn_opts=MQTTAsync_connectOptions_初始值设定项;
int rc;
printf(“\n连接丢失\n”);
printf(“原因:%s\n”,原因);
printf(“重新连接”);
conn_opts.keepAliveInterval=20;
conn_opts.cleansession=1;
if((rc=MQTTAsync\u连接(客户端和连接选项))!=MQTTAsync\u成功)
{
printf(“启动连接失败,返回代码%d\n”,rc);
完成=1;
}
}
void onDisconnect(void*上下文,MQTTAsync_successData*响应)
{
printf(“成功断开连接”);
完成=1;
}
void onSend(void*上下文,MQTTAsync_successData*响应)
{
MQTTAsync客户端=(MQTTAsync)上下文;
MQTTAsync_disconnectOptions opts=MQTTAsync_disconnectOptions_初始值设定项;
int rc;
printf(“令牌值为%d的邮件已确认传递,\n”,响应->令牌);
opts.onSuccess=onDisconnect;
opts.context=客户端;
if((rc=MQTTAsync\u断开连接(客户端和选项))!=MQTTAsync\u成功)
{
printf(“启动sendMessage失败,返回代码%d\n”,rc);
退出(退出失败);
}
}
void onConnectFailure(void*上下文,MQTTAsync_failureData*响应)
{
printf(“连接失败,rc%d\n”,响应?响应->代码:0);
完成=1;
}
void onConnect(void*上下文,MQTTAsync_successData*响应)
{
printf(“你好”);
MQTTAsync客户端=(MQTTAsync)上下文;
MQTTAsync_responseOptions opts=MQTTAsync_responseOptions_初始值设定项;
MQTTAsync_message pubmsg=MQTTAsync_message_初始值设定项;
int rc;
printf(“成功连接”);
opts.onSuccess=onSend;
opts.context=客户端;
publimsg.payload=有效载荷;
publimsg.payloadlen=strlen(有效载荷);
publimsg.qos=qos;
publimsg.reserved=0;
deliveredtoken=0;
if((rc=MQTTAsync_sendMessage(客户端、主题、发布消息和选项))!=MQTTAsync_成功)
{
printf(“启动sendMessage失败,返回代码%d\n”,rc);
退出(退出失败);
}
}
int main(int argc,char*argv[])
{
MQTTAsync客户端;
MQTTAsync_connectOptions conn_opts=MQTTAsync_connectOptions_初始值设定项;
int rc=5;
INTA;
a=MQTTAsync_create(&client,ADDRESS,CLIENTID,MQTTCLIENT_PERSISTENCE_NONE,NULL);
printf(“a是%d”,a);
printf(“嘿”);
MQTTAsync_setCallbacks(client,NULL,connlost,NULL,NULL);
printf(“check1\n”);
conn_opts.keepAliveInterval=5;
//conn_opts.username=”ssl://Ironman.azure-devices.net/stark";
//conn_opts.password=“SharedAccessSignature sr=Ironman.azure devices.net%2Fdevices%2FSTACK&sig=eoAum3Hl2gWWiFVJT%2FhmUMwYH4NFAT%2B5fG8tRTm%2BbaA%3D&se=1558677763”;
conn_opts.cleansession=1;
printf(“check2\n”);
conn_opts.onSuccess=onConnect;
conn_opts.username=“CCMSIoTHub.azure devices.net/EnergyMeter/api version=2016-11-14”;
conn_opts.password=“SharedAccessSignature sr=xxxxxxx.azure设备.net%2Devices%2EnergyMeter&sig=undNm%xxxxxxx z87i%3D&se=xxxxxxxxx”;
printf(“check3\n”);
conn_opts.onFailure=onConnectFailure;
conn_opts.context=客户端;
if((rc=MQTTAsync\u连接(客户端和连接选项))!=MQTTAsync\u成功)
{
printf(“启动连接失败,返回代码%d\n”,rc);
退出(退出失败);
}
其他的
{
printf(“rc为%d”,rc);
}
printf(“正在等待发布%s\n”
“在客户端ID为%s的客户端的主题%s上\n”,
有效载荷、主题、客户ID);
当(!完成)
#如果已定义(WIN32)| |已定义(WIN32)
睡眠(100);
#否则
usleep(10000升);
#恩迪夫
MQTTAsync_销毁(&C)客户端;
返回rc;
}
以下代码在我的情况下运行良好
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
#define ADDRESS "ssl://xxxxxxxxxx.azure-devices.net:8883" //tcp://localhost:1883"
#define CLIENTID "xxxxxxxxxx" //"ExampleClientPub"
#define TOPIC "devices/xxxxxxxxxx/messages/events/" //"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 = (char *)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);
}
#define TEST_SSL
int main(int argc, char* argv[])
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;
rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
1, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = "xxxxxxxxxx.azure-devices.net/xxxxxxxxxx/api-version=2016-11-14";
conn_opts.password = "SharedAccessSignature sr=xxxxxxxxxx.azure-devices.net%2Fdevices%2Fxxxxxxxxxx&sig=undNm%2Fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=15xxxxxx";
#ifdef TEST_SSL
MQTTClient_SSLOptions sslOptions = MQTTClient_SSLOptions_initializer;
sslOptions.enableServerCertAuth = 1;
sslOptions.trustStore = "D:/Parth/Projects/MQTT/MQTT Sample D2C Azure IOT Hub Tested Program/rootCA.crt";
conn_opts.ssl = &sslOptions;
#endif
//conn_opts.ssl->trustStore = "D:\Parth\Projects\MQTT\paho.mqtt.c-master\test\ssl\test - alt - ca.crt";
rc = 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);
}
pubmsg.payload = PAYLOAD;
pubmsg.payloadlen = strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
deliveredtoken = 0;
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
printf("Waiting for publication of %s\n"
"on topic %s for client with ClientID: %s\n",
PAYLOAD, TOPIC, CLIENTID);
while (deliveredtoken != token);
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括“MQTTClient.h”
#“定义地址”ssl://xxxxxxxxxx.azure-devices.net:8883" //tcp://localhost:1883"
#定义客户端ID“XXXXXXXXX”//“ExampleClientPub”
#定义主题“设备/xxxxxxxxx/messages/events/”/“MQTT示例”
#定义有效负载“你好,世界!”
#定义QOS 1
#定义超时10000L
易失性MQTTClient\u deliveryToken deliveredtoken;
已交付void(void*上下文,MQTTClient_deliveryToken dt)
{
printf(“令牌值为%d的邮件已确认传递,\n”,dt);
deliveredtoken=dt;
}
int-msgarrvd(void*context,char*topicName,int-topicLen,MQTTClient_message*message)
{
int i;
char*payloadptr;
printf(“消息到达\n”);
printf(“主题:%s\n”,主题名);
printf(“消息:”);
payloadptr=(char*)消息->有效载荷;
对于(i=0;ipayloadlen;i++)
{
putchar(*payloadptr++);
}
putchar('\n');
MQTTClient_freeMessage(&message);
MQTTClient_free(topicName);
返回1;
}
void connlost(void*上下文,char*原因)
{
printf(“\n连接丢失\n”);
printf(“原因:%s\n”,原因);
}
#定义测试单元SSL
int main(int argc,char*argv[])
{
MQTTClient;
MQTTClient\u connectOptions conn\u opts=MQTTClient\u conne