0

ESP8266 Connection Failure after successfully connecting
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

ESP8266 Connection Failure after successfully connecting

by kamaleda on Sun Feb 07, 2021 4:52 am

I'm using an Arduino mega connected to an ESP8266. I followed example code mqtt ESP8266. When I first connect to the adafruit server I get a sucessful connection. However, once I try to reset the arduino and start again with the exact same code, I get a connection failed error. The weird thing is if I check the Monitor on adafruit it says that I successfully connected. I'm even able to send data to my feed! I found that if i change my key, I get a successful connection message, but again when I restart the arduino it stops working.

Is it because I'm on the free version of IO and adafruit only allows a trigger every 15 min? I'm guessing the server is treating the return code (which is 0) as a trigger, and only allowing it to occur once every 15 min or when I reset the key. So even though the ESP is connected, it doesn't get the return code and keeps trying to connect, eventually triggering the error.

Here's the function in the code that is in question. it's straight from the example code.
Code: Select all | TOGGLE FULL SIZE
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }
  Serial.println("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}

kamaleda
 
Posts: 2
Joined: Sun Feb 07, 2021 12:27 am

Re: ESP8266 Connection Failure after successfully connecting

by brubell on Mon Feb 08, 2021 11:46 am

However, once I try to reset the arduino and start again with the exact same code, I get a connection failed error. The weird thing is if I check the Monitor on adafruit it says that I successfully connected.

Where do you get a connection failed error? Could you copy and paste this error into the thread?

I'm guessing the server is treating the return code (which is 0) as a trigger, and only allowing it to occur once every 15 min or when I reset the key.

This wouldnt matter - you disconnect from adafruit io when you power-cycle the board.

brubell
 
Posts: 1381
Joined: Fri Jul 17, 2015 10:33 pm

Re: ESP8266 Connection Failure after successfully connecting

by kamaleda on Mon Feb 08, 2021 3:50 pm

Hi thanks for the reply. Here's the error message I get:
connection_failed.png
connection_failed.png (47.61 KiB) Viewed 126 times


It occurs while trying to connect to the adafruit server.

If I try again 15 minutes later it works fine.
connection_success.png
connection_success.png (34.88 KiB) Viewed 126 times


But resetting the program will give me the same error as the first image, and I have to wait 15 min again before it succeeds.

kamaleda
 
Posts: 2
Joined: Sun Feb 07, 2021 12:27 am

Re: ESP8266 Connection Failure after successfully connecting

by GubTerra on Tue Feb 09, 2021 12:16 am

im running a mega with esp8266 in slave/master mode... using the adafruit arduino io library and some examples in this library u can suceed... code some like

Code: Select all | TOGGLE FULL SIZE
void configuraMQTT() {
  Serial.print("Connecting to Adafruit IO");
  io.connect();

   while (io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println();
  Serial.println(io.statusText());
}




GubTerra
 
Posts: 9
Joined: Wed Nov 04, 2020 10:48 am

Re: ESP8266 Connection Failure after successfully connecting

by brubell on Thu Feb 11, 2021 1:17 pm

Have you tried connecting to a different wireless network, or using your smartphone as a hotspot to host another network?

brubell
 
Posts: 1381
Joined: Fri Jul 17, 2015 10:33 pm

Please be positive and constructive with your questions and comments.