---
title: Paho Embedded - MQTT Client Library Encyclopedia
description: Explore the basic features of Paho Embedded and how to use this MQTT Client library for connecting to an MQTT Broker.
date: 2015-08-17
published: 2015-08-17
author: Ian Craggs
tags:
  - MQTT Client
source: https://www.hivemq.com/blog/mqtt-client-library-encyclopedia-paho-embedded
---

# Paho Embedded - MQTT Client Library Encyclopedia

The Paho embedded client libraries arose out of the desire to allow the smallest microcontrollers to easily connect to [MQTT](/mqtt/) servers. In this world, there are a large number of operating systems and network APIs, so it was important that the libraries be as portable as possible. With this in mind I decided, after some helpful discussions, that the attributes needed by the APIs were:

- to use very limited and predictable resources: no dynamic memory allocation
- not to rely on any particular libraries for networking, threading or memory management
- ANSI standard C, or basic C++ for maximum portability

## Overview of Paho Embedded MQTT Client Library&#x20;

While I tried to make these APIs as helpful as possible, the constraints of portability and low, predictable resource use means that they are sometimes less helpful than other MQTT libraries. Note that I say “APIs”: there is more than one. The two that have been released in Paho so far are:

1. **MQTTPacket**: The lowest level library in C, the base on which the other APIs are built. It simply deals with serialization and deserialization of MQTT packets. There are some helper functions for reading packets from and writing them to, the network. It will work just about anywhere a C compiler exists.
2. **MQTTClient**: A C++ library first written for the mbed platform. It is written in the conventional language for mbed, C++. It still avoids dynamic memory allocations, and has replaceable classes for OS and network dependent functions. Use of the STL is also avoided. It is based on, and requires, MQTTPacket.

A C version of MQTTClient also exists, and an asynchronous API is planned along with other optional features. For the purpose of this blog, I’m going to confine myself to the MQTTClient API.

## Features Supported by Paho Embedded MQTT Client Library&#x20;

## Advanced Features Supported by Paho Embedded MQTT Client Library&#x20;

## Usage

### Installation of Paho Embedded MQTT Client Library

Because of the variety of microcontroller environments, there is no one install mechanism. The simplest option is to clone the git repository - this contains build scripts for Linux. On mbed, you can import the library into your IDE from [](https://www.eclipse.org/paho/clients/c/embedded/). An Arduino package is available from the [Arduino Libraries page](https://www.arduinolibraries.info/libraries/arduino-mqtt).

#### Network and Timer Classes

First of all you need classes, which implement network and timer functionality needed by the API for the platform you are building your application on. Examples for Linux, mbed and Arduino are already available. It should be a simple matter to create your own for another environment. The timer class (which counts down, not up) needs to implement the following methods:

and the network class the following:

as a minimum.

#### Application Setup

Before including the header file, if you want to use QoS 2, you must set:

otherwise the QoS code is omitted to save resources. If you activate QoS 2, then there is another setting:

which is the maximum number of unprocessed, or “in flight” messages that the API can handle. Then we can include the header:

### How to Connect Paho Embedded MQTT Client Library?

First of all you need to establish the network connection. This is not done within the library, for portability. The mbed example for this is:

We create the client object by passing a network class instance to it:

Note the class names - these are passed as template parameters. `MQTT::Client` also has two other optional parameters:

which we have not used here. Now we can issue the MQTT connect:

### Connect With LWT

### Connect With Username/Password

### How to Publish MQTT Message Using Paho Embedded?

### How to Subscribe to An MQTT Message Using Paho Embedded?

Each subscription has its own callback, which receives messages on the subscribed topic.

### How to Unsubscribe to An MQTT Message Using Paho Embedded?

### How to Disconnect?

Similar to connect, the disconnect method just issues the MQTT disconnect. You must close the network connection explicitly.

### Using TLS/SSL

To use TLS, you must implement a network class, which establishes the secured connection and then reads from and writes to, that secured connection. The main MQTTClient code does not contain any specific TLS code. An example of how to do this is described [here](https://os.mbed.com/users/icraggs/notebook/using-tls-with-the-mqtt-c-client/).

### An Example Application of Using Paho Embedded

- [A simple general application (HelloMQTT) for mbed](https://developer.mbed.org/teams/mqtt/code/HelloMQTT/file/7f7aba7a4a8b/main.cpp)
- [An application which connects to IBM’s Internet of Things Foundation service](https://developer.mbed.org/teams/IBM_IoT/code/IBMIoTClientEthernetExample/file/94da9de96d54/main.cpp)
- [HelloMQTT for Arduino](https://github.com/eclipse/paho.mqtt.embedded-c/blob/master/MQTTClient/samples/arduino/Hello/Hello.ino)
- [Linux samples](https://github.com/eclipse/paho.mqtt.embedded-c/tree/master/MQTTClient/samples/linux)

Do check out[ HiveMQ MQTT Client libraries and other featured MQTT client libraries.](/mqtt/mqtt-client-library-encyclopedia)
