Connecting a Multi-Tech Systems Dragonfly™ to Twilio's Sync for IoT Quickstart. Blink a dev board LED.

Dependencies:   MQTT MbedJSONValue mbed mtsas

Fork of DragonflyMQTT by miao zhicheng

Code to connect a Multi-Tech® MultiConnect® Dragonfly™ to Twilio's Sync for IoT: https://www.twilio.com/docs/api/devices

Uses MQTT over TLS and subscribes to a topic where you can control an LED. See also our Quickstart using this code, here: https://www.twilio.com/docs/quickstart/sync-iot/mqtt-multi-tech-multiconnect-dragonfly-sync-iot

Revision:
9:2d119fbe7482
Parent:
2:d4dcf1ebaa99
--- a/TlsMQTTClient.hpp	Thu Sep 14 08:14:18 2017 +0000
+++ b/TlsMQTTClient.hpp	Fri Sep 15 22:41:22 2017 +0000
@@ -1,5 +1,4 @@
-#ifndef TLS_MQTT_CLIENT_HPTT
-#define TLS_MQTT_CLIENT_HPTT
+#pragma once
 
 #include <ssl.h>
 #include <mbed.h>
@@ -14,7 +13,11 @@
     ~TlsMQTTClient();
 
 private:
-    typedef MQTT::Client<TlsMQTTClient, Countdown, 1000 /* MAX_MQTT_PACKET_SIZE */> MQTTClient;
+    typedef MQTT::Client<
+        TlsMQTTClient, 
+        Countdown, 
+        1024 /* MAX_MQTT_PACKET_SIZE */
+    > MQTTClient;
 
     // MQTT Operations
 public:
@@ -26,16 +29,22 @@
      *  @param options - connect options
      *  @return success code -
      */
-    int connect(const char* host, const int port,
+    int connect(
+        const char* host, 
+        const int port,
         const char* certificates,
-        MQTTPacket_connectData& options);
+        MQTTPacket_connectData& options
+    );
 
     /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
      *  @param topic - the topic to publish to
      *  @param message - the message to send
      *  @return success code -
      */
-    int publish(const char* topicName, MQTT::Message& message);
+    int publish(
+        const char* topicName, 
+        MQTT::Message& message
+    );
    
     /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
      *  @param topic - the topic to publish to
@@ -45,7 +54,13 @@
      *  @param retained - whether the message should be retained
      *  @return success code -
      */
-    int publish(const char* topicName, void* payload, size_t payloadlen, enum MQTT::QoS qos = MQTT::QOS0, bool retained = false);
+    int publish(
+        const char* topicName, 
+        void* payload, 
+        size_t payloadlen, 
+        enum MQTT::QoS qos = MQTT::QOS0, 
+        bool retained = false
+    );
   
     /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
      *  @param topic - the topic to publish to
@@ -56,7 +71,14 @@
      *  @param retained - whether the message should be retained
      *  @return success code -
      */
-    int publish(const char* topicName, void* payload, size_t payloadlen, unsigned short& id, enum MQTT::QoS qos = MQTT::QOS1, bool retained = false);
+    int publish(
+        const char* topicName, 
+        void* payload, 
+        size_t payloadlen, 
+        unsigned short& id, 
+        enum MQTT::QoS qos = MQTT::QOS1, 
+        bool retained = false
+    );
 
     typedef void (*MessageHandler)(MQTT::MessageData&);
 
@@ -66,7 +88,11 @@
      *  @param mh - the callback function to be invoked when a message is received for this subscription
      *  @return success code -
      */
-    int subscribe(const char* topicFilter, enum MQTT::QoS qos, MessageHandler mh);
+    int subscribe(
+        const char* topicFilter, 
+        enum MQTT::QoS qos, 
+        MessageHandler mh
+    );
 
     /** MQTT Unsubscribe - send an MQTT unsubscribe packet and wait for the unsuback
      *  @param topicFilter - a topic pattern which can include wildcards
@@ -93,7 +119,6 @@
     bool isConnected();
 
     // Network interface for MQTT::Client
-public:
     int read(unsigned char* data, int max, int timeout = -1);
     int write(const unsigned char* data, int length, int timeout = -1);
 
@@ -101,12 +126,8 @@
     void cleanupTransport();
     static int ioRecv(CYASSL* ssl, char *buf, int sz, void *ctx);
     static int ioSend(CYASSL* ssl, char *buf, int sz, void *ctx);
-
-private:
     TCPSocketConnection* tcp;
     CYASSL_CTX* ctx;
     CYASSL *ssl;
     MQTTClient* mqttClient;
 };
-
-#endif