knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.81k stars 1.47k forks source link

using pubsubclient to connect to adafruit io #291

Open pearson opened 7 years ago

pearson commented 7 years ago

This is essentially the same issue as #256 which was, IMHO, closed early because the author had posted a different test that generated a different response code.

When connecting to Adafruit's IO service at io.adafruit.com with PubSubClient, I always get a response code 5. Using Adafruit's own MQTT library works, but of course it's a minimal library...

The key line may be if (client.connect(adafruit_username, aio_key, "")) and whether that way of connecting actually works with your library. It is what Adafruit uses in their example, as they use that key and no password for connecting to their service through MQTT. However, in a forum thread, Adafruit states that "we've updated to MQTT 3.1.1 and havent done heavy pubsub testing since".

Whether this is something you'd want to worry about is up to you. :-)

I've tried using their example from this link as well as your mqtt_esp8266.ino example, simply replacing the client.connect code with the line above. I've tried various variations on it as well, but with no success. rc=5 is what always returns.

Thanks.

Lennyz1988 commented 7 years ago

Yes I am experiencing the same problem.

Error 5 = 0x05 Connection Refused, not authorized The Client is not authorized to connect

It would be great if we could use this library to connect to Adafruit IO because their own library blocks my entire loop for x seconds.

For reference:

Error 6 = 0x06 Connection Refused, throttled Client has exceeded connection rate limit. Please try again later.

Source: https://io.adafruit.com/blog/mqtt/2016/06/28/extending-the-mqtt-protocol/

knolleary commented 7 years ago

Hi,

I don't currently have an arduino to hand to properly investigate, but I've dug through the code to see if there's anything off-spec with the username/password handling. I'm pretty certain this library is fully spec compliant, but there is one edge case that is worth checking.

Could you try:

if (client.connect(adafruit_username, aio_key, 0))

ie - rather than provide an empty string for the password, pass in 0.

If that works, then there is a bug with Adafruit's implementation of 3.1.1 and we can figure out the best next step.

pearson commented 7 years ago

Unfortunately, that doesn't work.

I've tried a few combinations, such as:

None have worked. All return rc=5.

I am running this on an Adafruit Huzzah Feather (an ESP8266 based board), but I haven't had any issues with other Arduino libraries in the past.

knolleary commented 7 years ago

@pearson hmm ok, thanks for trying those combinations - there must be something more subtle going on. NULL/0 are indeed the same case, and do result in a different CONNECT packet to the "" case. I've added a test case to verify the CONNECT packet is correct in the "" which wasn't there previously... but it doesn't solve the problem here.

I have limited time this week; I may be able to have a proper dig on Friday.

lowvoltage commented 7 years ago

@pearson Given that the method declaration is, for example: boolean connect(const char* id, const char* user, const char* pass);, then the correct statement should be: mqttclient.connect("id", ADAFRUIT_USERNAME, AIO_KEY); https://github.com/knolleary/pubsubclient/blob/master/src/PubSubClient.h#L126

This should get you past the rc=5 issue and possibly into the rc=6 issue - https://learn.adafruit.com/adafruit-io/mqtt-api#rate-limit

marcoracer commented 7 years ago

Actually, even the Adafruit_MQTT_Library ethernet example is not working, only the wifi versions is. Anybody have any idea why?

architmuchhal12 commented 7 years ago

Has anyone been able to connect to Adafruit IO using PubSub Library?

axxxo1 commented 6 years ago

Yes, I connected to Adafruit IO today with these lines of code:

MQTTUSER = ADAFRUIT_USERNAME; outTopic =\"ADAFRUIT_USERNAME/F/outTopic" MQTTkey = AIO_KEY; String clientId = "ESP8266Client-"; clientId += String(random(0xffff), HEX); // Attempt to connect if (MQTTclient.connect(clientId.c_str(), MQTTuser, MQTTkey)) { // Once connected, publish an announcement... MQTTclient.publish(outTopic, "hello world");

borch84 commented 6 years ago

Hello, I am having the same issue, but I am testing my connection agains a mosquitto broker with authentication. If I subscribe manually using mosquitto_sub with username and password it works, but If I use the connect function from the PubSubClient library I got rc=5

This is the version of the function I am using:

client.connect((char) clientName.c_str()),(char) mqtt_user.c_str(),(char*) mqtt_pass.c_str())

I am using a Adafruit Feather ESP8266. When I remove the authentication from the mosquitto.conf file it works.

UPDATE 20170105: I changed to Adafruit's MQTT library and I was able to implement username / password feature.

Danorexic commented 6 years ago

I followed axxo1's advice and it worked. I did however use "ADAFRUIT_USERNAME/feeds/topicname" for mine and didn't test using /f/ inbetween my username and the topic name. I noticed their mqtt_esp8266_callback example, they used /feeds/ when setting up their topic. Seen here: https://github.com/adafruit/Adafruit_MQTT_Library/blob/master/examples/mqtt_esp8266_callback/mqtt_esp8266_callback.ino

jigneshk5 commented 6 years ago

+axxx01 bro, I am using your method...Acctualy it works partly but I got an error message "mqtt subscription error on /jigneshk5/feeds/blink: you are not authorized" in the live error section of adafruit.io, I am using the topic as topic = "/jigneshk5/feeds/blink"; Using topic like "jigneshk5/feeds/blink" don't do anything at the adafruit

dgorbunov commented 4 years ago

Don't mean to bump this thread but just want anyone that comes across this in the future that username case matters with IO, if incorrect you will not be able to connect.

TheRobertTalley commented 3 years ago

This works for me:

https://github.com/TheRobertTalley/lilygo-sim7000-ESP32/blob/main/Connect%20tcall%20sim7000%20to%20Adafruit%20IO

dmadison commented 2 years ago

The MQTT docs for the current API are here: https://io.adafruit.com/api/docs/mqtt.html#adafruit-io-mqtt-api

My problem was that I forgot to remove https from the MQTT server string, and was using the non-SSL port. Adafruit also recommends to use a blank client string. Here are my settings in case it helps anyone:

const char* mqtt_server = "io.adafruit.com";
const char* mqtt_username = "your_username_here";
const char* mqtt_key = "your_key_here";

client.setServer(mqtt_server , 1883);

if (client.connect("", mqtt_username, mqtt_key)) {
    // success
}