Reputation: 1
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 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;
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?
Upvotes: 0
Views: 71