MQTTClient for mbed - version 2.0


Background: As part of RFID messaging project, a MQTT client is required to run on mbed platform to publish RFID check in events to third party via MQTT protocol. There are rich MQTT clients on PCs and other embedded platform, but there is no previous implementation of such a client on mbed platform. To move forward, this work aims to develop a fullly functional MQTT client running on mbed platform. Detailed information about MQTT protocol are available at

Acknowledgement: The development of this library has largely inspired by excellent works of Nick O’leary, who wrote a  MQTT client for Arduino platform @

mbed Client for MQTT v2.0

This is a simple MQTT client developed to run on mbed. The newest version is 2.0.

Change History: v2.0: 1. The structure of the library is changed significantly, and now based on TCP socket event. 2. Can subscribe to a topic and extract messages from it. 3. Change parameter of callback function. 4. Change class name to MQTTClient. 5. live function does not return a value now. 6. Updated API.

v1.0: 1. Connect to MQTT server via TCP connection. 2.  Publish message to MQTT server. 3.  Keep alive with server.

Limitations: 1. Only QOS 0 messaging is supported. 2. Keepavlive interval is set to 30s.

Hardware Requirements: You do need a ethernet connector or alternate ethernet shield, and a pair of ethernet cables. I used a expansion shied from Sparkfun, which has an Ethernet jack and two USB ports.

Library: EthernetNetIf

MQTT resouces: rmsb -- A small MQTT service broker, a server. iA92 -- Java based MQTT client. Those two programs can be found @

A simple pub-sub example: This is simple test of publish and subscribe function of MQTTClient version 2.0. The idea is that let the mbed subscribes on topic “/mirror” and publishes to “/mbed”. And on the other side, a MQTT client, iA92 running on a PC subscribes on “/mbed” and publishes strings to “/mbed”. Then, whatever messages publish on “/mirror” topic will appear on topic “/mbed”. Thus, this can prove both publish and subscribe functions are working.


Results: This is a screen shot of rsmb when broker accepts connection from mbed.

The picture below is taken at the moment that the iA92 publishes a message “mirror, mirror..” to topic “/mirror”. The same string can be also seen on subscribed topic “/mbed”. This proves both subscription and publication functions performs well.

Test platform: Mac OSX + rsmb 1.2.0 + iA92 + NXP LPC1768 + ethernet shield from Sparkfun


MQTTClient(struct ip_addr serverIp, int port, void (*callback)(char*, char*));

Description: Class constructor, create a client instance. Parameters:

@serverIp: IP address of server.

@port: Port number TCP connection goes through

@void (*callback)(char*, char*): Call back function when data arrives from port.  

int connect(char * id); Description: Open TCP port, connect to server on given IP address. Parameters: @id: The client name shown on MQTT server. Returns: -1: If connect to server failed. -2: Failed to open session on server. 1: Connection accessed.

int publish(char *pub_topic, char *msg); Description: Publish a message on a topic. Parameters: @pub_topic: The topic name the massage will be publish on. @msg: The massage to be published. Returns: -1: Failed to publish message. 1: Publish sucessed.

void subscribe(char *topic); Description: Subscribe to a topic. Parameters: @topic: The topic name to be subscribed.

void live(); Description: Send keep alive message to server on specified rate, maintain connections with server.

void disconnect(); Description: Disconnect from server.