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

main.cpp

Committer:
miaotwilio
Date:
2017-05-09
Revision:
0:b32fa0c757d7
Child:
1:5a896191c3c4

File content as of revision 0:b32fa0c757d7:

#include "MTSCellularManager.hpp"
#include "TlsMQTTClient.hpp"
#include "Certificates.h"
#include <mbed.h>
#include <mtsas.h>
#include <ssl.h>

// This line controls the regulator's battery charger.
// BC_NCE = 0 enables the battery charger
// BC_NCE = 1 disables the battery charger
DigitalOut bc_nce(PB_2);

static bool exitCmd = false;

static void test2Handler(MQTT::MessageData& data);

int main() {
    // Disable the battery charger unless a battery is attached.
    bc_nce = 1;
    
    // Change the baud rate of the debug port from the default 9600 to 115200.
    Serial debug(USBTX, USBRX);
    debug.baud(115200);
    
    //Sets the log level to INFO, higher log levels produce more log output.
    //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
    mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);

    logDebug("Program started");

    logDebug("Initializing cellular");
    MTSCellularManager cellularManager("wireless.twilio.com");   
    if (! cellularManager.init()) {
        while (true) {
            logError("failed to initialize cellular radio");
            wait(1);
        }
    }

    logDebug("Initializing CyaSSL");
    CyaSSL_Init();

    logDebug("Connecting MQTT Client");
    TlsMQTTClient client = TlsMQTTClient();
    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    data.MQTTVersion = 3;
    // Account: AC79339963a99f476e6f0b3214abd48a9d
    // Fleet: FL1a84e19cd6014020b7a26a6f89521d4f
    // Deployment: DL1daae5d60c5941aea5cd8cf2b693e990
    //    Sync List: https://preview.twilio.com/Sync/Services/IS1a84e19cd6014020b7a26a6f89521d4f/Lists/
    // Device: TH6008344511ed4286b249c3d21891b5ae
    data.clientID.cstring = "dragonfly-mqtt";
    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");

        // QoS 1 list item
        sprintf(buf, "{\"test\": 42}");
        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);

        while (!exitCmd) {
            client.yield(); // keep alive aconnection
        }

        logDebug("MQTT disconnecting");
        client.disconnect();
    }

    logDebug("Cleaning up CyaSSL");
    CyaSSL_Cleanup();

    logDebug("Shutting down cellular");
    cellularManager.uninit();

    logDebug("Program finished");
    wait(1E12);
    return 0;
}

static void test2Handler(MQTT::MessageData& data) {
    static const size_t MAX_DISPLAY_MESSAGE_SIZE = 30;
    char buf[MAX_DISPLAY_MESSAGE_SIZE + 1];
    if (data.message.payloadlen <= MAX_DISPLAY_MESSAGE_SIZE) {
        strncpy(buf, (char*)data.message.payload, data.message.payloadlen);
        buf[data.message.payloadlen] = '\0';
    } else {
        strncpy(buf, (char*)data.message.payload, MAX_DISPLAY_MESSAGE_SIZE - 3);
        buf[MAX_DISPLAY_MESSAGE_SIZE-3] = '.';
        buf[MAX_DISPLAY_MESSAGE_SIZE-2] = '.';
        buf[MAX_DISPLAY_MESSAGE_SIZE-1] = '.';
        buf[MAX_DISPLAY_MESSAGE_SIZE] = '\0';
    }
    logDebug("topic %s payload received len %d data %s", data.topicName.lenstring.data, data.message.payloadlen, buf);

    // sync client can send binary data using payload format:
    // {
    //    "payload": "ZXhpdA==", # base64 encoded "exit"
    //    "_iot_meta": {
    //        "payload_encoding": "base64",
    //        "payload_type": "application/octet-stream",
    //    }
    // }
    if (0 == strncmp((char*)data.message.payload, "exit", data.message.payloadlen)) {
        exitCmd = true;
    }
}