Jump to content









Photo

MQTT Broker client program

Mqtt clientprogram java broker

  • Please log in to reply
3 replies to this topic

#1 parvathyr

parvathyr

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 18 December 2015 - 01:41 PM

Hi,

 

Can you provide a sample client program in java using MQTT .I

 

In the given code what should be given in the clientId and server broker url ?

 

 

 
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;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
 
public class MqttPublishSample1 {
 
    public static void main(String[] args) {
 
        String topic        = "MQTT Examples";
        String content      = "Message from MqttPublishSample";
        int qos             = 2;
        String broker       = "tcp://iot.eclipse.org:1883";
        String clientId     = "JavaSample";
        MemoryPersistence persistence = new MemoryPersistence();
 
        try {
            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            System.out.println("Connecting to broker: "+broker);
            sampleClient.connect(connOpts);
            if(sampleClient.isConnected()==true)
            {
            System.out.println("Connected");
            }
            System.out.println("Publishing message: "+content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
            System.exit(0);
        } catch(MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
        }
    }
}

 



#2 supriyav

supriyav

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 19 December 2015 - 06:00 PM

Hi Parvathy,

 

You can give "mqtt.carriots.com:1883" as broker url.

 

 

   



#3 parvathyr

parvathyr

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 21 December 2015 - 03:58 AM

Hi,

 

What can I give as clientId ? It is specified as clientId should not exceed 23 charcaters .



#4 dompablo

dompablo

    Master of Puppets

  • Members
  • PipPip
  • 14 posts

Posted 22 December 2015 - 03:23 PM

Hi Parvathyr,

 

You don't need to set a client id. This is a java example, the code is not tested so may contain errors.

 

Here you can find more info about MQTT (https://www.carriots...umentation/mqtt) and an example (https://www.carriots...end_stream/mqtt) or try to search on github (https://gist.github....MqttClient java)

 

 

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 MqttPublishSample1 {
 
    public static void main(String[] args) {
 
        String broker       = "tcp://mqtt.carriots.com:1883";
        String username     = "{apikey}"; // set your apikey
        String topic        =  username+"/streams";
        
        // set your ID_DEVELOPER_DEVICE
        String content  = "{\"protocol\": \"v2\", \"device\": \"{ID_DEVELOPER_DEVICE}\", \"at\": \"now\", \"data\": {\"temp\": 21}}";
 
        try {
            MqttClient sampleClient = new MqttClient(broker);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            connOpt.setUserName(username);
            connOpt.setPassword("");
            System.out.println("Connecting to broker: "+broker);
            sampleClient.connect(connOpts);
            if(sampleClient.isConnected()==true) {
                System.out.println("Connected");
            }
            System.out.println("Publishing message: "+content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(0);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
            System.exit(0);
        } catch(MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
        }
    }
}






Also tagged with one or more of these keywords: Mqtt, clientprogram, java, broker

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users