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
main.cpp@1:5a896191c3c4, 2017-05-12 (annotated)
- Committer:
- miaotwilio
- Date:
- Fri May 12 11:49:58 2017 +0000
- Revision:
- 1:5a896191c3c4
- Parent:
- 0:b32fa0c757d7
- Child:
- 2:d4dcf1ebaa99
integrated with UI
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
miaotwilio | 0:b32fa0c757d7 | 1 | #include "MTSCellularManager.hpp" |
miaotwilio | 0:b32fa0c757d7 | 2 | #include "TlsMQTTClient.hpp" |
miaotwilio | 0:b32fa0c757d7 | 3 | #include "Certificates.h" |
miaotwilio | 0:b32fa0c757d7 | 4 | #include <mbed.h> |
miaotwilio | 0:b32fa0c757d7 | 5 | #include <mtsas.h> |
miaotwilio | 0:b32fa0c757d7 | 6 | #include <ssl.h> |
miaotwilio | 0:b32fa0c757d7 | 7 | |
miaotwilio | 0:b32fa0c757d7 | 8 | // This line controls the regulator's battery charger. |
miaotwilio | 0:b32fa0c757d7 | 9 | // BC_NCE = 0 enables the battery charger |
miaotwilio | 0:b32fa0c757d7 | 10 | // BC_NCE = 1 disables the battery charger |
miaotwilio | 0:b32fa0c757d7 | 11 | DigitalOut bc_nce(PB_2); |
miaotwilio | 1:5a896191c3c4 | 12 | DigitalOut ledMQTTYield(D5); |
miaotwilio | 1:5a896191c3c4 | 13 | DigitalOut ledGPS(D8); |
miaotwilio | 1:5a896191c3c4 | 14 | |
miaotwilio | 1:5a896191c3c4 | 15 | static const int VEHICLE_DATA_POLLING_PERIOD_MS = 5000; |
miaotwilio | 0:b32fa0c757d7 | 16 | |
miaotwilio | 0:b32fa0c757d7 | 17 | static bool exitCmd = false; |
miaotwilio | 0:b32fa0c757d7 | 18 | |
miaotwilio | 1:5a896191c3c4 | 19 | static void sendVehicleData(MTSCellularManager::GPSStatus& gpsStatus); |
miaotwilio | 0:b32fa0c757d7 | 20 | static void test2Handler(MQTT::MessageData& data); |
miaotwilio | 0:b32fa0c757d7 | 21 | |
miaotwilio | 0:b32fa0c757d7 | 22 | int main() { |
miaotwilio | 0:b32fa0c757d7 | 23 | // Disable the battery charger unless a battery is attached. |
miaotwilio | 0:b32fa0c757d7 | 24 | bc_nce = 1; |
miaotwilio | 1:5a896191c3c4 | 25 | |
miaotwilio | 1:5a896191c3c4 | 26 | ledMQTTYield = 1; |
miaotwilio | 1:5a896191c3c4 | 27 | ledGPS = 1; |
miaotwilio | 0:b32fa0c757d7 | 28 | |
miaotwilio | 0:b32fa0c757d7 | 29 | // Change the baud rate of the debug port from the default 9600 to 115200. |
miaotwilio | 0:b32fa0c757d7 | 30 | Serial debug(USBTX, USBRX); |
miaotwilio | 0:b32fa0c757d7 | 31 | debug.baud(115200); |
miaotwilio | 0:b32fa0c757d7 | 32 | |
miaotwilio | 0:b32fa0c757d7 | 33 | //Sets the log level to INFO, higher log levels produce more log output. |
miaotwilio | 0:b32fa0c757d7 | 34 | //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE |
miaotwilio | 0:b32fa0c757d7 | 35 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
miaotwilio | 0:b32fa0c757d7 | 36 | |
miaotwilio | 1:5a896191c3c4 | 37 | logInfo("Program started"); |
miaotwilio | 1:5a896191c3c4 | 38 | |
miaotwilio | 1:5a896191c3c4 | 39 | logInfo("Initializing cellular"); |
miaotwilio | 0:b32fa0c757d7 | 40 | MTSCellularManager cellularManager("wireless.twilio.com"); |
miaotwilio | 0:b32fa0c757d7 | 41 | if (! cellularManager.init()) { |
miaotwilio | 0:b32fa0c757d7 | 42 | while (true) { |
miaotwilio | 0:b32fa0c757d7 | 43 | logError("failed to initialize cellular radio"); |
miaotwilio | 0:b32fa0c757d7 | 44 | wait(1); |
miaotwilio | 0:b32fa0c757d7 | 45 | } |
miaotwilio | 0:b32fa0c757d7 | 46 | } |
miaotwilio | 0:b32fa0c757d7 | 47 | |
miaotwilio | 1:5a896191c3c4 | 48 | { |
miaotwilio | 1:5a896191c3c4 | 49 | logInfo("Initializing GPS"); |
miaotwilio | 1:5a896191c3c4 | 50 | cellularManager.enableGps(); |
miaotwilio | 1:5a896191c3c4 | 51 | logInfo("GPS Initialized"); |
miaotwilio | 1:5a896191c3c4 | 52 | } |
miaotwilio | 1:5a896191c3c4 | 53 | |
miaotwilio | 1:5a896191c3c4 | 54 | logInfo("Initializing CyaSSL"); |
miaotwilio | 0:b32fa0c757d7 | 55 | CyaSSL_Init(); |
miaotwilio | 0:b32fa0c757d7 | 56 | |
miaotwilio | 1:5a896191c3c4 | 57 | while (!exitCmd) { |
miaotwilio | 1:5a896191c3c4 | 58 | MTSCellularManager::GPSStatus gpsStatus = cellularManager.gpsPollStatus(); |
miaotwilio | 1:5a896191c3c4 | 59 | if (gpsStatus.success) { |
miaotwilio | 1:5a896191c3c4 | 60 | ledGPS = 0; |
miaotwilio | 1:5a896191c3c4 | 61 | sendVehicleData(gpsStatus); |
miaotwilio | 1:5a896191c3c4 | 62 | } else { |
miaotwilio | 1:5a896191c3c4 | 63 | ledGPS = 1; |
miaotwilio | 1:5a896191c3c4 | 64 | } |
miaotwilio | 1:5a896191c3c4 | 65 | wait_ms(VEHICLE_DATA_POLLING_PERIOD_MS); |
miaotwilio | 1:5a896191c3c4 | 66 | } |
miaotwilio | 1:5a896191c3c4 | 67 | |
miaotwilio | 1:5a896191c3c4 | 68 | logInfo("Cleaning up CyaSSL"); |
miaotwilio | 1:5a896191c3c4 | 69 | CyaSSL_Cleanup(); |
miaotwilio | 1:5a896191c3c4 | 70 | |
miaotwilio | 1:5a896191c3c4 | 71 | logInfo("Shutting down cellular"); |
miaotwilio | 1:5a896191c3c4 | 72 | cellularManager.uninit(); |
miaotwilio | 1:5a896191c3c4 | 73 | |
miaotwilio | 1:5a896191c3c4 | 74 | logInfo("Program finished"); |
miaotwilio | 1:5a896191c3c4 | 75 | wait(1E12); |
miaotwilio | 1:5a896191c3c4 | 76 | return 0; |
miaotwilio | 1:5a896191c3c4 | 77 | } |
miaotwilio | 1:5a896191c3c4 | 78 | |
miaotwilio | 1:5a896191c3c4 | 79 | static void sendVehicleData(MTSCellularManager::GPSStatus& gpsStatus) { |
miaotwilio | 1:5a896191c3c4 | 80 | ledMQTTYield = 0; |
miaotwilio | 1:5a896191c3c4 | 81 | |
miaotwilio | 1:5a896191c3c4 | 82 | logInfo("Connecting MQTT Client"); |
miaotwilio | 0:b32fa0c757d7 | 83 | TlsMQTTClient client = TlsMQTTClient(); |
miaotwilio | 0:b32fa0c757d7 | 84 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
miaotwilio | 0:b32fa0c757d7 | 85 | // Account: AC79339963a99f476e6f0b3214abd48a9d |
miaotwilio | 0:b32fa0c757d7 | 86 | // Fleet: FL1a84e19cd6014020b7a26a6f89521d4f |
miaotwilio | 0:b32fa0c757d7 | 87 | // Deployment: DL1daae5d60c5941aea5cd8cf2b693e990 |
miaotwilio | 0:b32fa0c757d7 | 88 | // Sync List: https://preview.twilio.com/Sync/Services/IS1a84e19cd6014020b7a26a6f89521d4f/Lists/ |
miaotwilio | 0:b32fa0c757d7 | 89 | // Device: TH6008344511ed4286b249c3d21891b5ae |
miaotwilio | 0:b32fa0c757d7 | 90 | data.clientID.cstring = "dragonfly-mqtt"; |
miaotwilio | 0:b32fa0c757d7 | 91 | data.username.cstring = "KY977b46cdd67645b494da001ced07e40f"; |
miaotwilio | 0:b32fa0c757d7 | 92 | data.password.cstring = "RteF2I/fwCjmmdIOoe32qw"; |
miaotwilio | 0:b32fa0c757d7 | 93 | if (MQTT::SUCCESS == client.connect("mqtt-sync.us1.twilio.com", 8883, NULL /*MQTT_GATEWAY_PROD_ROOT_CA_PEM*/, data)) { |
miaotwilio | 0:b32fa0c757d7 | 94 | MQTT::Message message; |
miaotwilio | 0:b32fa0c757d7 | 95 | char buf[512]; |
miaotwilio | 0:b32fa0c757d7 | 96 | |
miaotwilio | 1:5a896191c3c4 | 97 | logInfo("MQTT connected"); |
miaotwilio | 0:b32fa0c757d7 | 98 | |
miaotwilio | 1:5a896191c3c4 | 99 | sprintf(buf, "{" |
miaotwilio | 1:5a896191c3c4 | 100 | "\"driver_id\": 1," |
miaotwilio | 1:5a896191c3c4 | 101 | "\"runtime\": 10," |
miaotwilio | 1:5a896191c3c4 | 102 | "\"miles\": 10," |
miaotwilio | 1:5a896191c3c4 | 103 | "\"speed\": %f," |
miaotwilio | 1:5a896191c3c4 | 104 | "\"minT\": 40," |
miaotwilio | 1:5a896191c3c4 | 105 | "\"maxT\": 60," |
miaotwilio | 1:5a896191c3c4 | 106 | "\"avgT\": 50," |
miaotwilio | 1:5a896191c3c4 | 107 | "\"fuel\": 50," |
miaotwilio | 1:5a896191c3c4 | 108 | "\"brake\": 0," |
miaotwilio | 1:5a896191c3c4 | 109 | "\"lat\": %lf," |
miaotwilio | 1:5a896191c3c4 | 110 | "\"lon\": %lf" |
miaotwilio | 1:5a896191c3c4 | 111 | "}", |
miaotwilio | 1:5a896191c3c4 | 112 | gpsStatus.speedVal, |
miaotwilio | 1:5a896191c3c4 | 113 | gpsStatus.latitudeVal, |
miaotwilio | 1:5a896191c3c4 | 114 | gpsStatus.longitudeVal); |
miaotwilio | 0:b32fa0c757d7 | 115 | message.qos = MQTT::QOS1; |
miaotwilio | 0:b32fa0c757d7 | 116 | message.payload = (void*)buf; |
miaotwilio | 0:b32fa0c757d7 | 117 | message.payloadlen = strlen(buf) + 1; |
miaotwilio | 1:5a896191c3c4 | 118 | logInfo("MQTT message publishing buf: %s", buf); |
miaotwilio | 1:5a896191c3c4 | 119 | int rc = client.publish("sync/lists/vehicle0-data", message); |
miaotwilio | 1:5a896191c3c4 | 120 | logInfo("MQTT message publish result: %d", rc); |
miaotwilio | 0:b32fa0c757d7 | 121 | |
miaotwilio | 1:5a896191c3c4 | 122 | logInfo("MQTT disconnecting"); |
miaotwilio | 0:b32fa0c757d7 | 123 | client.disconnect(); |
miaotwilio | 0:b32fa0c757d7 | 124 | } |
miaotwilio | 1:5a896191c3c4 | 125 | ledMQTTYield = 1; |
miaotwilio | 1:5a896191c3c4 | 126 | } |
miaotwilio | 0:b32fa0c757d7 | 127 | |
miaotwilio | 1:5a896191c3c4 | 128 | /* |
miaotwilio | 1:5a896191c3c4 | 129 | static void subscribeToTest2() { |
miaotwilio | 1:5a896191c3c4 | 130 | rc = client.subscribe("sync/lists/test2", MQTT::QOS1, test2Handler); |
miaotwilio | 1:5a896191c3c4 | 131 | logInfo("MQTT subscription result: %d", rc); |
miaotwilio | 0:b32fa0c757d7 | 132 | } |
miaotwilio | 0:b32fa0c757d7 | 133 | |
miaotwilio | 0:b32fa0c757d7 | 134 | static void test2Handler(MQTT::MessageData& data) { |
miaotwilio | 0:b32fa0c757d7 | 135 | static const size_t MAX_DISPLAY_MESSAGE_SIZE = 30; |
miaotwilio | 0:b32fa0c757d7 | 136 | char buf[MAX_DISPLAY_MESSAGE_SIZE + 1]; |
miaotwilio | 0:b32fa0c757d7 | 137 | if (data.message.payloadlen <= MAX_DISPLAY_MESSAGE_SIZE) { |
miaotwilio | 0:b32fa0c757d7 | 138 | strncpy(buf, (char*)data.message.payload, data.message.payloadlen); |
miaotwilio | 0:b32fa0c757d7 | 139 | buf[data.message.payloadlen] = '\0'; |
miaotwilio | 0:b32fa0c757d7 | 140 | } else { |
miaotwilio | 0:b32fa0c757d7 | 141 | strncpy(buf, (char*)data.message.payload, MAX_DISPLAY_MESSAGE_SIZE - 3); |
miaotwilio | 0:b32fa0c757d7 | 142 | buf[MAX_DISPLAY_MESSAGE_SIZE-3] = '.'; |
miaotwilio | 0:b32fa0c757d7 | 143 | buf[MAX_DISPLAY_MESSAGE_SIZE-2] = '.'; |
miaotwilio | 0:b32fa0c757d7 | 144 | buf[MAX_DISPLAY_MESSAGE_SIZE-1] = '.'; |
miaotwilio | 0:b32fa0c757d7 | 145 | buf[MAX_DISPLAY_MESSAGE_SIZE] = '\0'; |
miaotwilio | 0:b32fa0c757d7 | 146 | } |
miaotwilio | 0:b32fa0c757d7 | 147 | logDebug("topic %s payload received len %d data %s", data.topicName.lenstring.data, data.message.payloadlen, buf); |
miaotwilio | 0:b32fa0c757d7 | 148 | |
miaotwilio | 0:b32fa0c757d7 | 149 | // sync client can send binary data using payload format: |
miaotwilio | 0:b32fa0c757d7 | 150 | // { |
miaotwilio | 0:b32fa0c757d7 | 151 | // "payload": "ZXhpdA==", # base64 encoded "exit" |
miaotwilio | 0:b32fa0c757d7 | 152 | // "_iot_meta": { |
miaotwilio | 0:b32fa0c757d7 | 153 | // "payload_encoding": "base64", |
miaotwilio | 0:b32fa0c757d7 | 154 | // "payload_type": "application/octet-stream", |
miaotwilio | 0:b32fa0c757d7 | 155 | // } |
miaotwilio | 0:b32fa0c757d7 | 156 | // } |
miaotwilio | 0:b32fa0c757d7 | 157 | if (0 == strncmp((char*)data.message.payload, "exit", data.message.payloadlen)) { |
miaotwilio | 0:b32fa0c757d7 | 158 | exitCmd = true; |
miaotwilio | 0:b32fa0c757d7 | 159 | } |
miaotwilio | 1:5a896191c3c4 | 160 | }*/ |