Simple usage example of MQTTS library

Dependencies:   EthernetInterface MQTTS NTPClient SDFileSystem mbed-rtos mbed wolfSSL

Fork of HelloMQTT by MQTT

MQTT is light weight protocol for M2M, IoT. MQTTS adds TLS(SSL) security layer into the MQTT. This program shows simple usage example of MQTTS library.

Connect information has to be stored in SD file "connectInfo.txt", with following lines in it.

  • Host Name
  • User Name
  • Password
  • Client ID
  • Topic

The program asks following information on the terminal

  • Port Number to be connected. If the port is >8000 it assumes MQTTS.
  • Message to be published
  • Certificate file name, if MQTTS. Input file name on SD card. Simply Return key force it to no server verification. The program set up the realtime clock by NTP for certificate verification.

This program was tested with FRDM-K64F, against Sango MQTT server by Shiguredo. https://sango.shiguredo.jp/

References:

Committer:
wolfSSL
Date:
Sat Aug 01 20:50:27 2015 +0000
Revision:
20:0404c7f31c69
Parent:
8:a3e3113054a1
Added QoS2

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