Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Twilio Sync for IoT and Programmable Wireless on a MultiConnect® Dragonfly™

Today we'll bring Twilio Sync for IoT to 4G using the Arm Mbed Online IDE and a Multi-Tech MultiConnect® DragonflyTM. We'll use the Twilio Device Manager to create a Device Fleet then a connection to Sync. We'll do all of that using Twilio's Programmable Wireless cellular connectivity.

And what are we going to do with all of that power? Blink an LED, of course!

Sync for IoT is currently in Developer Preview, which means access is by invite only. If you'd like to try what you see in these docs, sign up for the developer preview and the team at Twilio will get you onboarded as soon as possible.

Sign Up For (or Sign Into) a Twilio Account

Create a new Twilio account (you can sign up for a free Twilio trial) or sign into an existing Twilio account.

Once logged in, click the Twilio Device Manager link to go to the Sync for IoT console.

Purchase and Activate a Programmable Wireless SIM Card

The code in this Quickstart will work with 4G via Twilio's Programmable Wireless. You can order SIM cards here directly from the Twilio console.

Here's the order of operations to activate a SIM card:

Once your SIM has a rate plan with data enabled insert it into the board's SIM slot. Additionally, attach the cellular antenna to your board.

Deploy a Fleet of MultiConnect® DragonflyTM Things

Device Fleets are collections of devices, keys or certificates, and configurations of Twilio Sync. You may create many fleets, but each individual fleet will be isolated.

A Default Fleet is already provisioned for you in Sync for IoT. Get started by navigating to it in the Device Management console:

Locating the default Ffleet in the Twilio Console

(You may also deploy and configure a fleet through the Twilio REST API.)

Loading Code Sample...
      
      
      
      
      Using the API to create a Device Fleet in Sync for IoT.

      Create a Device Fleet

      Using the API to create a Device Fleet in Sync for IoT.

      Let's move next to the Deployment.

      Configure the Default Deployment

      Twilio provides a default Deployment as well, configuring it upon Fleet creation. The 'Default Deployment' field in your Fleet should look something like this:

      A default deployment is automatically provisioned for each new Device Fleet.

      Follow the 'Configure the Deployment' link:

      Every deployment is configured with a default Service Instance.

      Twilio also creates a default Sync Service Instance. Instances are similar to isolated databases. When you make an update on a Sync primitive (such as a Document), the update only affects the named primitive in this Instance.

      As before, you can create and configure a Deployment with the API.

      Loading Code Sample...
          
          
          
          

          Deployment handled, next create a Device and Device Key.

          Create a Device and Device Key

          For Sync for IoT, a Device is one piece of hardware managed through the Device Manager. When connecting to Sync, Twilio identifies your device either through a key or certificate. Today we'll be using a Device Key to authenticate with Sync.

          While inside your Fleet, on the left sidebar click the 'Devices' link. Enter a Unique and Friendly name for the board then hit the 'Create' button.

          Create a New ESP8266 Device in Deployed Devices

          You can also create and manage a Device through the Twilio API.

          Loading Code Sample...
              
              
              
              

              Now (in your new Device configuration) click the 'Keys' link. You'll be presented with the following screen:

              Create New Device Key Button

              The Device Sid should automagically be populated, so enter a Unique Name and 'Create'. Note: Save both the Key and the Secret - once the secret is lost, you can't recover it!

              Create a Device Key in Sync Deployed Devices

              You can create and delete Device Keys using the REST API. Here's an example:

              Loading Code Sample...
                  
                  
                  
                  

                  In Arm Mbed, you'll be copying the Device Key and Device Secret that you just created. Keep them handy for that step.

                  Create a Sync Document

                  A Document is the simplest type of object in Sync (excellent for LED state tracking!). You will have to create a document before you connect to Sync. The Document name will map directly to a MQTT topic name, and you'll subscribe in the code. Name your Document 'BoardLED' (match our capitalization).

                  Loading Code Sample...
                      
                      
                      
                      
                      Use the Sync Service Sid from the 'Configure Deployment' Screen, as Shown Above.

                      Create a Sync Document

                      Use the Sync Service Sid from the 'Configure Deployment' Screen, as Shown Above.

                      We initialize led key to value OFF and create a new document BoardLED.

                      Prerequisites to Use a MultiConnect® DragonflyTM with Sync

                      You can grab all of our LinkIt code quickly from Github.

                      For this guide's development, we used a Multi-Tech MultiConnect® DragonflyTM with the Arm Mbe IDE. For an overview of connecting to Mbed with this board, please see this page. For help setting up the dev board, SIM, PCB antenna, or modem find Multi-Tech's instructions here.

                      Add All Our Code to the Arm Mbed IDE

                      The easiest way to get started with our code is to follow this link:

                      At that link, simply follow the yellow 'Import into Compiler' button and IDE prompts to load our code into your workspace.

                      Add Your Device Key and Secret to the Code

                      Above, you created a new device key and saved the lkey and secret somewhere safe. Now, edit the lines at the top of main.cpp which ask for those credentials.

                      Loading Code Sample...
                          
                          
                          
                          
                          Paste your generated device key and device secret into Arm Mbed.

                          Add Your Device Key and Secret

                          Paste your generated device key and device secret into Arm Mbed.

                          Excellent - that's all you actually need to do to get the demo going!

                          Libraries Used in this Quickstart

                          We used four libraries for this demo today. Please note that while our code is licensed MIT, not all other libraries share this license. Be sure to research the suitability of all relevant licenses when developing using our code.

                          Burn the Code and Toggle the LED

                          We're getting close now - it's burning time!

                          Inside Mbed, click the 'Compile' button. Mbed will compile your code for the DragonflyTM and produce a binary file for uploading. While your board and dev kit are connected to the computer via USB, upload the binary file to the enumerated USB storage device. When complete, hit the white reset button to load the new binary.

                          Arm Mbed Compile Program

                          Optionally, now open up a serial monitor to listen to the board. On Windows we like PuTTY, and on *NIX we prefer screen.

                          But wait - those newly scrolling messages aren't the demo! Let's blink one of the LEDs onboard that dev board. Set 'led' to 'ON' in the Document you created above.

                          Loading Code Sample...
                              
                              
                              
                              

                              You should quickly see three signs your API Request went through:

                              • Sync responds to your code or command with a success message
                              • The dev board LED will light
                              • If you've got PuTTY or Screen going, you'll see a nice message

                              Multiconnect Dragonfly Blinking LED

                              Sending Updates Back to Sync from the LinkIt

                              If you look in main.cpp, there are commented lines which would send updates back to the Document. Remove the comment block to send a msg back to Sync periodically.

                              Loading Code Sample...
                                  
                                  
                                  
                                  
                                  Comment out these lines to periodically update the BoardLED Document from the device

                                  Optionally Send Data Back to Sync

                                  Comment out these lines to periodically update the BoardLED Document from the device

                                  Whatever you send back to Sync should be in JSON format.

                                  Wirelessly Expanding Your Internet of Things

                                  4G speeds, blue LEDs, scalable infrastructure - awesome, isn't it?

                                  While we know that your application is going to go a lot further than blinky lights, the same infrastructure you just built can scale to crazy heights with you. Scale far and wide - build dashboards, synchronize hardware, or... well, you decide.

                                  Wherever your business or project takes you hit us with your tweets on Twitter. We can't wait to see what you build!

                                  Kat King

                                  Need some help?

                                  We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

                                  Loading Code Sample...