knolleary / pubsubclient

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

Connection failed rc= -1 when connecting to RabbitMQ with token auth0 #1057

Closed vedrancek closed 1 month ago

vedrancek commented 1 month ago

Hello,

I am facing a problem connecting to my RabbitMQ with a username and password as a token. RabbitMQ is configured to authenticate with Oauth 2.0. I tested the connection with Mosquitto pub and sub functions. For example: mosquitto_sub -h RabbitMQ_IP -p 1883 -t test/topic -u "user" -P "eyJhbGciOiJSUzI1NiIsInR5cC token)" to subscribe and:
mosquitto_pub -h RabbitMQ_IP -p 1883 -t test/topic -m "Hello, MQTT" -u "user" -P "eyJhbGciOiJSUzI1NiIsInR5cC token" to publish a message. And this works.

When I want to do the same with the ESP32 with a simple Arduino sketch I get an rc=-1 error.

Arduino sketch:

#include <WiFi.h>
#include <PubSubClient.h>

// WiFi credentials
const char* ssid = "ssid";
const char* password = "password";

// RabbitMQ credentials
const char* mqtt_server = "RabbitMQ_IP";
const int mqtt_port = 1883;
const char* mqtt_user = "user";
const char* mqtt_password = "eyJhbGciOiJSUzI1NiIsInR5cC....a token";

// MQTT topic
const char* topic = "test/hello";

// WiFi and MQTT clients
WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    Serial.print('\n'); 
    Serial.print(mqtt_user);
    Serial.print('\n'); 
    Serial.print(mqtt_password);
    Serial.print('\n'); 
    if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
      Serial.println("connected");
      client.subscribe(topic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  static unsigned long lastMsg = 0;
  unsigned long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    Serial.print("Publish message: ");
    Serial.println("Hello World");
    client.publish(topic, "Hello World");
  }
}

What am I doing wrong? MQTT 3.1.1 protocol should support OAuth [RFC6749] tokens. Link to the specification

Best regards.

vedrancek commented 1 month ago

Hello,

I works... the problem was in the too small MQTT packet size... My token has over 900 characters. So I had to only adjust the setBufferSize(). My ignorance in this case...

Best regards.

vedrancek commented 1 month ago

I am closing this....