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

MTSCellularManager.cpp

Committer:
miaotwilio
Date:
2017-08-17
Revision:
3:0a48c984e15b
Parent:
1:5a896191c3c4
Child:
4:f4e8f0fca631

File content as of revision 3:0a48c984e15b:

#include "MTSCellularManager.hpp"

MTSCellularManager::MTSCellularManager(const char* apn_) : apn(apn_), io(NULL), radio(NULL) {
}

MTSCellularManager::~MTSCellularManager() {
    delete radio;
    delete io;
}

bool MTSCellularManager::init() {
    logInfo("initializing cellular radio");

    io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS);
    // radio default baud rate is 115200
    io->baud(115200);
    if (! io)
        return false;
    
    radio = mts::CellularFactory::create(io);
    if (! radio)
        return false;
    
    // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created
    Transport::setTransport(radio);

    logInfo("setting APN");
    if (radio->setApn(apn) != MTS_SUCCESS) {
        logError("failed to set APN to \"%s\"", apn);
        return false;
    }

    logInfo("bringing up the link");
    if (! radio->connect()) {
        logError("failed to bring up the link");
        return false;
    } else {
        return true;
    }
}

void MTSCellularManager::uninit() {
    logInfo("finished - bringing down link");
    radio->disconnect();
    delete radio;
    delete io;
}

void MTSCellularManager::enableGps() {

    if( !radio->GPSenabled() ) {
        logInfo("GPS: enabling");
        radio->GPSenable();
        while( !radio->GPSenabled() ) {
            logInfo("...");
            wait(5);
        }
        logInfo("GPS enabled");
    }
}

MTSCellularManager::GPSStatus MTSCellularManager::gpsPollStatus() {
    GPSStatus status;

    int i;
    char c;
    
    if( !radio->GPSenabled() ) {
        logInfo("GPS: enabling");
        radio->GPSenable();
        while( !radio->GPSenabled() ) {
            logInfo("...");
            wait(5);
        }
    }


    logInfo("GPS polling");
    mts::Cellular::gpsData gpsValues = radio->GPSgetPosition();

    // mocked data
    /*
    gpsValues.success = true;
    gpsValues.fix = 3;
    gpsValues.satellites = 4;
    gpsValues.timestamp = "17/05/12, 08:59:56.483";
    gpsValues.latitude = "5925.4477N";
    gpsValues.longitude = "02444.9046E";
    */

    if (gpsValues.success && gpsValues.fix > 2) {
        logInfo("GPS polled: success %d, sats: %d, fix: %d, ts %s, lat %s, lng %s",
            gpsValues.success, gpsValues.satellites, gpsValues.fix, gpsValues.timestamp.c_str(), gpsValues.latitude.c_str(), gpsValues.longitude.c_str());
        status.success = gpsValues.success;
        status.fix = gpsValues.fix;
        status.speedVal = gpsValues.kmhr / 0.621371f;
        status.latitudeVal = .0;
        status.longitudeVal = .0;
        sscanf(gpsValues.latitude.substr(0, gpsValues.latitude.size()-1).c_str(), "%02d%lf", &i, &status.latitudeVal);
        c = gpsValues.latitude.c_str()[gpsValues.latitude.size()-1];
        //logInfo("i=%d,lat=%f,c=%c", i, latitudeVal ,c);
        status.latitudeVal /= 60.0;
        status.latitudeVal += i;
        if(c == 'S')
            status.latitudeVal = 0.0 - status.latitudeVal;
        sscanf(gpsValues.longitude.substr(0, gpsValues.longitude.size()-1).c_str(), "%03d%lf", &i, &status.longitudeVal);
        c = gpsValues.longitude.c_str()[gpsValues.longitude.size()-1];
        //logInfo("i=%d,lon=%f,c=%c", i, status.longitudeVal, c);
        status.longitudeVal /= 60.0;
        status.longitudeVal += i;
        if(c == 'W')
            status.longitudeVal = 0.0 - status.longitudeVal;
    } else {
        status.success = false;
        logInfo("GPS polled failed: success %d, sats: %d, fix: %d",
            gpsValues.success, gpsValues.satellites, gpsValues.fix);
    }

    return status;
}