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?