如何使用 Paho C 库连接到 HiveMQ 代理
How to connect to HiveMQ broker with Paho C library
我在HiveMq云中创建了一个hiveMQ集群,并创建了用户名和密码。
我从 Paho C 库创建 MQTTClient_connectOptions 并将我的用户名和密码作为参数:
#define ADDRESS "myURL:8883" // broker address for use in local machine
#define CLIENTID "myclientID"
#define TOPIC "testtopic"
#define TIMEOUT 10000L // ms
#define USERNAME "myUsername"
#define PASSWORD "myPassword"
int main(int argc, char* argv[])
{
/* MQTT Client initialization */
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc; //status code received from broker
rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
printf("Client create reason code: %d\n", rc);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = USERNAME;
conn_opts.password = PASSWORD;
MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
// checks whether the connection is successful or not
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", (MQTTClient_connect(client, &conn_opts)));
exit(-1);
}
MQTTClient_message msg = MQTTClient_message_initializer;
/*
sending data
pass sensor data to this function for publishing
*/
rc = publish_message(client, TOPIC, msg, &token, "4561237891", "23.6", "170.3", "524.08");
// Disconnect
int timeout = 100; //second
MQTTClient_disconnect(client, timeout);
MQTTClient_destroy(&client);
return rc;
}
MQTTClient_connect
无法连接到代理并且 return -1
:
Failed to connect, return code -1
我尝试使用 MQTT CLI 连接,它成功了,我发布并订阅了一个主题并传输了一条消息。所以我的身份验证是错误的。
如何使用 Paho C 库正确连接简单身份验证?
ADDRESS
应该是 URI,而不仅仅是 host:port 组合。
因此您需要在 host:port
之前包含 tcp://
或 ssl://
例如ssl://5xxxxxxxxxxxxxxxxxxxxxxxxx7bcac5.env-1.hivemq.cloud:8883
来自 Paho C 客户端 doc:
Parameters
- handle -- A pointer to an MQTTClient handle. The handle is populated with a valid client reference following a successful return
from this function.
- serverURI -- A null-terminated string specifying the server to which the client will connect. It takes the form protocol://host:port.
Currently, protocol must be tcp or ssl. For host, you can specify
either an IP address or a host name. For instance, to connect to a
server running on the local machines with the default MQTT port,
specify tcp://localhost:1883.
正如@hardillb 所写,您需要使用 TLS 连接到 HiveMQ Cloud。
您可以通过连接到 ssl://<your-address>:<your-port>
并通过将 MQTTClient_SSLOptions
添加到您的 MQTTClient_connectOptions
来为客户端启用它来做到这一点。
MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;
ssl_opts.enableServerCertAuth = 0;
conn_opts.ssl = &ssl_opts;
来源:https://www.hivemq.com/blog/mqtt-client-library-pahocclient/
您需要带有 s
的变体(例如 paho-mqtt3cs
)以支持 SSL/TLS。
命令行工具的源码中也使用了这个:
https://github.com/eclipse/paho.mqtt.c/blob/master/src/samples/paho_cs_pub.c
github 自述文件还展示了如何构建它(如果需要):
https://github.com/eclipse/paho.mqtt.c/blob/master/README.md
我在HiveMq云中创建了一个hiveMQ集群,并创建了用户名和密码。
我从 Paho C 库创建 MQTTClient_connectOptions 并将我的用户名和密码作为参数:
#define ADDRESS "myURL:8883" // broker address for use in local machine
#define CLIENTID "myclientID"
#define TOPIC "testtopic"
#define TIMEOUT 10000L // ms
#define USERNAME "myUsername"
#define PASSWORD "myPassword"
int main(int argc, char* argv[])
{
/* MQTT Client initialization */
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc; //status code received from broker
rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
printf("Client create reason code: %d\n", rc);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = USERNAME;
conn_opts.password = PASSWORD;
MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
// checks whether the connection is successful or not
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", (MQTTClient_connect(client, &conn_opts)));
exit(-1);
}
MQTTClient_message msg = MQTTClient_message_initializer;
/*
sending data
pass sensor data to this function for publishing
*/
rc = publish_message(client, TOPIC, msg, &token, "4561237891", "23.6", "170.3", "524.08");
// Disconnect
int timeout = 100; //second
MQTTClient_disconnect(client, timeout);
MQTTClient_destroy(&client);
return rc;
}
MQTTClient_connect
无法连接到代理并且 return -1
:
Failed to connect, return code -1
我尝试使用 MQTT CLI 连接,它成功了,我发布并订阅了一个主题并传输了一条消息。所以我的身份验证是错误的。
如何使用 Paho C 库正确连接简单身份验证?
ADDRESS
应该是 URI,而不仅仅是 host:port 组合。
因此您需要在 host:port
之前包含tcp://
或 ssl://
例如ssl://5xxxxxxxxxxxxxxxxxxxxxxxxx7bcac5.env-1.hivemq.cloud:8883
来自 Paho C 客户端 doc:
Parameters
- handle -- A pointer to an MQTTClient handle. The handle is populated with a valid client reference following a successful return from this function.
- serverURI -- A null-terminated string specifying the server to which the client will connect. It takes the form protocol://host:port. Currently, protocol must be tcp or ssl. For host, you can specify either an IP address or a host name. For instance, to connect to a server running on the local machines with the default MQTT port, specify tcp://localhost:1883.
正如@hardillb 所写,您需要使用 TLS 连接到 HiveMQ Cloud。
您可以通过连接到 ssl://<your-address>:<your-port>
并通过将 MQTTClient_SSLOptions
添加到您的 MQTTClient_connectOptions
来为客户端启用它来做到这一点。
MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;
ssl_opts.enableServerCertAuth = 0;
conn_opts.ssl = &ssl_opts;
来源:https://www.hivemq.com/blog/mqtt-client-library-pahocclient/
您需要带有 s
的变体(例如 paho-mqtt3cs
)以支持 SSL/TLS。
命令行工具的源码中也使用了这个: https://github.com/eclipse/paho.mqtt.c/blob/master/src/samples/paho_cs_pub.c
github 自述文件还展示了如何构建它(如果需要): https://github.com/eclipse/paho.mqtt.c/blob/master/README.md