Eclipse Paho read data from MQTT Broker

0 votes
I'm using the Eclipse Paho and I can ping my device but I don't know why I'm not being able to read my sensor data using MQTT.

The following is my code, please have a look to check what I could have been doing wrong:

  public static void main(String[] args) {
    MqttClient client;
    MqttMessage msg;
    MemoryPersistence persistence;
    MqttConnectOptions conn;
    IMqttMessageListener listen;

    String broker = "tcp://url:1883";
    String str = "password";
    char[] accessKey = str.toCharArray();
    String appEUI = "userID";

    try {
        persistence = new MemoryPersistence();
        client = new MqttClient(broker, appEUI, persistence);
        conn = new MqttConnectOptions();
        conn.setCleanSession(true);
        conn.setPassword(accessKey);
        conn.setUserName(appEUI);
        client.connect(conn);
        //client.connect();

        if(client.isConnected()) {
            System.out.println("Connected..");
        }else {
            System.out.println("Unable to connect");
            System.exit(0);
        }

        msg = new MqttMessage();
        byte[] data = msg.getPayload();
        System.out.println(d);

    }catch(Exception x) {
        x.printStackTrace();
    }

}
Please help!
Aug 9, 2018 in IoT (Internet of Things) by Bharani
• 4,660 points
2,483 views

1 answer to this question.

0 votes

From what I could analyze, you've been trying to read data from the broker directly, but that's not how it works exactly. With the MQTT broker, you will just have to subscribe to a topic and receive data everytime a new message gets published to it.

Try setting the connection on an instance of the MqttCallback interface:

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

Although, you don't need to create the desired topic, just tell the broker the topics you are interested in, like this:

client.subscribe("topic/foo")

answered Aug 9, 2018 by DataKing99
• 8,250 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

How to read data from MQTT in Eclipse Paho?

You don't read data from a MQTT ...READ MORE

answered Aug 9, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
957 views
0 votes
1 answer

What is the maximum message length for a MQTT broker?

It's not entirely clear what you're asking ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
9,487 views
0 votes
1 answer

AWS IoT login from android MQTT client using IAM is not working

Seeing your comments and questions. I had ...READ MORE

answered Jul 24, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
2,368 views
0 votes
1 answer

Send data from cloud to aws iot thing

Your solution is not recommend because there ...READ MORE

answered Aug 2, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,417 views
0 votes
1 answer

MQTT broker for Google App Engine

You had a hard time finding anything ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,568 views
0 votes
1 answer

Where is published message stored by mosquitto broker if offline?

Well, of course, they are stored! That ...READ MORE

answered Aug 10, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
3,823 views
0 votes
1 answer

Need to enclose MQTTCLient Instance in try catch block

The instance doesn't need to be surrounded by try/catch, but ...READ MORE

answered Aug 21, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,268 views
0 votes
1 answer

MQTT : Connection lost on MQTT subscriber to Internet of Things Server

An invalid topic "matteo" seems to be causing the ...READ MORE

answered Oct 3, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
4,665 views
0 votes
1 answer

How to use the data I receive from Azure IoT Hub?

The payload you receive will be a ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
1,818 views
0 votes
1 answer

Changing message format in KURA MQTT cloud client

The Eclipse Kura communicates with a MQTT ...READ MORE

answered Jul 29, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
1,188 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP