Using HTTP, HTTPS, MQTT and CoAP from mbed OS


One of the biggest changes in mbed OS 5 compared to mbed OS 2.0 was the unification of network interfaces. Every interface, whether it's Ethernet, Wi-Fi, Cellular, 6LoWPAN or Thread mesh, implements the same NetworkInterface API. This makes it easier to write portable code that matches the available connectivity method. In addition this API is easy to implement on top of any library that supports socket primitives, so porting new targets is straightforward. Recently we also added the Easy Connect library, which allows users to switch between all supported connectivity methods through a config file - tremendously helpful for tutorials and workshops.

But adding connectivity is only one side of the story; actually doing something meaningful with the connection requires additional libraries. Recently we learned that it's not always clear which application protocols mbed supports, so in this blog post we'll show how to use the most popular internet protocols with mbed OS 5: HTTP, HTTPS, MQTT and CoAP. All examples run on every mbed OS 5 board that implements the NetworkInterface API.

Tip: Want a hassle-free and secure connection to the internet? Use mbed Device Connector with mbed Client.

HTTP

We can use TCP sockets to talk to any HTTP server, but HTTP can be a messy protocol, especially to parse. We have an example application that uses mbed OS 5 together with node.js/http-parser to make HTTP calls.

HTTP calls using mbed OS 5.

Tip: When talking to web services, you may also be interested in a JSON parser and serializer.

HTTPS

mbed OS 5 comes with mbed TLS, an open source SSL library. This example demonstrates how to use mbed TLS to download files over HTTPS.

HTTPS calls using mbed OS 5.

MQTT

The Eclipse™ Paho project is an open-source client implementation of the MQTT protocol, and a port for mbed has been around since 2014. Recently we updated the HelloMQTT application to use mbed OS 5.

MQTT client using mbed OS 5.

CoAP

The Constrained Application Protocol (CoAP) is a lightweight web protocol for IoT devices. It's similar to HTTP, but with a much lower footprint and additional features like multicast. mbed Device Connector, our device management solution, uses it as its transport layer - we therefore ship a CoAP library as part of mbed OS 5. We can use this library - which includes both a CoAP serializer and parser - to connect to any CoAP server.

CoAP client using mbed OS 5.

-

Jan Jongboom is Developer Evangelist IoT.