IoT: do I need to use MQTT or HTTP?

I am working on a device that senses and collects information from an environment such as temperature, humidity, etc.

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

He is almost in a deep sleep state most of the time, and he only wakes up when he needs to sense and transmit data. This operation takes about 1-2 minutes and then goes back to sleep.

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

I am currently sending data over HTTP, but I am wondering if it makes sense to implement MQTT? Should I take advantage of HTTP for this scenario?

+3


source to share


4 answers


There is a wealth of literature around MQTT versus HTTP that you should go into in detail, but here are some of the high points for your application.

MQTT

  • MQTT allows for persistent connections, which can save significant resources over HTTP. This is most relevant if you are using SSL.
  • If you are only sending a few metrics, MQTT will generally have more bandwidth than HTTP.
  • Since MQTT is designed to deliver data (not entire pages), its pub / submodule offers a lot of useful built-in features like save and last will.
  • MQTT also offers an easy way to implement encryption, authentication, and access control.
  • MQTT is for situations where your connection might be intermittent or unreliable. Its various levels of quality of service provide you with important ways to ensure that your data is sent reliably.
  • In larger applications, the MQTT broker provides easier management in my experience. I especially like that there is a standardized set of metrics for measuring performance.
  • The MQTT topic / subtopic structure helps organize your data and makes it easy to scale and share resources across multiple projects.
  • This may be personal preference, but I find the MQTT protocol easier to understand, troubleshoot, and program than HTTP. In particular, the Python Paho MQTT library is simple and works well.

In conclusion, MQTT has many features that would seem to be perfect for your application. However, you will most likely repeat many of them using HTTP, but it will take more work.

HTTP



  • It is largely supported everywhere, allowing for easier compatibility with firewalls. This can be important if you are deploying on networks that you do not control.

  • This is a more common protocol so that you (and your end users) will already feel comfortable with it. Likewise, you can already understand a security model that will make it easier to protect.

  • There are some differences between the various MQTT implementations that can be difficult (for example, I use Mosquitto and sometimes get confused when people talk about setting them up for HiveMQ). HTTP strikes me as more versatile and has a much larger community of people who can help you.

Thus, HTTP has some inherent disadvantages compared to MQTT, but it will get the job done and may be more practical if the specific features of MQTT are not to your liking.

But really, if it's a large-scale project, it's worth trying both and running some tests and tests on your specific application and environment. It wouldn't be hard to set up a test environment and get some metrics. If it is more of a hobby / one-off project, I would use what you find more convenient or more interesting.

Sources / Further reading:

http://stephendnicholas.com/posts/power-profiling-mqtt-vs-https https://www.ibm.com/developerworks/community/blogs/sowhatfordevs/entry/using_mqtt_protocol_advantages_over_http_in_mobile_application=development5 ? com / @ shubhanshusingh / http-vs-mqtt-9008d448bf88 https://www.slideshare.net/paolopat/mqtt-iot-protocols-comparison https://mobilebit.wordpress.com/2013/05/03/rest-is -for-sleeping-mqtt-is-for-mobile / http://bec-systems.com/site/1200/iot-protocols-mqtt-vs-coap-vs-http

+3


source


I'm new to the game, but I like mqtt for collecting data and sending information to devices.

I am using rf200 synapse chips (self learning cell network) and I have built a bridge by connecting one of the rf200s in series to the esp8266 wifi chip.



Basically I have mqtt_in and mqtt_out as themes and my iOS / android c # xamarin app connects to the request mqtt broker from rf200 using mqtt_in (to rf200 mftt network) and nodes respond with mqtt_out (from rf200 mesh). I am gathering all the information when the strings are parsed as needed and converted to whatever I need.

This is very low overhead and the mqtt broker is very easy to set up.

+1


source


We tested MQTT vs HTTP (REST) over SSL to the usual server and Raspberry Pi board both in terms of performance and in terms of energy consumption. The results depend on the use case and the device on which the process is running.

As for your use case, we also have a dedicated test => delivery of multiple messages (multiple probes) via HTTP, HTTP packet, or MQTT. The results are pretty straightforward, if you have the ability to send data over a single HTTP request, that would be the best option. MQTT comes in second, and HTTP with message delivery is much less efficient and slower than MQTT.

+1


source


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

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

      

}

0


source







All Articles