![](/media/cache/group/mqtt.png.50x50_q85.png)
Sample MQTT program - simple send and receive
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 NetworkInterface APIs in mbed OS 5 to show how this works. The MQTT library contains an MQTTNetwork.h header, which is a wrapper around the mbed networking interface. To switch between connectivity methods (the default is Ethernet) the easy-connect library is provided in this example application. You can change the connectivity method in mbed_app.json
.
Adding new connectivity methods to the program is trivial, as long as they implement the mbed OS 5 NetworkStack
API.
Diff: main.cpp
- Revision:
- 19:7f7aba7a4a8b
- Parent:
- 18:07a79d8f01c3
- Child:
- 20:49c9daf2b0ff
--- a/main.cpp Mon Jul 27 09:30:31 2015 +0000 +++ b/main.cpp Thu Jul 30 13:49:18 2015 +0000 @@ -88,7 +88,7 @@ if ((rc = client.connect(data)) != 0) printf("rc from MQTT connect is %d\n", rc); - if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0) + if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0) printf("rc from MQTT subscribe is %d\n", rc); MQTT::Message message; @@ -122,16 +122,10 @@ client.yield(100); if ((rc = client.unsubscribe(topic)) != 0) - { - lcd.cls(); printf("rc from unsubscribe was %d\n", rc); - } if ((rc = client.disconnect()) != 0) - { - lcd.cls(); printf("rc from disconnect was %d\n", rc); - } ipstack.disconnect();