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{
Jan Jongboom 20:49c9daf2b0ff 2 "config": {
Jan Jongboom 20:49c9daf2b0ff 3 "network-interface":{
Jan Jongboom 20:49c9daf2b0ff 4 "help": "options are ETHERNET,WIFI_ESP8266,MESH_LOWPAN_ND,MESH_THREAD",
Jan Jongboom 20:49c9daf2b0ff 5 "value": "ETHERNET"
Jan Jongboom 20:49c9daf2b0ff 6 },
Jan Jongboom 20:49c9daf2b0ff 7 "mesh_radio_type": {
Jan Jongboom 20:49c9daf2b0ff 8 "help": "options are ATMEL, MCR20",
Jan Jongboom 20:49c9daf2b0ff 9 "value": "ATMEL"
Jan Jongboom 20:49c9daf2b0ff 10 },
Jan Jongboom 20:49c9daf2b0ff 11 "esp8266-tx": {
Jan Jongboom 20:49c9daf2b0ff 12 "help": "Pin used as TX (connects to ESP8266 RX)",
Jan Jongboom 20:49c9daf2b0ff 13 "value": "D1"
Jan Jongboom 20:49c9daf2b0ff 14 },
Jan Jongboom 20:49c9daf2b0ff 15 "esp8266-rx": {
Jan Jongboom 20:49c9daf2b0ff 16 "help": "Pin used as RX (connects to ESP8266 TX)",
Jan Jongboom 20:49c9daf2b0ff 17 "value": "D0"
Jan Jongboom 20:49c9daf2b0ff 18 },
Jan Jongboom 20:49c9daf2b0ff 19 "esp8266-ssid": {
Jan Jongboom 20:49c9daf2b0ff 20 "value": "\"SSID\""
Jan Jongboom 20:49c9daf2b0ff 21 },
Jan Jongboom 20:49c9daf2b0ff 22 "esp8266-password": {
Jan Jongboom 20:49c9daf2b0ff 23 "value": "\"Password\""
Jan Jongboom 20:49c9daf2b0ff 24 },
Jan Jongboom 20:49c9daf2b0ff 25 "esp8266-debug": {
Jan Jongboom 20:49c9daf2b0ff 26 "value": true
Jan Jongboom 20:49c9daf2b0ff 27 },
Jan Jongboom 20:49c9daf2b0ff 28 "lcd-mosi": {
Jan Jongboom 20:49c9daf2b0ff 29 "value": "D11",
Jan Jongboom 20:49c9daf2b0ff 30 "macro_name": "LCD_MOSI"
Jan Jongboom 20:49c9daf2b0ff 31 },
Jan Jongboom 20:49c9daf2b0ff 32 "lcd-sck": {
Jan Jongboom 20:49c9daf2b0ff 33 "value": "D13",
Jan Jongboom 20:49c9daf2b0ff 34 "macro_name": "LCD_SCK"
Jan Jongboom 20:49c9daf2b0ff 35 },
Jan Jongboom 20:49c9daf2b0ff 36 "lcd-miso": {
Jan Jongboom 20:49c9daf2b0ff 37 "value": "D12",
Jan Jongboom 20:49c9daf2b0ff 38 "macro_name": "LCD_MISO"
Jan Jongboom 20:49c9daf2b0ff 39 },
Jan Jongboom 20:49c9daf2b0ff 40 "lcd-a0": {
Jan Jongboom 20:49c9daf2b0ff 41 "value": "D7",
Jan Jongboom 20:49c9daf2b0ff 42 "macro_name": "LCD_A0"
Jan Jongboom 20:49c9daf2b0ff 43 },
Jan Jongboom 20:49c9daf2b0ff 44 "lcd-ncs": {
Jan Jongboom 20:49c9daf2b0ff 45 "value": "D10",
Jan Jongboom 20:49c9daf2b0ff 46 "macro_name": "LCD_NCS"
Jan Jongboom 20:49c9daf2b0ff 47 }
Jan Jongboom 20:49c9daf2b0ff 48 },
Jan Jongboom 20:49c9daf2b0ff 49 "target_overrides": {
Jan Jongboom 20:49c9daf2b0ff 50 "*": {
Jan Jongboom 20:49c9daf2b0ff 51 "target.features_add": ["NANOSTACK", "LOWPAN_ROUTER", "COMMON_PAL"],
Jan Jongboom 20:49c9daf2b0ff 52 "mbed-mesh-api.6lowpan-nd-channel-page": 0,
Jan Jongboom 20:49c9daf2b0ff 53 "mbed-mesh-api.6lowpan-nd-channel": 12,
Jan Jongboom 20:49c9daf2b0ff 54 "mbed-trace.enable": 0
Jan Jongboom 20:49c9daf2b0ff 55 },
Jan Jongboom 20:49c9daf2b0ff 56 "HEXIWEAR": {
Jan Jongboom 20:49c9daf2b0ff 57 "esp8266-tx": "PTD3",
Jan Jongboom 20:49c9daf2b0ff 58 "esp8266-rx": "PTD2"
Jan Jongboom 20:49c9daf2b0ff 59 },
Jan Jongboom 20:49c9daf2b0ff 60 "NUCLEO_F401RE": {
Jan Jongboom 20:49c9daf2b0ff 61 "esp8266-tx": "D8",
Jan Jongboom 20:49c9daf2b0ff 62 "esp8266-rx": "D2"
Jan Jongboom 20:49c9daf2b0ff 63 },
Jan Jongboom 20:49c9daf2b0ff 64 "NUCLEO_F411RE": {
Jan Jongboom 20:49c9daf2b0ff 65 "esp8266-tx": "D8",
Jan Jongboom 20:49c9daf2b0ff 66 "esp8266-rx": "D2"
Jan Jongboom 20:49c9daf2b0ff 67 }
Jan Jongboom 20:49c9daf2b0ff 68 }
Jan Jongboom 20:49c9daf2b0ff 69}