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:

Linux-example/LinuxMQTT.h

Committer:
wolfSSL
Date:
2015-08-01
Revision:
20:0404c7f31c69
Parent:
8:a3e3113054a1

File content as of revision 20:0404c7f31c69:

#if !defined(LINUXMQTT_H)
#define LINUXMQTT_H

class Countdown
{
public:
    Countdown()
    { 
    
    }

    Countdown(int ms)
    { 
        countdown_ms(ms);
    }
    

    bool expired()
    {
        struct timeval now, res;
        gettimeofday(&now, NULL);
        timersub(&end_time, &now, &res);        
        //printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000);
        //if (res.tv_sec > 0 || res.tv_usec > 0)
        //  printf("expired %d %d\n", res.tv_sec, res.tv_usec);
        return res.tv_sec < 0 || (res.tv_sec == 0 && res.tv_usec <= 0);
    }
    

    void countdown_ms(int ms)  
    {
        struct timeval now;
        gettimeofday(&now, NULL);
        struct timeval interval = {ms / 1000, (ms % 1000) * 1000};
        //printf("interval %d %d\n", interval.tv_sec, interval.tv_usec);
        timeradd(&now, &interval, &end_time);
    }

    
    void countdown(int seconds)
    {
        struct timeval now;
        gettimeofday(&now, NULL);
        struct timeval interval = {seconds, 0};
        timeradd(&now, &interval, &end_time);
    }

    
    int left_ms()
    {
        struct timeval now, res;
        gettimeofday(&now, NULL);
        timersub(&end_time, &now, &res);
        //printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000);
        return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
    }
    
private:

    struct timeval end_time;
};


#endif