如何订阅MQTT主题并在Eclipse上打印接收到的消息(Java)

问题描述:

我有一个微控制器,通过恒温器将其数据通过Raspberry Pi发送到使用MQTT协议的计算机。库拉已安装并正在开发覆盆子。如何订阅MQTT主题并在Eclipse上打印接收到的消息(Java)

我在接收Putty上的数据时没有问题,但现在我需要在Eclipse上接收它,所以我可以开发一个程序。

我管理经由使用PAHO用下面的代码蚀在话题发布,(这是本主题的其他的Subscribe and Read MQTT Message Using PAHO改编):

package publish; 

import org.eclipse.paho.client.mqttv3.MqttClient; 
import org.eclipse.paho.client.mqttv3.MqttException; 
import org.eclipse.paho.client.mqttv3.MqttMessage; 

public class PublishSemInterface { 
MqttClient client; 

public PublishSemInterface() {} 

public static void main(String[] args) { 
    new PublishSemInterface().doDemo(); 
} 

public void doDemo() { 
    try { 
     client = new MqttClient("tcp://192.168.0.39:1883", "user"); 
     client.connect(); 
     MqttMessage message = new MqttMessage(); 
     message.setPayload("Published message".getBytes()); 
     client.publish("sensor/temp/out", message); 
     client.disconnect(); 
    } catch (MqttException e) { 
     e.printStackTrace(); 
    } 
} 
} 

但订阅是正在痛苦。我试图用我上面提到的话题的答案,实现MqttCallback接口:

public class PublishSemInterface implements MqttCallback 

连接到客户端和需要的接口方法(我只需要messageArrived)后,添加setCallback:

client.setCallback(this); 

@Override 
public void connectionLost(Throwable cause) {} 

@Override 
public void messageArrived(String topic, MqttMessage message) 
    throws Exception { 
System.out.println(message); 
} 

@Override 
public void deliveryComplete(IMqttDeliveryToken token) {} 

但它没有工作。 How to read data from MQTT in Eclipse Paho?

public static void main(String[] args) { 

    MqttClient client; 
    MqttConnectOptions conn; 

    try { 
     client = new MqttClient("tcp://192.168.0.39:1883", "user"); 
     client.connect(); 
     client.setCallback(new MqttCallback() { 
      public void connectionLost(Throwable cause) {} 

      public void messageArrived(String topic, 
        MqttMessage message) 
          throws Exception { 
       System.out.println(message.toString()); 
      } 

      public void deliveryComplete(IMqttDeliveryToken token) {} 
     }); 

     client.subscribe("sensor/temp/in"); 

    } catch (MqttException e) { 
     e.printStackTrace(); 
    } 
} 

除了它没有任何工作:我用从以下话题的答案也试过。在这两种情况下,当我运行代码时,控制台处于活动状态,但是当微控制器发送数据(出现在Putty上)而不是打印时,程序将终止。它看起来好像messageArrived方法没有被调用。

任何人都可以帮我在Eclipse的控制台上订阅和打印吗?

我已经设法让发送的数据出现在Eclipse控制台上。它看起来ClientId是错误的,但我也根据我在我的问题上链接的主题的答案添加了一些修改。这里是代码:

private Map<String, Object> properties; 

public void updated(Map<String, Object> properties) { 
    this.properties = properties; 
    String broker = ""; 
    String clientId = ""; 
    String topic = ""; 

    if(properties != null && !properties.isEmpty()) { 

    broker = (String) properties.get("broker.name"); 
    clientId = (String) properties.get("clientId.name"); 
    topic = (String) properties.get("topic.name"); 

    doDemo(broker, clientId, topic); 
    } 
} 

public void doDemo(String broker, String clientId, String topic) { 
    MemoryPersistence persistence = new MemoryPersistence(); 

    try { 
    MqttClient sampleClient = new MqttClient(broker, clientId, persistence); 
    MqttConnectOptions connOpts = new MqttConnectOptions(); 
    connOpts.setCleanSession(true); 

    sampleClient.setCallback(new MqttCallback() { 
     public void connectionLost(Throwable cause) {} 

     public void messageArrived(String topic, MqttMessage message) throws Exception { 
     System.out.println("Message: " + message.toString()); 
     } 

     public void deliveryComplete(IMqttDeliveryToken token) {} 
    }); 

    sampleClient.connect(connOpts); 
    sampleClient.subscribe(topic); 

    } catch(MqttException e) { 
    e.printStackTrace(); 
    } 
} 

如您所见:client.publish("sensor/temp/out", message);,您的主题是sensor/temp/out。因此,您的订阅者应该订阅同一主题,而不是以下行:client.subscribe("sensor/temp/in");,请尝试订阅主题:sensor/temp/out

另外,我会建议你使用额外的mqtt选项创建连接。这样的事情:

MqttClient client = new MqttClient(serverUrl, UUID.randomUUID().toString().replace("-", "")); //clientID needs to be unique and has meaning only for mqtt broker 
MqttConnectOptions options = new MqttConnectOptions(); 
options.setUserName("username"); //part of the password_file inside mqtt broker 
options.setPassword("password".toCharArray()); //also part of password_file. Username and password might not be needed. 
options.setConnectionTimeout(60); 
options.setKeepAliveInterval(60); //how often to send PINGREQ messages 
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1); //newest version 
client.connect(options); 
+0

主题的差异是由于这些代码来自我的订阅和发布程序。 “sensor/temp/out”用于接收数据,“sensor/temp/in”用于发送数据。至于额外的MQTT选项,我还没有使用它们,因为现在我专注于订阅和适当地显示消息。一旦我成功了,我一定会将这些选项添加到连接中。 – Ernani