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