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:
1:5a896191c3c4
Parent:
0:b32fa0c757d7
Child:
2:d4dcf1ebaa99
--- a/main.cpp	Tue May 09 13:16:48 2017 +0000
+++ b/main.cpp	Fri May 12 11:49:58 2017 +0000
@@ -9,14 +9,22 @@
 // BC_NCE = 0 enables the battery charger
 // BC_NCE = 1 disables the battery charger
 DigitalOut bc_nce(PB_2);
+DigitalOut ledMQTTYield(D5);
+DigitalOut ledGPS(D8);
+
+static const int VEHICLE_DATA_POLLING_PERIOD_MS = 5000;
 
 static bool exitCmd = false;
 
+static void sendVehicleData(MTSCellularManager::GPSStatus& gpsStatus);
 static void test2Handler(MQTT::MessageData& data);
 
 int main() {
     // Disable the battery charger unless a battery is attached.
     bc_nce = 1;
+
+    ledMQTTYield = 1;
+    ledGPS = 1;
     
     // Change the baud rate of the debug port from the default 9600 to 115200.
     Serial debug(USBTX, USBRX);
@@ -26,9 +34,9 @@
     //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
 
-    logDebug("Program started");
-
-    logDebug("Initializing cellular");
+    logInfo("Program started");
+        
+    logInfo("Initializing cellular");
     MTSCellularManager cellularManager("wireless.twilio.com");   
     if (! cellularManager.init()) {
         while (true) {
@@ -37,13 +45,43 @@
         }
     }
 
-    logDebug("Initializing CyaSSL");
+    {
+        logInfo("Initializing GPS");
+        cellularManager.enableGps();
+        logInfo("GPS Initialized");
+    }
+
+    logInfo("Initializing CyaSSL");
     CyaSSL_Init();
 
-    logDebug("Connecting MQTT Client");
+    while (!exitCmd) {
+        MTSCellularManager::GPSStatus gpsStatus = cellularManager.gpsPollStatus();
+        if (gpsStatus.success) {
+            ledGPS = 0;
+            sendVehicleData(gpsStatus);
+        } else {
+            ledGPS = 1;
+        }
+        wait_ms(VEHICLE_DATA_POLLING_PERIOD_MS);
+    }
+
+    logInfo("Cleaning up CyaSSL");
+    CyaSSL_Cleanup();
+
+    logInfo("Shutting down cellular");
+    cellularManager.uninit();
+
+    logInfo("Program finished");
+    wait(1E12);
+    return 0;
+}
+
+static void sendVehicleData(MTSCellularManager::GPSStatus& gpsStatus) {
+    ledMQTTYield = 0;
+
+    logInfo("Connecting MQTT Client");
     TlsMQTTClient client = TlsMQTTClient();
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
-    data.MQTTVersion = 3;
     // Account: AC79339963a99f476e6f0b3214abd48a9d
     // Fleet: FL1a84e19cd6014020b7a26a6f89521d4f
     // Deployment: DL1daae5d60c5941aea5cd8cf2b693e990
@@ -53,40 +91,44 @@
     data.username.cstring = "KY977b46cdd67645b494da001ced07e40f";
     data.password.cstring = "RteF2I/fwCjmmdIOoe32qw";
     if (MQTT::SUCCESS == client.connect("mqtt-sync.us1.twilio.com", 8883, NULL /*MQTT_GATEWAY_PROD_ROOT_CA_PEM*/, data)) {
-        int rc;
         MQTT::Message message;
         char buf[512];
 
-        logDebug("MQTT connected");
+        logInfo("MQTT connected");
 
-        // QoS 1 list item
-        sprintf(buf, "{\"test\": 42}");
+        sprintf(buf, "{"
+            "\"driver_id\": 1,"
+            "\"runtime\": 10,"
+            "\"miles\": 10,"
+            "\"speed\": %f,"
+            "\"minT\": 40,"
+            "\"maxT\": 60,"
+            "\"avgT\": 50,"
+            "\"fuel\": 50,"
+            "\"brake\": 0,"
+            "\"lat\": %lf,"
+            "\"lon\": %lf"
+            "}",
+                gpsStatus.speedVal,
+                gpsStatus.latitudeVal,
+                gpsStatus.longitudeVal);
         message.qos = MQTT::QOS1;
         message.payload = (void*)buf;
         message.payloadlen = strlen(buf) + 1;
-        rc = client.publish("sync/lists/test", message);
-        logDebug("MQTT message publish result: %d", rc);
-
-        rc = client.subscribe("sync/lists/test2", MQTT::QOS1, test2Handler);
-        logDebug("MQTT subscription result: %d", rc);
+        logInfo("MQTT message publishing buf: %s", buf);
+        int rc = client.publish("sync/lists/vehicle0-data", message);
+        logInfo("MQTT message publish result: %d", rc);
 
-        while (!exitCmd) {
-            client.yield(); // keep alive aconnection
-        }
-
-        logDebug("MQTT disconnecting");
+        logInfo("MQTT disconnecting");
         client.disconnect();
     }
-
-    logDebug("Cleaning up CyaSSL");
-    CyaSSL_Cleanup();
+    ledMQTTYield = 1;
+}
 
-    logDebug("Shutting down cellular");
-    cellularManager.uninit();
-
-    logDebug("Program finished");
-    wait(1E12);
-    return 0;
+/*
+static void subscribeToTest2() {
+    rc = client.subscribe("sync/lists/test2", MQTT::QOS1, test2Handler);
+    logInfo("MQTT subscription result: %d", rc);
 }
 
 static void test2Handler(MQTT::MessageData& data) {
@@ -115,4 +157,4 @@
     if (0 == strncmp((char*)data.message.payload, "exit", data.message.payloadlen)) {
         exitCmd = true;
     }
-}
+}*/