IoT: do I need to use MQTT or HTTP?

Viewed 16969

I'm working on a device that sense and collect information from the environment such as temperature, humidity, etc.

The device is not connected to any power source, but it has a battery, and a solar panel to charge it.

It's almost in a deep sleep state most of the time, and it only wake up when it need to sense and transfer data. This operation takes about 1-2 minutes, and then it goes to sleep again.

I'm not an expert in this area, but I think MQTT should be a good option if the device need to be accessible to receive messages from a topic all the time, but in my scenario it only read sensors, and send data to a server periodically.

Currently I'm sending the data through HTTP, but I'm wondering if it makes sense to implement MQTT? Should I get any advantage over HTTP for this scenario?

4 Answers

We have tested MQTT vs HTTP(REST) over SSL from normal server and Raspberry Pi boards both from performance point of view and from energy consumption. The results depend on use-case and device where process is running.

Regarding your use case we have special test as well => delivering many messages (multiple probes) via HTTP, HTTP-batch or MQTT. The results are quite simple, if you have possibility to send data in one HTTP request - it will be the best option. MQTT is on the second place and HTTP with per-message delivery is much less power efficient and slower then MQTT.

I think MQTT is best for u..You can use eclipse paho library.. This class may help u.

public class PahoMqttClient {

private static final String TAG = "PahoMqttClient";
private MqttAndroidClient mqttAndroidClient;

private String broker_userName,broker_password; public MqttAndroidClient getMqttClient(Context context, String brokerUrl, String clientId,String broker_password) {

    this.broker_userName=clientId;
    this.broker_password=broker_password;

    mqttAndroidClient = new MqttAndroidClient(context, brokerUrl, clientId);
    try {
        IMqttToken token = mqttAndroidClient.connect(getMqttConnectionOption(), null, new IMqttActionListener() {
                    @Override
                    public void onSuccess(IMqttToken iMqttToken) {
                        Log.d(TAG, "Success");
                    }

                    @Override
                    public void onFailure(IMqttToken iMqttToken, Throwable exception) {
                        Log.d(TAG, "Failure " + exception.toString());

                    }
                });


    } catch (MqttException e) {
        e.printStackTrace();
    }

    return mqttAndroidClient;
}

public void disconnect(@NonNull MqttAndroidClient client) throws MqttException {
    IMqttToken mqttToken = client.disconnect();
    mqttToken.setActionCallback(new IMqttActionListener() {
        @Override
        public void onSuccess(IMqttToken iMqttToken) {
            Log.d(TAG, "Successfully disconnected");
        }

        @Override
        public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
            Log.d(TAG, "Failed to disconnected " + throwable.toString());
        }
    });
}

@NonNull
private DisconnectedBufferOptions getDisconnectedBufferOptions() {
    DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
    disconnectedBufferOptions.setBufferEnabled(true);
    disconnectedBufferOptions.setBufferSize(100);
    disconnectedBufferOptions.setPersistBuffer(false);
    disconnectedBufferOptions.setDeleteOldestMessages(false);
    return disconnectedBufferOptions;
}

@NonNull
private MqttConnectOptions getMqttConnectionOption() {
    MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
    mqttConnectOptions.setCleanSession(false);
    mqttConnectOptions.setAutomaticReconnect(true);
    mqttConnectOptions.setKeepAliveInterval(120);
    //mqttConnectOptions.setWill(Constants.PUBLISH_TOPIC, "I am going offline".getBytes(), 1, true);
    mqttConnectOptions.setUserName(broker_userName);
    mqttConnectOptions.setPassword(broker_password.toCharArray());
    return mqttConnectOptions;
}


public void publishMessage(@NonNull MqttAndroidClient client, @NonNull String msg, int qos, @NonNull String topic)
        throws MqttException, UnsupportedEncodingException {
    byte[] encodedPayload = new byte[0];
    encodedPayload = msg.getBytes("UTF-8");
    MqttMessage message = new MqttMessage(encodedPayload);
    message.setId(320);
    message.setRetained(false);
    message.setQos(qos);
    try {
        client.publish(topic, message);
    }catch (Exception e){ Log.e("PUB", e.getMessage());}

}

public void subscribe(@NonNull MqttAndroidClient client, @NonNull final String topic, int qos) throws MqttException {
    IMqttToken token = client.subscribe(topic, qos);
    token.setActionCallback(new IMqttActionListener() {
        @Override
        public void onSuccess(IMqttToken iMqttToken) {
            Log.d(TAG, "Subscribe Successfully " + topic);
        }

        @Override
        public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
            Log.e(TAG, "Subscribe Failed " + topic);

        }
    });
}

public void unSubscribe(@NonNull MqttAndroidClient client, @NonNull final String topic) throws MqttException {

    IMqttToken token = client.unsubscribe(topic);

    token.setActionCallback(new IMqttActionListener() {
        @Override
        public void onSuccess(IMqttToken iMqttToken) {
            Log.d(TAG, "UnSubscribe Successfully " + topic);
        }

        @Override
        public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
            Log.e(TAG, "UnSubscribe Failed " + topic);
        }
    });
}

}

Related