Reputation: 898
I created a hiveMQ cluster in the HiveMq cloud and created a username and password.
From Paho C library I created MQTTClient_connectOptions and put my username and password as parameters:
#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;
}
the MQTTClient_connect
cannot connect to the broker and return -1
:
Failed to connect, return code -1
I tried connecting with MQTT CLI, it was successful, I published and subscribed to a topic and transferred a msg. So my authentication is wrong.
How to correctly connect with simple authentication with the Paho C library?
Upvotes: 1
Views: 1116
Reputation: 193
As @hardillb already wrote, you need to connect to HiveMQ Cloud with TLS.
You do that by connecting to ssl://<your-address>:<your-port>
and enabling it for the client by addding MQTTClient_SSLOptions
to your MQTTClient_connectOptions
.
MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;
ssl_opts.enableServerCertAuth = 0;
conn_opts.ssl = &ssl_opts;
Source: https://www.hivemq.com/blog/mqtt-client-library-pahocclient/
You need the variants with the s
(e.g. paho-mqtt3cs
) to support SSL/TLS.
This is also used in the source of the command line tools: https://github.com/eclipse/paho.mqtt.c/blob/master/src/samples/paho_cs_pub.c
The github README also shows how to build it (if required): https://github.com/eclipse/paho.mqtt.c/blob/master/README.md
Upvotes: 3
Reputation: 59648
The ADDRESS
should be URI not just a host:port combination.
So you need to include tcp://
or ssl://
before the host:port
e.g. ssl://5xxxxxxxxxxxxxxxxxxxxxxxxx7bcac5.env-1.hivemq.cloud:8883
From the Paho C client 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.
Upvotes: 2