2017-02-21 38 views
2

我有一個微控制器,通過恆溫器將其數據通過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的控制檯上訂閱和打印嗎?

回答

0

我已經設法讓發送的數據出現在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(); 
    } 
} 
1

如您所見: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

相關問題