MQTT / HelloMQTT

Sample MQTT program - simple send and receive

Dependencies:   C12832 MQTT

Dependents:   MQTT_G_SENSOR

This program and the MQTT libraries it uses are part of the EclipseTM Paho project; specifically the embedded client.

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.

Committer:
Jan Jongboom
Date:
Tue Jan 10 18:10:17 2017 -0600
Revision:
20:49c9daf2b0ff
Update to mbed OS 5, use NetworkInterface for multiple connectivity methods - not just ethernet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jan Jongboom 20:49c9daf2b0ff 1#ifndef _MQTTNETWORK_H_
Jan Jongboom 20:49c9daf2b0ff 2#define _MQTTNETWORK_H_
Jan Jongboom 20:49c9daf2b0ff 3
Jan Jongboom 20:49c9daf2b0ff 4#include "NetworkInterface.h"
Jan Jongboom 20:49c9daf2b0ff 5
Jan Jongboom 20:49c9daf2b0ff 6class MQTTNetwork {
Jan Jongboom 20:49c9daf2b0ff 7public:
Jan Jongboom 20:49c9daf2b0ff 8 MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) {
Jan Jongboom 20:49c9daf2b0ff 9 socket = new TCPSocket();
Jan Jongboom 20:49c9daf2b0ff 10 }
Jan Jongboom 20:49c9daf2b0ff 11
Jan Jongboom 20:49c9daf2b0ff 12 ~MQTTNetwork() {
Jan Jongboom 20:49c9daf2b0ff 13 delete socket;
Jan Jongboom 20:49c9daf2b0ff 14 }
Jan Jongboom 20:49c9daf2b0ff 15
Jan Jongboom 20:49c9daf2b0ff 16 int read(unsigned char* buffer, int len, int timeout) {
Jan Jongboom 20:49c9daf2b0ff 17 return socket->recv(buffer, len);
Jan Jongboom 20:49c9daf2b0ff 18 }
Jan Jongboom 20:49c9daf2b0ff 19
Jan Jongboom 20:49c9daf2b0ff 20 int write(unsigned char* buffer, int len, int timeout) {
Jan Jongboom 20:49c9daf2b0ff 21 return socket->send(buffer, len);
Jan Jongboom 20:49c9daf2b0ff 22 }
Jan Jongboom 20:49c9daf2b0ff 23
Jan Jongboom 20:49c9daf2b0ff 24 int connect(const char* hostname, int port) {
Jan Jongboom 20:49c9daf2b0ff 25 socket->open(network);
Jan Jongboom 20:49c9daf2b0ff 26 return socket->connect(hostname, port);
Jan Jongboom 20:49c9daf2b0ff 27 }
Jan Jongboom 20:49c9daf2b0ff 28
Jan Jongboom 20:49c9daf2b0ff 29 void disconnect() {
Jan Jongboom 20:49c9daf2b0ff 30
Jan Jongboom 20:49c9daf2b0ff 31 }
Jan Jongboom 20:49c9daf2b0ff 32
Jan Jongboom 20:49c9daf2b0ff 33private:
Jan Jongboom 20:49c9daf2b0ff 34 NetworkInterface* network;
Jan Jongboom 20:49c9daf2b0ff 35 TCPSocket* socket;
Jan Jongboom 20:49c9daf2b0ff 36};
Jan Jongboom 20:49c9daf2b0ff 37
Jan Jongboom 20:49c9daf2b0ff 38#endif // _MQTTNETWORK_H_