mbed client for MQTT - version 1.0

 

 

Tags: mbed, MQTT, client, pub-sub, mosquito.   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 http://mqtt.org.

Acknowledgement: The development of this library has largely inspired by excellent works of Nick O’leary, who wrote a  MQTT client for Arduino platform @http://knolleary.net/arduino-client-for-mqtt/.

mbed Client for MQTT

This is a simple MQTT client developed to run on mbed. The library is still under developing, and has not be able to implement all functionality of MQTT protocol. The current version of the library is version 1.0.

MQTTClient for mbed version 2.0 is currently available at: http://ceit.uq.edu.au/content/mqttclient-mbed-version-20

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

Limitations: 1. Can not receive messages from server. 2. Only QOS 0 messaging is supported

Requirements: The MQTT client library itself requires a third part Ethernet and TCP libraries to support basic networking functions. And you also need a ethernet connector, and a pair of ethernet cables. I used a expansion shied from Sparkfun, which has an Ethernet jack and two USB ports.

Usage:

1. Import this library into your compiler as a program. http://mbed.org/users/no2chem/programs/EthernetTester/6095m

2. Then drag “lwip” folder into your mbed program you wish to implement MQTT client.

3. Set up Ethernet interface in main function as shown below. You only need to change “serverIP”, the IP address of MQTT server, to right IP address you want to connect to.

4. Create a instance of MQTT client and publish a message.

Test: I wrote a simple program to test functions of this MQTT client. The mbed and a mac are wired to same router. And the program is designed to connect the RSMB on mac using a name of “mbed”. After sucessful connection, it will first publish a message “Hello, here is mbed!” on topic “/mbed”, and then sends “Mirror, mirror where r u..” three times on same topic. And then the program will run 200s doing nothing, except sending a keep-alive signal every 10 seconds.

This is a shot from the RSMB, when the broker accepted the connection from mbed.  

This is the first message from mbed shown on a iA92 client, running on a mac. Here is the second message “Mirror, Mirror, where r u..” send from mbed.

This serial port window shows progresses of full test session.

The library has been tested on mbed NXP LPC1768, and  IBM’s RSMB  is chosen as MQTT server and iA92 java as a client running on PC and mac.

Downloads: Full codes of the test program shown above are attached below together with MQTT client for mbed version 1.0. Also, the .bin generated for the test program is included.

API

PubSub_mbed(struct ip_addr serverIp, int port, tcp_accept_fn fn);

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

@serverIp: IP address of server.

@port: Port number TCP connection goes through

@fn: Call back function when data arrives from port.  tcp_accept_fn  is defined by:

typedef err_t (*tcp_accept_fn)(void *arg, struct tcp_pcb *newpcb, err_t err);

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 send the identification message to broker. 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.

int live(); Description: Send keep alive message to server on specified rate, mentain connections with server. Returns: 0: If TCP connection is closed. 1: Keep alive message send successful.

void disconnect(); Description: Disconnect from server.