gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:19:03 24 4
gpt4 key购买 nike

我有一个带有恒温器的微 Controller ,它使用 MQTT 协议(protocol)通过 Raspberry Pi 将其数据发送到我的计算机。 Kura 已安装并在 Raspberry 上运行。

我在 Putty 上接收数据没有问题,但现在我需要在 Eclipse 上接收它,这样我才能开发程序。

我设法通过 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 接口(interface):

public class PublishSemInterface implements MqttCallback

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

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();
}
}

除了它也不起作用。在这两种情况下,当我运行代码时,控制台处于 Activity 状态,但当微 Controller 发送数据(显示在 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();
}
}

关于java - 如何订阅 MQTT 主题并在 Eclipse (Java) 上打印收到的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42378119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com