T
T
thatmaniscool2019-10-11 14:36:59
Java
thatmaniscool, 2019-10-11 14:36:59

Error connecting to MQTT server. What is the reason?

I'm trying to connect to an MQTT server (Cloud MQTT) using the PahoMqtt library.
Gives the following errors:
Reason code - 5, according to the documentation REASON_CODE_NOT_AUTHORIZED.
Message code: No permission to connect.
I bring the code:

public static void main(String[] args) {
    String topic	= "Lesson_MQTT_server";
    String broker	= "tcp://m15.cloudmqtt.com:18594";
    char [] pass	= "qmaoaTfDebag".toCharArray(); // Fake pass
    String userId	= "oelmanhl";
    String content	= "Hello world!";
    int qos			= 0;
    
    
    try {
      MqttClient client = new MqttClient (broker, userId, new MemoryPersistence());
      MqttConnectOptions options = new MqttConnectOptions ();
      //options.setAutomaticReconnect(true);
      options.setCleanSession(true);
      options.setPassword(pass);			
      System.out.println ("Connecting..."); 
      
      client.connect(options); // Stop here, can't connect to MQTT server.
      System.out.println ("Conntected!");
      
      MqttMessage message = new MqttMessage ();
      message.clearPayload();
      message.setPayload(content.getBytes());
      message.setQos(qos);
      System.out.println ("Publishing message...");
      
      client.publish(topic, message);
      System.out.println ("Message published!");
      
      client.disconnect();		
      
    } catch (MqttException ex) {
      System.out.println ("Reason code: " + ex.getReasonCode());
      System.out.println ("Cause code: " + ex.getCause());
      System.out.println ("Message code: " + ex.getMessage());
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question