0

Error code explanation
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Error code explanation

by jeffrey on Thu Dec 27, 2018 11:31 am

My sketch was working just fine (well, at least it was sending data to AdafruitIO via MQTT). Then it wouldn't connect to MQTT. I turned printed out the error being returned and it was "Not authorized to connect." I dug around to see what might be causing this and found the following possible error messages in Adafruit_MQTT.cpp. Can anyone give me an idea what these error codes mean, and specifically "Not authorized to connect."? I am at a standstill until I can get this resolved. I did change my key since I was dumb enough to post it in an different thread and updated my sketch, but that didn't make any difference. I checked it again this morning and on the web site it just said "Loading". I regenerated another code and that worked. Maybe there was a hiccup on the site?

I am really curious to see if any of the Adafruit people actually respond to this. This is the third post and I haven't heard squat from a moderator. Thus far they seem pretty good at recommending using libraries without really answering any questions regarding the library (Adafruit IO being the one in question). And yes, it is hard to be positive when you can't get any assistance. However, this point is constructive.

Code: Select all | TOGGLE FULL SIZE
      case 1: return F("The Server does not support the level of the MQTT protocol requested");
      case 2: return F("The Client identifier is correct UTF-8 but not allowed by the Server");
      case 3: return F("The MQTT service is unavailable");
      case 4: return F("The data in the user name or password is malformed");
      case 5: return F("Not authorized to connect");
      case 6: return F("Exceeded reconnect rate limit. Please try again later.");
      case 7: return F("You have been banned from connecting. Please contact the MQTT server administrator for more details.");
      case -1: return F("Connection failed");
      case -2: return F("Failed to subscribe");
      default: return F("Unknown error");

jeffrey
 
Posts: 31
Joined: Sat May 12, 2012 8:04 pm

Re: Error code explanation

by adafruit_support_carter on Thu Dec 27, 2018 4:09 pm

It sounds like a credentials error when trying to connect. I'm a little unclear and what is currently happening. It sounds like you have one sketch that works OK and one that gives you the connection error?

adafruit_support_carter
 
Posts: 11467
Joined: Tue Nov 29, 2016 2:45 pm

Re: Error code explanation

by jeffrey on Fri Dec 28, 2018 10:31 am

None worked. I went to the site to check my code and all it said was "Loading", so I generated another code, loaded that into my sketches, and everything worked.

jeffrey
 
Posts: 31
Joined: Sat May 12, 2012 8:04 pm

Re: Error code explanation

by adafruit_support_carter on Fri Dec 28, 2018 12:31 pm

Not sure then. Somehow the credentials got out of sync. By generating new ones, everything got synced back up. It's all working OK now? Only mystery is what happened before?

adafruit_support_carter
 
Posts: 11467
Joined: Tue Nov 29, 2016 2:45 pm

Re: Error code explanation

by jeffrey on Fri Dec 28, 2018 1:23 pm

Yep, it's working. Is there an explanation for the error codes?

jeffrey
 
Posts: 31
Joined: Sat May 12, 2012 8:04 pm

Re: Error code explanation

by adafruit_support_carter on Fri Dec 28, 2018 2:04 pm

I think you found it in that case statement. An error code of 5 means "Not authorized to connect". Are you looking for something that further explains this?

adafruit_support_carter
 
Posts: 11467
Joined: Tue Nov 29, 2016 2:45 pm

Re: Error code explanation

by jeffrey on Fri Dec 28, 2018 2:14 pm

I am pretty sure that info would be useful to anyone using your MQTT if they have an error. That kind of info should be posted somewhere for everybody to see.

jeffrey
 
Posts: 31
Joined: Sat May 12, 2012 8:04 pm

Re: Error code explanation

by adafruit_support_carter on Fri Dec 28, 2018 2:24 pm

Somewhere other than source code? Like here?
https://learn.adafruit.com/welcome-to-a ... entation-2

adafruit_support_carter
 
Posts: 11467
Joined: Tue Nov 29, 2016 2:45 pm

Re: Error code explanation

by jeffrey on Fri Dec 28, 2018 2:47 pm

Now you are talking! I think I visited this page in the beginning and forgot about it. You ought to put a link to it on the Adafruit IO page.

I just made a few small adjustments to my sketch and I am getting the not authorized error again. The following is what shows up on the serial monitor.

Retrying MQTT connection in 2.5 seconds...
MQTT disconnect packet:
[0xE0], [0x00],
Writing packet
Failed to send packet.
Unable to send disconnect packet
Connecting to TCP
MQTT connect packet:
[0x10], . [0x2E], [0x00], [0x04], M [0x4D], Q [0x51], T [0x54], T [0x54],
[0x04], B [0x42], [0x01], , [0x2C], [0x00], [0x00], [0x00], [0x20],
(my key...),

Writing packet
Reading data..
Read: [0x20],
Packet Type: [0x20],
Reading data..
Read: [0x02],
Packet Length: 2
Reading data..
Read: [0x00], [0x05],
Not authorized to connect

jeffrey
 
Posts: 31
Joined: Sat May 12, 2012 8:04 pm

Re: Error code explanation

by adafruit_support_carter on Fri Dec 28, 2018 2:52 pm

Maybe you are exceeding the rate limit? For the free account level it's:
10 feeds, 5 dashboards, 30 days of data storage, and a 30 data points per minute data rate

If you try to exceed that, the account may be temporarily blocked and you would need to wait and try again later.

adafruit_support_carter
 
Posts: 11467
Joined: Tue Nov 29, 2016 2:45 pm

Re: Error code explanation

by jeffrey on Fri Dec 28, 2018 2:58 pm

I have two feeds, one dashboard, and I publish data once every thirty seconds. I assume that the "30 days of data storage" automatically deletes the first days as you pass the 30 day mark (FIFO)?

jeffrey
 
Posts: 31
Joined: Sat May 12, 2012 8:04 pm

Re: Error code explanation

by adafruit_support_carter on Fri Dec 28, 2018 3:03 pm

Yes, oldest data deleted first.

adafruit_support_carter
 
Posts: 11467
Joined: Tue Nov 29, 2016 2:45 pm

Re: Error code explanation

by jeffrey on Fri Dec 28, 2018 3:13 pm

I tried cycling power to my Uno and that didn't help. I regenerated my key and that didn't help, either. This is getting old. It ran overnight with nary a hiccup. It shouldn't be anything in the sketch that causes this since I get the same error with your examples.

jeffrey
 
Posts: 31
Joined: Sat May 12, 2012 8:04 pm

Re: Error code explanation

by adafruit_support_carter on Sat Dec 29, 2018 6:28 pm

What is the simplest of the example sketches (ours) that shows the issue?

adafruit_support_carter
 
Posts: 11467
Joined: Tue Nov 29, 2016 2:45 pm

Re: Error code explanation

by jeffrey on Sat Dec 29, 2018 8:21 pm

This was after I deleted my feeds and dashboard again. Still no luck. I also regenerated my key again just in case.

Code: Select all | TOGGLE FULL SIZE
/***************************************************
  Adafruit MQTT Library FONA Example

  Designed specifically to work with the Adafruit FONA
  ----> http://www.adafruit.com/products/1946
  ----> http://www.adafruit.com/products/1963
  ----> http://www.adafruit.com/products/2468
  ----> http://www.adafruit.com/products/2542

  These cellular modules use TTL Serial to communicate, 2 pins are
  required to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
 ****************************************************/
#include <Adafruit_SleepyDog.h>
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_FONA.h"

/*************************** FONA Pins ***********************************/

// Default pins for Feather 32u4 FONA
#define FONA_RX  2
#define FONA_TX  3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

/************************* WiFi Access Point *********************************/

  // Optionally configure a GPRS APN, username, and password.
  // You might need to do this to access your network's GPRS/data
  // network.  Contact your provider for the exact APN, username,
  // and password values.  Username and password are optional and
  // can be removed, but APN is required.
#define FONA_APN       ""
#define FONA_USERNAME  ""
#define FONA_PASSWORD  ""

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "xxxxxx"
#define AIO_KEY         "xxxxxxxx"

/************ Global State (you don't need to change this!) ******************/

// Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details.
Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

// You don't need to change anything below this line!
#define halt(s) { Serial.println(F( s )); while(1);  }

// FONAconnect is a helper function that sets up the FONA and connects to
// the GPRS network. See the fonahelper.cpp tab above for the source!
boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password);

/****************************** Feeds ***************************************/

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");

// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");

/*************************** Sketch Code ************************************/

// How many transmission failures in a row we're willing to be ok with before reset
uint8_t txfailures = 0;
#define MAXTXFAILURES 3

void setup() {
  while (!Serial);

  // Watchdog is optional!
  //Watchdog.enable(8000);
 
  Serial.begin(115200);

  Serial.println(F("Adafruit FONA MQTT demo"));

  mqtt.subscribe(&onoffbutton);

  Watchdog.reset();
  delay(5000);  // wait a few seconds to stabilize connection
  Watchdog.reset();
 
  // Initialise the FONA module
  while (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) {
    Serial.println("Retrying FONA");
  }

  Serial.println(F("Connected to Cellular!"));

  Watchdog.reset();
  delay(5000);  // wait a few seconds to stabilize connection
  Watchdog.reset();
}

uint32_t x=0;

void loop() {
  // Make sure to reset watchdog every loop iteration!
  Watchdog.reset();

  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

  Watchdog.reset();
  // Now we can publish stuff!
  Serial.print(F("\nSending photocell val "));
  Serial.print(x);
  Serial.print("...");
  if (! photocell.publish(x++)) {
    Serial.println(F("Failed"));
    txfailures++;
  } else {
    Serial.println(F("OK!"));
    txfailures = 0;
  }

  Watchdog.reset(); 
  // this is our 'wait for incoming subscription packets' busy subloop
  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {
    if (subscription == &onoffbutton) {
      Serial.print(F("Got: "));
      Serial.println((char *)onoffbutton.lastread);
    }
  }

  // ping the server to keep the mqtt connection alive, only needed if we're not publishing
  //if(! mqtt.ping()) {
  //  Serial.println(F("MQTT Ping failed."));
  //}

}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  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
  }
  Serial.println("MQTT Connected!");
}

jeffrey
 
Posts: 31
Joined: Sat May 12, 2012 8:04 pm

Please be positive and constructive with your questions and comments.