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

Committer:
pkamp
Date:
Tue Sep 19 21:45:19 2017 +0000
Revision:
10:e9abab84df23
Parent:
9:2d119fbe7482
Adding publishing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
miaotwilio 0:b32fa0c757d7 1 #include <mbed.h>
miaotwilio 0:b32fa0c757d7 2 #include <mtsas.h>
miaotwilio 0:b32fa0c757d7 3 #include <ssl.h>
pkamp 9:2d119fbe7482 4 #include <MbedJSONValue.h>
pkamp 9:2d119fbe7482 5 #include <string>
pkamp 9:2d119fbe7482 6 #include "MTSCellularManager.hpp"
pkamp 9:2d119fbe7482 7 #include "TlsMQTTClient.hpp"
pkamp 9:2d119fbe7482 8 #include "certificates.hpp"
miaotwilio 5:ded8fe5991a2 9
pkamp 9:2d119fbe7482 10 /*
pkamp 9:2d119fbe7482 11 * Sync Settings
pkamp 9:2d119fbe7482 12 *
pkamp 9:2d119fbe7482 13 * Enter a Sync Key & Password, your document unique name,
pkamp 9:2d119fbe7482 14 * and the device name
pkamp 9:2d119fbe7482 15 */
pkamp 9:2d119fbe7482 16 char* sync_key = "KYXXXXXXXXXXXXXXXXXXXX";
pkamp 9:2d119fbe7482 17 char* sync_password = "SECRET_HERE";
pkamp 9:2d119fbe7482 18 char* sync_document = "sync/docs/BoardLED";
pkamp 10:e9abab84df23 19 char* sync_device_name = "MultiConnect Dragonfly";
pkamp 9:2d119fbe7482 20
pkamp 9:2d119fbe7482 21 /* Sync server and MQTT setup; you probably don't have to change these. */
pkamp 9:2d119fbe7482 22 const char* mqtt_server = "mqtt-sync.us1.twilio.com";
pkamp 9:2d119fbe7482 23 const uint16_t mqtt_port = 8883;
pkamp 9:2d119fbe7482 24 const uint16_t maxMQTTpackageSize = 512;
pkamp 9:2d119fbe7482 25
pkamp 9:2d119fbe7482 26 TlsMQTTClient client = TlsMQTTClient();
pkamp 9:2d119fbe7482 27 DigitalOut led(D7);
pkamp 9:2d119fbe7482 28 DigitalOut bc_nce(PB_2);
pkamp 9:2d119fbe7482 29 const uint8_t MQTT_HEARTBEAT = 15;
miaotwilio 1:5a896191c3c4 30
pkamp 9:2d119fbe7482 31 /*
pkamp 9:2d119fbe7482 32 * Our Twilio Connected Devices message handling callback. This is passed as a
pkamp 9:2d119fbe7482 33 * callback function when we subscribe to the document, and any messages will
pkamp 9:2d119fbe7482 34 * appear here.
pkamp 9:2d119fbe7482 35 */
pkamp 9:2d119fbe7482 36 void callback(MQTT::MessageData& data)
pkamp 9:2d119fbe7482 37 {
pkamp 9:2d119fbe7482 38 if (data.message.payloadlen > maxMQTTpackageSize) {
pkamp 9:2d119fbe7482 39 return;
pkamp 9:2d119fbe7482 40 }
pkamp 9:2d119fbe7482 41 char buf[maxMQTTpackageSize + 1];
pkamp 9:2d119fbe7482 42 strncpy(buf, (char*)data.message.payload, data.message.payloadlen);
pkamp 9:2d119fbe7482 43 buf[data.message.payloadlen] = '\0';
pkamp 9:2d119fbe7482 44
pkamp 9:2d119fbe7482 45 logDebug("Received new update %s", buf);
pkamp 9:2d119fbe7482 46 /* JSON Parse 'led' */
pkamp 9:2d119fbe7482 47 MbedJSONValue parser;
pkamp 9:2d119fbe7482 48 parse(parser, buf);
pkamp 9:2d119fbe7482 49
pkamp 9:2d119fbe7482 50 /* The parser will segfault and reset the board if "led" isn't contained. */
pkamp 9:2d119fbe7482 51 if (parser.hasMember("led")) {
pkamp 9:2d119fbe7482 52 std::string led_str;
pkamp 10:e9abab84df23 53 led_str = parser["led"].get<std::string>();
pkamp 9:2d119fbe7482 54
pkamp 9:2d119fbe7482 55 if (led_str.compare("ON") == 0) {
pkamp 9:2d119fbe7482 56 logDebug("Turning LED ON");
pkamp 9:2d119fbe7482 57 led = 0; // Active LOW
pkamp 9:2d119fbe7482 58 } else {
pkamp 9:2d119fbe7482 59 logDebug("Turning LED OFF");
pkamp 9:2d119fbe7482 60 led = 1; // Active LOW
pkamp 9:2d119fbe7482 61 }
pkamp 9:2d119fbe7482 62 }
pkamp 9:2d119fbe7482 63 }
miaotwilio 5:ded8fe5991a2 64
miaotwilio 0:b32fa0c757d7 65
pkamp 9:2d119fbe7482 66 /*
pkamp 9:2d119fbe7482 67 * This function connects to Sync via MQTT. We connect using the key, password,
pkamp 9:2d119fbe7482 68 * and device name defined as constants above, and checks the server
pkamp 9:2d119fbe7482 69 * certificate.
pkamp 9:2d119fbe7482 70 *
pkamp 9:2d119fbe7482 71 * If everything works, we subscribe to the document topic and return.
pkamp 9:2d119fbe7482 72 */
pkamp 9:2d119fbe7482 73 void connect_mqtt()
pkamp 9:2d119fbe7482 74 {
pkamp 9:2d119fbe7482 75 MQTTPacket_connectData conn_data = MQTTPacket_connectData_initializer;
pkamp 9:2d119fbe7482 76 conn_data.clientID.cstring = sync_device_name;
pkamp 9:2d119fbe7482 77 conn_data.username.cstring = sync_key;
pkamp 9:2d119fbe7482 78 conn_data.password.cstring = sync_password;
pkamp 9:2d119fbe7482 79 int rc = client.connect(
pkamp 9:2d119fbe7482 80 mqtt_server,
pkamp 9:2d119fbe7482 81 mqtt_port,
pkamp 9:2d119fbe7482 82 MQTT_GATEWAY_PROD_ROOT_CA_PEM,
pkamp 9:2d119fbe7482 83 conn_data
pkamp 9:2d119fbe7482 84 );
pkamp 9:2d119fbe7482 85 logInfo("MQTT connect result: %d", rc);
pkamp 9:2d119fbe7482 86
pkamp 9:2d119fbe7482 87 rc = client.subscribe(
pkamp 9:2d119fbe7482 88 "sync/docs/BoardLED",
pkamp 9:2d119fbe7482 89 MQTT::QOS1,
pkamp 9:2d119fbe7482 90 callback
pkamp 9:2d119fbe7482 91 );
pkamp 9:2d119fbe7482 92 logInfo("MQTT subscription result: %d", rc);
pkamp 9:2d119fbe7482 93 }
miaotwilio 1:5a896191c3c4 94
miaotwilio 6:4a25f3b9caef 95
pkamp 9:2d119fbe7482 96 /*
pkamp 9:2d119fbe7482 97 * Very basic device loop - all we do is reconnect when disconnected
pkamp 9:2d119fbe7482 98 */
pkamp 9:2d119fbe7482 99 void loop()
pkamp 9:2d119fbe7482 100 {
pkamp 9:2d119fbe7482 101 if (client.isConnected()) {
pkamp 9:2d119fbe7482 102 client.yield(MQTT_HEARTBEAT/2.0);
pkamp 9:2d119fbe7482 103 wait(MQTT_HEARTBEAT);
pkamp 9:2d119fbe7482 104 } else {
pkamp 9:2d119fbe7482 105 wait(MQTT_HEARTBEAT*10);
pkamp 9:2d119fbe7482 106 connect_mqtt();
pkamp 9:2d119fbe7482 107 }
pkamp 10:e9abab84df23 108
pkamp 10:e9abab84df23 109 // Here's an example of publishing from the MultiConnect Dragonfly.
pkamp 10:e9abab84df23 110 // Uncomment until the end of the function to send a 'msg' back to Sync
pkamp 10:e9abab84df23 111 // every 5 cycles through the loop! (5*MQTT_HEARTBEAT seconds)
pkamp 10:e9abab84df23 112 /*
pkamp 10:e9abab84df23 113 static uint32_t ticks = 0;
pkamp 10:e9abab84df23 114 MQTT::Message message;
pkamp 10:e9abab84df23 115 char buf[maxMQTTpackageSize] = "{\"msg\":\"Ahoy!\",\"led\":\"ON\"}";
pkamp 10:e9abab84df23 116 message.qos = MQTT::QOS1;
pkamp 10:e9abab84df23 117 message.payload = (void*)buf;
pkamp 10:e9abab84df23 118 message.payloadlen = strlen(buf) + 1;
pkamp 10:e9abab84df23 119 if (ticks++ > 4) {
pkamp 10:e9abab84df23 120 logInfo("Sending ON message to Twilio!");
pkamp 10:e9abab84df23 121 client.publish(
pkamp 10:e9abab84df23 122 sync_document,
pkamp 10:e9abab84df23 123 message
pkamp 10:e9abab84df23 124 );
pkamp 10:e9abab84df23 125 ticks = 0;
pkamp 10:e9abab84df23 126 }
pkamp 10:e9abab84df23 127 */
pkamp 9:2d119fbe7482 128 }
pkamp 9:2d119fbe7482 129
pkamp 9:2d119fbe7482 130
pkamp 9:2d119fbe7482 131 /*
pkamp 9:2d119fbe7482 132 * In main, we configure our LEDs, connect to Twilio Programmable Wireless,
pkamp 9:2d119fbe7482 133 * and initialize CyaSSL. We then connect our MQTT client for the first time.
pkamp 9:2d119fbe7482 134 *
pkamp 9:2d119fbe7482 135 * When done, we pass control to the MQTT loop, which handles yield()s.
pkamp 9:2d119fbe7482 136 */
pkamp 9:2d119fbe7482 137 int main()
pkamp 9:2d119fbe7482 138 {
pkamp 9:2d119fbe7482 139 led = 1; // Active LOW
pkamp 9:2d119fbe7482 140 bc_nce = 1;
miaotwilio 0:b32fa0c757d7 141 Serial debug(USBTX, USBRX);
miaotwilio 0:b32fa0c757d7 142 debug.baud(115200);
miaotwilio 0:b32fa0c757d7 143 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
miaotwilio 0:b32fa0c757d7 144
pkamp 9:2d119fbe7482 145 // Be sure your SIM is registered in the Twilio console.
pkamp 9:2d119fbe7482 146 // https://www.twilio.com/console/wireless/sims/
pkamp 9:2d119fbe7482 147 logInfo("Initializing Twilio Programmable Wireless");
miaotwilio 0:b32fa0c757d7 148 MTSCellularManager cellularManager("wireless.twilio.com");
miaotwilio 0:b32fa0c757d7 149 if (! cellularManager.init()) {
miaotwilio 0:b32fa0c757d7 150 while (true) {
pkamp 9:2d119fbe7482 151 logError("failed to initialize cellular radio"); wait(10);
miaotwilio 1:5a896191c3c4 152 }
miaotwilio 1:5a896191c3c4 153 }
pkamp 9:2d119fbe7482 154 CyaSSL_Init();
pkamp 9:2d119fbe7482 155 connect_mqtt();
miaotwilio 0:b32fa0c757d7 156
pkamp 9:2d119fbe7482 157 /* We're done; pass off control to the loop */
pkamp 9:2d119fbe7482 158 while (1) {
pkamp 9:2d119fbe7482 159 loop();
miaotwilio 0:b32fa0c757d7 160 }
pkamp 9:2d119fbe7482 161 }