picoTCP has been used for academic projects before and continues to be popular among thesis students. For his Masters project, Maarten Arits is investigating how to implement an MQTT layer on top of picoTCP. The stack is quite suitable to implement new layers into and to investigate new networking technologies.
Message Queue Telemetry Transport (MQTT) will be a new layer on top of picoTCP, allowing you to use a simple publish/subscribe protocol. MQTT is a lightweight messaging protocol on top of TCP/IP. It adds an extra abstraction layer, allowing easy and reliable messaging between MQTT compatible nodes. MQTT is optimized for lossy network connections with low data rates.
The best way to illustrate how MQTT works is by example. Building light and temperature sensors, you want to get the data from them to a heater to determine if it should be turned on or not. With MQTT this is trivial: you start by opening a connection to an MQTT server. With this connection two queues will be opened, one for incoming messages and one for outgoing messages.
Next you want your sensors to be able to publish their data to the server. This is done easily by calling the publish function with your message and the topic on the server to which you wish to publish. As an extra feature MQTT offers you different quality of service levels for each message.
at level 0 the message is sent once, not guaranteeing any delivery
at level 1 the message will be kept sending until it is delivered at least once
at level 2 the message will be delivered exactly once
As mentioned before, MQTT makes use of topics to which you publish or subscribe. With each topic having a different name, if you publish a message to a topic, everybody that is subscribed to it will receive the message. You can define topics in a tree-like structure allowing more fine tuned management of the messages being distributed. In the sensor use case, it would make sense to create a topic for each room and a subtopic for temperature and one for light.
Subscribing to a topic requires a single call to the subscribe function with the topic name. In the use case, our heater would like to know the temperature and light intensity of the living room. It can subscribe to “livingroom/temperature” and “livingroom/light” or it could use a wildcard “livingroom/+” and request the quality of service it would like to have.
What to expect
As a software engineer, it is important to know that the order of messages is not guaranteed and that MQTT doesn’t offer multicast or any network optimization. Real-time is absolutely not one of the features that MQTT aims for. Why these limitations? MQTT is intended for infrequent messaging (you don’t want to overflow your fragile network connection) and to allow devices to sleep for long periods of time without losing any messages. Then why should you be excited about MQTT? The moment you publish a message into the queue, MQTT will take care of the delivery process. It will automatically reestablish a lost connection, keep the connection alive and handle the different stages of delivery. If subscribed to a topic, you will receive the messages without having to worry about the connection.
The main reason why MQTT is very popular as a communication channel between devices is because it’s super lightweight. There are many so called Message Queueing Bus implementations that offer many features, but also require a lot from your platform. The picoTCP team is eager to see how Maarten will get all of this done.