Sample MQTT program - simple send and receive

Dependencies:   C12832 MQTT

Dependents:   MQTT_G_SENSOR

You are viewing an older revision! See the latest version

Homepage

This example and API are working, but are still in progress. Please give us your feedback.

HelloMQTT is an example of using the MQTT API. The MQTT API is portable across network interface stacks. MQTT is designed to be used with TCP/IP, but any transport with similar characteristics should be suitable.

HelloMQTT uses the EthernetInterface to show how this works. The MQTT library contains an MQTTEthernet.h header, which is a wrapper around the mbed ethernet interface. To use the MQTT API with ethernet, include the following two headers:

#include "MQTTEthernet.h"
#include "MQTTClient.h"

then instantiate an MQTT client like this:

MQTTEthernet ipstack = MQTTEthernet();             
MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);

Countdown is a timer class supplied in the MQTT library that is specific to mbed, so you should not need to change that.

If you want to use the MQTT API with a different network stack, create an interface header similar to MQTTEthernet.h. The class you create must have the following two methods at a minimum:

    int read(char* buffer, int len, int timeout);
    int write(char* buffer, int len, int timeout);

where the timeout is in milliseconds, and the return value is the number of characters read from or written to the network, or -1 on failure.


All wikipages