Sample MQTT program - simple send and receive
You are viewing an older revision! See the latest version
Homepage
Warning! This example and API are still a work 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 included the ethernet interface needed. To use the MQTT with ethernet, include these 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.