gpt4 book ai didi

c - 向 IOT 中心发布消息 - Eclypse Paho MQTT

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:08 24 4
gpt4 key购买 nike

下面是我使用 Eclypse Paho MQTT 库从设备连接数据并将数据发送到云的代码。在我的情况下,我能够以 rc 结果 0 连接 IOT 集线器,但它不会向我的 IOT 集线器设备发送消息。谁能指导我哪里犯了错误或者我做错了什么?我正在使用带有 .Net Framework 4.0 和 eclipse-paho-mqtt-c-windows-1.0.3 库的 Visual studio 2015

#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;
}

最佳答案

下面的代码在我的例子中工作正常

#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;
}

关于c - 向 IOT 中心发布消息 - Eclypse Paho MQTT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51045360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com