admin管理员组文章数量:1305515
I'm trying to play around with Paho MQTT for Java to send and listen to messages from my Meshtastic chat. Following an example code and a documentation I found, I tried the following:
import .eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import .eclipse.paho.client.mqttv3.MqttCallback;
import .eclipse.paho.client.mqttv3.MqttClient;
import .eclipse.paho.client.mqttv3.MqttConnectOptions;
import .eclipse.paho.client.mqttv3.MqttException;
import .eclipse.paho.client.mqttv3.MqttMessage;
public class Main {
public static void main(String[] args) {
String broker = "tcp://BROKERLINK:1883";
String clientId = "clientId";
String topic = "msh/EU_433/2/e/CHANNEL/1234";
int subQos = 1;
int pubQos = 1;
String msg = "Hello MQTT";
try {
MqttClient client = new MqttClient(broker, clientId);
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName(clientId);
options.setPassword("PASSWORD".toCharArray());
options.setCleanSession(true);
client.connect(options);
if (client.isConnected()) {
client.setCallback(new MqttCallback() {
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println("topic: " + topic);
System.out.println("qos: " + message.getQos());
System.out.println("message content: " + new String(message.getPayload()));
}
public void connectionLost(Throwable cause) {
System.out.println("connectionLost: " + cause.getMessage());
}
public void deliveryComplete(IMqttDeliveryToken token) {
System.out.println("deliveryComplete: " + token.isComplete());
}
});
client.subscribe(topic, subQos);
MqttMessage message = new MqttMessage(msg.getBytes());
message.setQos(pubQos);
client.publish(topic, message);
}
client.disconnect();
client.close();
} catch (MqttException e) {
e.printStackTrace();
}
}
}
Whenever I send a message from my phone I can see it using MQTT Explorer. The message I send from the Java code can also be seen there under 1234
, yet it does not appear in the Meshtastic chat. What am I doing wrong?
本文标签: Publish message to Meshtastic chat through MQTT using JavaStack Overflow
版权声明:本文标题:Publish message to Meshtastic chat through MQTT using Java - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741803428a2398359.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论