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 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.

Committer:
icraggs
Date:
Tue Jan 16 13:41:29 2018 +0000
Revision:
23:e38aaf532823
Parent:
8:a3e3113054a1
Update the MQTT library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 8:a3e3113054a1 1 #if !defined(LINUXMQTT_H)
icraggs 8:a3e3113054a1 2 #define LINUXMQTT_H
icraggs 8:a3e3113054a1 3
icraggs 8:a3e3113054a1 4 class Countdown
icraggs 8:a3e3113054a1 5 {
icraggs 8:a3e3113054a1 6 public:
icraggs 8:a3e3113054a1 7 Countdown()
icraggs 8:a3e3113054a1 8 {
icraggs 8:a3e3113054a1 9
icraggs 8:a3e3113054a1 10 }
icraggs 8:a3e3113054a1 11
icraggs 8:a3e3113054a1 12 Countdown(int ms)
icraggs 8:a3e3113054a1 13 {
icraggs 8:a3e3113054a1 14 countdown_ms(ms);
icraggs 8:a3e3113054a1 15 }
icraggs 8:a3e3113054a1 16
icraggs 8:a3e3113054a1 17
icraggs 8:a3e3113054a1 18 bool expired()
icraggs 8:a3e3113054a1 19 {
icraggs 8:a3e3113054a1 20 struct timeval now, res;
icraggs 8:a3e3113054a1 21 gettimeofday(&now, NULL);
icraggs 8:a3e3113054a1 22 timersub(&end_time, &now, &res);
icraggs 8:a3e3113054a1 23 //printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000);
icraggs 8:a3e3113054a1 24 //if (res.tv_sec > 0 || res.tv_usec > 0)
icraggs 8:a3e3113054a1 25 // printf("expired %d %d\n", res.tv_sec, res.tv_usec);
icraggs 8:a3e3113054a1 26 return res.tv_sec < 0 || (res.tv_sec == 0 && res.tv_usec <= 0);
icraggs 8:a3e3113054a1 27 }
icraggs 8:a3e3113054a1 28
icraggs 8:a3e3113054a1 29
icraggs 8:a3e3113054a1 30 void countdown_ms(int ms)
icraggs 8:a3e3113054a1 31 {
icraggs 8:a3e3113054a1 32 struct timeval now;
icraggs 8:a3e3113054a1 33 gettimeofday(&now, NULL);
icraggs 8:a3e3113054a1 34 struct timeval interval = {ms / 1000, (ms % 1000) * 1000};
icraggs 8:a3e3113054a1 35 //printf("interval %d %d\n", interval.tv_sec, interval.tv_usec);
icraggs 8:a3e3113054a1 36 timeradd(&now, &interval, &end_time);
icraggs 8:a3e3113054a1 37 }
icraggs 8:a3e3113054a1 38
icraggs 8:a3e3113054a1 39
icraggs 8:a3e3113054a1 40 void countdown(int seconds)
icraggs 8:a3e3113054a1 41 {
icraggs 8:a3e3113054a1 42 struct timeval now;
icraggs 8:a3e3113054a1 43 gettimeofday(&now, NULL);
icraggs 8:a3e3113054a1 44 struct timeval interval = {seconds, 0};
icraggs 8:a3e3113054a1 45 timeradd(&now, &interval, &end_time);
icraggs 8:a3e3113054a1 46 }
icraggs 8:a3e3113054a1 47
icraggs 8:a3e3113054a1 48
icraggs 8:a3e3113054a1 49 int left_ms()
icraggs 8:a3e3113054a1 50 {
icraggs 8:a3e3113054a1 51 struct timeval now, res;
icraggs 8:a3e3113054a1 52 gettimeofday(&now, NULL);
icraggs 8:a3e3113054a1 53 timersub(&end_time, &now, &res);
icraggs 8:a3e3113054a1 54 //printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000);
icraggs 8:a3e3113054a1 55 return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
icraggs 8:a3e3113054a1 56 }
icraggs 8:a3e3113054a1 57
icraggs 8:a3e3113054a1 58 private:
icraggs 8:a3e3113054a1 59
icraggs 8:a3e3113054a1 60 struct timeval end_time;
icraggs 8:a3e3113054a1 61 };
icraggs 8:a3e3113054a1 62
icraggs 8:a3e3113054a1 63
icraggs 8:a3e3113054a1 64 #endif