1

I have some trouble in subscribing at the topic v1/devices/me/telemetry. I have no problems in subscribing on v1/devices/me/attributes using the paho Java-MQTT-Client. At the attributes-topic I can get new attributes when I post them in the UI. So my Java-Program seems to runs fine (see bottom).

I get the following at the console:

Subscriber running  
checking  
Mqtt Connecting to broker: tcp://192.168.1.25:1883  
Mqtt Connected  
MqttException (128)  
MqttException (128)  
at org.eclipse.paho.client.mqttv3.MqttClient.subscribe(MqttClient.java:438)  
at org.eclipse.paho.client.mqttv3.MqttClient.subscribe(MqttClient.java:406)  
at Test.MqttSubscriber.subscribe(MqttSubscriber.java:57)  
at Test.MqttSubscriber.main(MqttSubscriber.java:30)

I guess that Error Code 128 means that the subscription was pulled back.

What am I doing wrong? Publishing content to thingsboard at that topic is no problem. Do I have to activate the broker for publishing/subscribing somehow? Does the internal broker of TB need a special command (JSON maybe) to grant a subscribtion? Or do I have to realise it with the IoT gateway (I understand it the way that TB can push data to an external broker - but here a simple subscription is needed)? Which alternative do I have to get device-telemetry from Thingsboard using MQTT?

I hope someone can help :) Thank you!

The Code is (MqttSubscriber.java):

 package Test;

    import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
    import org.eclipse.paho.client.mqttv3.MqttCallback;
    import org.eclipse.paho.client.mqttv3.MqttClient;
    import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
    import org.eclipse.paho.client.mqttv3.MqttException;
    import org.eclipse.paho.client.mqttv3.MqttMessage;
    import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
    
    public class MqttSubscriber implements MqttCallback  {


    private static final String brokerUrl ="tcp://192.168.1.25:1883"; //Broker


    private static final String clientId = "test"; //Client-ID


    private static final String topic = "v1/devices/me/telemetry"; //Topic

    private static final String user = "AT2"; // Accesstoken/User from Device in TB!

    private static final String pw = "test";

    private static final char[] password = pw.toCharArray();

    public static void main(String[] args) {

    System.out.println("Subscriber running");

    new MqttSubscriber().subscribe(topic);
    }

    public void subscribe(String topic) { 

    MemoryPersistence persistence = new MemoryPersistence();

    try
    {

        MqttClient sampleClient = new MqttClient(brokerUrl, clientId, persistence);
        MqttConnectOptions connOpts = new MqttConnectOptions();
        
        
        
        connOpts.setCleanSession(true);
        connOpts.setUserName(user);
        connOpts.setPassword(password);

        System.out.println("checking");
        System.out.println("Mqtt Connecting to broker: " + brokerUrl);

        sampleClient.connect(connOpts);
        if (sampleClient.isConnected()==true) System.out.println("Mqtt Connected");
        else System.out.println("could not connect");

        sampleClient.setCallback(this);
        sampleClient.subscribe(topic);
        
        
        

        System.out.println("Subscribed");
        System.out.println("Listening");

        } catch (MqttException me) {
        System.out.println(me);
        me.printStackTrace();
        }
        }

        //Called when the client lost the connection to the broker
        public void connectionLost(Throwable arg0) {
    
        }

        //Called when a outgoing publish is complete
        public void deliveryComplete(IMqttDeliveryToken arg0) {

        }

        public void messageArrived(String topic, MqttMessage message) throws Exception {

    
        System.out.println("| Topic:" + topic);
        System.out.println("| Message: " +message.toString());
        System.out.println("-------------------------------------------------");

        }

        }
m4ld3
  • 11
  • 1

1 Answers1

0

As far as I can see the problem is an unsatisfied QoS level. A subscription without a QoS parameter defaults to QoS == 1. If this QoS is not supported for the requested topic the client throws this exception. Excerpt from Paho client whereby your call to subscribe(topic) cascades to this subscribe method:

    public void subscribe(String[] topicFilters, int[] qos, IMqttMessageListener[] messageListeners) throws MqttException { 
        IMqttToken tok = aClient.subscribe(topicFilters, qos, null, null, messageListeners);
        tok.waitForCompletion(getTimeToWait());
        int[] grantedQos = tok.getGrantedQos();
        for (int i = 0; i < grantedQos.length; ++i) {
            qos[i] = grantedQos[i];
        }
        if (grantedQos.length == 1 && qos[0] == 0x80) {
            throw new MqttException(MqttException.REASON_CODE_SUBSCRIBE_FAILED);
        }
    }

So you have to check the QoS level of the requested topic and subscribe with that QoS level. Because QoS 1 is rejected I presume the topic is published with QoS 0.

Stefan D.
  • 299
  • 3
  • 8