How to read data from MQTT in Eclipse Paho

0 votes

I am trying to read sensor data using MQTT, using Eclipse Paho. I am successfully connected, but I want to read data. my code so far:

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

}

But i am unable to read data. Can someone please guide?

Aug 9, 2018 in IoT (Internet of Things) by Matt
• 2,270 points
941 views

1 answer to this question.

0 votes

You don't read data from a MQTT broker, instead you subscribe to a topic and get sent the data when ever a new message is published to that topic.

So you need to implement an instance of the MqttCallback interface and set it on the connection:

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

Then you need to tell the broker which topics you are interested in:

client.subscribe("topic/foo")
answered Aug 9, 2018 by anonymous2
• 4,240 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

Eclipse Paho: read data from MQTT Broker

From what I could analyze, you've been ...READ MORE

answered Aug 9, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
2,450 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,808 views
0 votes
1 answer

Publishing commands to device in IBM IoT using MQTT in Java

If you are publishing from an application, ...READ MORE

answered Jul 27, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,506 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,399 views
0 votes
1 answer

MQTT protocol connection error

Ok so you need two libraries to ...READ MORE

answered Jul 6, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points

reshown Jul 6, 2018 by Vardhan 3,067 views
0 votes
1 answer

Mosquitto 1.4.2 Websocket support

down vote In the dir mosquitto-1.4.X edit the ...READ MORE

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

IoT request response protocol

Based on your requirement of a light ...READ MORE

answered Jul 13, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,842 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,463 views
0 votes
1 answer

How to send a data from arduino uno to a webpage through esp8266 wifi module?

You are missing a few \r\n and the length ...READ MORE

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

How to get Unicast, Dns and Gateway Address in UWP?

Try this code Snippet I found here: https://social.msdn.microsoft.com/Forums/en-US/27a8b7a8-8071-4bc1-bbd4-e7c1fc2bd8d7/windows-10-iot-core-how-do-you-create-a-tcp-server-and-client?forum=WindowsIoT ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,214 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