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
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
Diff: main.cpp
- Revision:
- 6:4a25f3b9caef
- Parent:
- 5:ded8fe5991a2
- Child:
- 8:f8a346582627
--- a/main.cpp Tue Sep 05 15:00:17 2017 +0000 +++ b/main.cpp Fri Sep 08 07:35:33 2017 +0000 @@ -12,34 +12,30 @@ // BC_NCE = 1 disables the battery charger DigitalOut bc_nce(PB_2); -DigitalOut ledMQTTYield(D5); - DigitalOut ledGPS(D8); +static bool enableODB = false; +static Ticker vehicleDataSamplingTicker; +static const int VEHICLE_DATA_SAMPLING_PERIOD_MS = 50; +static const int VEHICLE_DATA_REPORTING_PERIOD_MS = 3000; MTSSerial obd(D1, D0); DigitalOut ledOBD(D6); - -static const int VEHICLE_DATA_SAMPLING_PERIOD_MS = 50; -static Ticker vehicleDataSamplingTicker; static TripDataReader* pTripDataReader = NULL; static void vehicleDataSamplingCallback(); -static const int VEHICLE_DATA_REPORTING_PERIOD_MS = 3000; - -static bool exitCmd = false; - +DigitalOut vehicleDataLED(D5); static void sendVehicleData(const MTSCellularManager::GPSStatus& gpsStatus, const TripDataReader::TripData& tripData); -//static void test2Handler(MQTT::MessageData& data); +static bool exitCmd = false; int main() { // Disable the battery charger unless a battery is attached. bc_nce = 1; - ledMQTTYield = 1; + vehicleDataLED = 1; ledGPS = 1; - + // Change the baud rate of the debug port from the default 9600 to 115200. Serial debug(USBTX, USBRX); debug.baud(115200); @@ -49,7 +45,13 @@ mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); logInfo("Program started"); - + + logInfo("Vehicle ID: %s, TYPE: %s", VEHICLE_ID, VEHICLE_TYPE); + if (0 == strcmp(VEHICLE_TYPE, "CAR")) { + logInfo("Enable OBD for it is a car"); + enableODB = true; + } + logInfo("Initializing cellular"); MTSCellularManager cellularManager("wireless.twilio.com"); if (! cellularManager.init()) { @@ -67,7 +69,7 @@ TripDataReader tripDataReader(obd, ledOBD); pTripDataReader = &tripDataReader; - while (true) { + while (enableODB) { logInfo("Initializing OBD"); int r = tripDataReader.init(); logInfo("OBD Initialization result: %d", r); @@ -87,8 +89,11 @@ while (!exitCmd) { wait_ms(VEHICLE_DATA_REPORTING_PERIOD_MS); - TripDataReader::TripData tripData = tripDataReader.getTripData(); - tripDataReader.resetAverageWindow(); + TripDataReader::TripData tripData; + if (enableODB) { + tripData = tripDataReader.getTripData(); + tripDataReader.resetAverageWindow(); + } MTSCellularManager::GPSStatus gpsStatus = cellularManager.gpsPollStatus(); if (gpsStatus.success) { ledGPS = 0; @@ -115,7 +120,7 @@ static void sendVehicleData(const MTSCellularManager::GPSStatus& gpsStatus, const TripDataReader::TripData& tripData) { - ledMQTTYield = 0; + vehicleDataLED = 0; logInfo("Connecting MQTT Client"); TlsMQTTClient client = TlsMQTTClient(); @@ -130,20 +135,19 @@ logInfo("MQTT connected"); - sprintf(buf, "{" - "\"driver_id\": %s," - "\"runtime\": %d," - "\"miles\": %d," - "\"speed\": %f," - "\"minT\": %f," - "\"maxT\": %f," - "\"avgT\": %f," - "\"fuel\": %d," - "\"brake\": %d," - "\"lat\": %lf," - "\"lon\": %lf" - "}", - VEHICLE_ID, + if (enableODB) { + sprintf(buf, "{" + "\"runtime\": %d," + "\"miles\": %d," + "\"speed\": %f," + "\"minT\": %f," + "\"maxT\": %f," + "\"avgT\": %f," + "\"fuel\": %d," + "\"brake\": %d," + "\"lat\": %lf," + "\"lon\": %lf" + "}", tripData.runtime, tripData.distance, tripData.averageSpeed, @@ -154,6 +158,17 @@ tripData.hardBrakeCount, gpsStatus.latitudeVal, gpsStatus.longitudeVal); + } else { + sprintf(buf, "{" + "\"speed\": %f," + "\"lat\": %lf," + "\"lon\": %lf" + "}", + gpsStatus.speedVal, + gpsStatus.latitudeVal, + gpsStatus.longitudeVal); + } + message.qos = MQTT::QOS1; message.payload = (void*)buf; message.payloadlen = strlen(buf) + 1; @@ -166,7 +181,7 @@ } else { logError("MQTT connection failed %d", result); } - ledMQTTYield = 1; + vehicleDataLED = 1; } /*