I have try to connect MQTT broker but it can’t connect with first attempt when i try to reconnect that broker it will connect success fully.Why it is not connect in first attempt?
There is a million answer.You does not provide any clue for what it could be happening.Post your code, ip's , broker address , wiring
Without this is just a bet
Thanks for reply
example as
const char *uuid = "deep";
const char *clientKey = "ck-47046d75cf64402f";
IPAddress dnServer(192, 168, 0, 3);
// the subnet:
IPAddress subnet(255, 255, 255, 0);
// set ip address
IPAddress ip(192, 168, 60, 169);
void reconnect() {
char err_string[YALGAAR_ERROR_MESSAGE_LENGTH] = {'\0'};
// Loop until we're reconnected
while (!yalgaarclient.is_connected()) {
Serial.println("Attempting to reconnect Yalgaar connection...");
// Attempt to connect
ret = yalgaarclient.yalgaar_connect(clientKey, uuid, connection_Callback);
if (SUCCESS != ret)
{
yalgaarclient.enum_to_message(ret, err_string);
Serial.println(err_string);
}
if (ret == SUCCESS) {
Serial.println("connected");
// ... and resubscribe
ret = yalgaarclient.yalgaar_subscribe("deepvyas", subscribe_message_callback, presence_message_callback, error_message_callback);
if (SUCCESS != ret)
{
yalgaarclient.enum_to_message(ret, err_string);
Serial.println(err_string);
}
} else {
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
yalgaarclient.setServer("api.yalgaar.io", 1883);
Ethernet.begin(mac, ip);
Serial.println("Attempting yalgaar connection...");
//Connect to Yalgaar
ret = yalgaarclient.yalgaar_connect(clientKey, uuid, connection_Callback);
if (SUCCESS != ret)
{
yalgaarclient.enum_to_message(ret, err_string);
Serial.println(err_string);
}
if (ret == SUCCESS) {
Serial.println("connected to yalgaar");
ret = yalgaarclient.yalgaar_subscribe("deepvyas", subscribe_message_callback, presence_message_callback, error_message_callback);
if (SUCCESS != ret)
{
yalgaarclient.enum_to_message(ret, err_string);
Serial.println(err_string);
}
}
else
{
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
void loop() {
// put your main code here, to run repeatedly:
if (!yalgaarclient.is_connected()) {
reconnect();
}
// Once connected, publish an announcement...
if (yalgaarclient.is_connected())
{
if (pub_count)
{
delay(2000);
ret = yalgaarclient.yalgaar_publish("deepvyas", "TEST-PUBLISH");
if (SUCCESS != ret)
{
yalgaarclient.enum_to_message(ret, err_string);
Serial.println(err_string);
}
pub_count--;
}
}
yalgaarclient.yalgaar_loop();
}
First time it try to connect with broker but it fails to connect and then it goes to reconnect to broker that time it will
connect successfully
Please help to solve this problem