Saturday, April 9, 2016

Messaging Protocol to use


The differences between XMPP and MQTT. What are they, actually?

XMPP (eXtensible Messaging and Presence Protocol) was born as a protocol for messaging apps. Jabber uses it, Google Talk (today Hangouts) used it, WhatsApp uses it. It's a great and reliable messaging protocol, far more reliable than GCM is. It also has many implementation guides/tutorials/examples.
MQTT  (MQ Telemetry Transport) instead, was born as a communication protocol designed for low-power devices which have limited power capacity and low computational power. It's fast and reliable, and uses far less data&power than XMPP needs to do the same exact work. Facebook Messenger uses it. However it's not very popular and may lack tutorials/examples for beginning developers.
Here's a small table comparison on how much data those protocols use: XMPP versus MQTT: comparing apples with pears
...and here's a battery consumption comparison: Power Profiling: HTTPS Long Polling vs. MQTT with SSL, on Android


To conclude, which one you're going to use is up to you.
Both are good and reliable application protocols, even though XMPP relies on HTTP, which makes it automatically more heavyweight but it doesn't mean it's less adequate for this job.


https://www.quora.com/Which-is-the-best-protocol-to-use-for-IOT-implementation-MQTT-CoAP-XMPP-SOAP-UPnP
 

Wednesday, April 6, 2016

Mqtt client Sample

Server : Eclipse IOT for Mqtt
Client : paho 9eclipse project)
 Messaging : Active MQ
Download the jar files below to run the client

https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.client.mqttv3/1.0.2/
https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.mqtt.utility/1.0.2/


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 MqttPublishSample {
   
    public static void main(String[] args) {

        String topic        = "MQTT Examples";
        String content      = "Message from MqttPublishSample";
        int qos             = 2;
     
        String broker       = "tcp://x.x.x.x: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);
            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();
        }
    }
   
   

}




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.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MqttSubscribeSample implements MqttCallback {
   
    MqttClient myClient;
    MqttConnectOptions connOpt;
   
    static final String BROKER_URL = "tcp://xxxxx:1883";
    static final String M2MIO_DOMAIN = "";
    static final String M2MIO_STUFF = "things";
    static final String M2MIO_THING = "desktop13";//clientid //"";
    static final String M2MIO_USERNAME = "";
    static final String M2MIO_PASSWORD_MD5 = "";

    // the following two flags control whether this example is a publisher, a subscriber or both
    static final Boolean subscriber = true;
    //static final Boolean publisher = true;
   
    String myTopic = "VirtualTopic.News";
    String content = "Message from MqttPublishSample";
    int qos = 0;

   
    public static void main(String[] args) {
        MqttSubscribeSample smc = new MqttSubscribeSample();
        smc.runClient();
    }
   
   
   
    public void runClient() {
        // setup MQTT Client
        String clientID = M2MIO_THING;
        connOpt = new MqttConnectOptions();
       
        connOpt.setCleanSession(true); // non-durable subscriber
        connOpt.setKeepAliveInterval(30);
        connOpt.setUserName(M2MIO_USERNAME);
        connOpt.setPassword(M2MIO_PASSWORD_MD5.toCharArray());
       
        // Connect to Broker
        try {
            myClient = new MqttClient(BROKER_URL, clientID);
            myClient.setCallback(this);
            myClient.connect(connOpt);
        } catch (MqttException e) {
            e.printStackTrace();
            System.exit(-1);
        }
       
        System.out.println("Connected to " + BROKER_URL);

        // setup topic
        // topics on m2m.io are in the form //
        //String myTopic = M2MIO_DOMAIN + "/" + M2MIO_STUFF + "/" + M2MIO_THING;
        //MqttTopic topic = myClient.getTopic(myTopic);

        // subscribe to topic if subscriber
        if (subscriber) {
            try {
                int subQoS = 2;
                myClient.subscribe(myTopic, subQoS);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
   

    @Override
    public void connectionLost(Throwable t) {
        System.out.println("Connection lost!");
        // code to reconnect to the broker would go here if desired
       
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken arg0) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void messageArrived(String arg0, MqttMessage message) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("-------------------------------------------------");
        System.out.println("| Topic:" + myTopic);
        System.out.println("| Message: " + new String(message.getPayload()));
        System.out.println("| Time: " + System.currentTimeMillis());
        System.out.println("-------------------------------------------------");
    }

}


 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 MqttQueueSample {
  
     public static void main(String[] args) {
        
          String topic        = "VirtualTopic.News";
            String content      = "Message from offline test";
          
       
            int qos             = 0;
       
            String broker       = "tcp://xxxxx:1883";
            String clientId     = "desktopclient1";
            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);
                System.out.println("Connected");
              
               // sampleClient.
               // System.out.println("Message Subscrived");
              
                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();
            }

      
        }

  }