IoT: do I need to use MQTT or HTTP?

zephrax picture zephrax · May 19, 2017 · Viewed 13.8k times · Source

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?

Answer

blp picture blp · May 19, 2017

There is a rich literature around MQTT vs. HTTP which you should delve into for details, 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 be more bandwidth efficient than HTTP.
  • As MQTT is designed for delivering data (not entire pages) its pub / sub model offers many useful built-in features such as retention and last will.
  • Also MQTT offers a simple way of implementing encryption, authentication, and access control.
  • MQTT is intended for situations where your connection may be intermittent or otherwise unreliable. Its various Quality-of-Service levels provide you with significant ways of ensuring that data is sent reliably.
  • In larger applications the MQTT broker provides for easier management in my experience. I particularly like that there is a standardized set of metrics for measuring performance.
  • The topic/subtopic value structure of MQTT helps to organize your data and makes it easy to scale things up and share resources between multiple projects.
  • This may be personal preference, but I find the MQTT protocol easier to understand, troubleshoot, and program than working with HTTP. In particular, the Python Paho MQTT library is simple and works well.

In summary, MQTT has many features which would seem to be perfect for your application. That said, you could likely replicate many of them using HTTP but it would require more work.

HTTP

  • Pretty much supported everywhere which ensures easier compatibility with firewalls. This could be important if you are deploying on networks you do not control.

  • It is a more common protocol so you (and your end users) may already feel comfortable with it. Similarly you may already understand the security model which would make it easier to secure.

  • There are some differences between different MQTT implementations which might cause difficulties (i.e I use Mosquitto and sometimes find it confusing when people talk about their HiveMQ setup). HTTP strikes me as more universal and has a much larger community of people available to help you.

So HTTP has some inherent disadvantages versus MQTT but it will get the job done and may be more practical if the specific features of MQTT don't appeal to you.

But really if this is a large-scale project it is worth trying both and running some benchmarks and tests in your particular application and environment. It wouldn't be hard to set up a test environment and get some metrics. If this is more of a hobby / one-off project then I would use whichever one you are more comfortable with or find 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?lang=en https://medium.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