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.
Once logged in, click the Twilio Device Manager link to go to the Sync for IoT console.
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:
- Navigate to the Programmable Wireless section of the console with a SIM in hand
- If your SIM is already associated, follow the instructions on the SIMs page (Click the plus '+' button)
- If it is unassociated, follow the instructions in 'Starter SIMs'
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.
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:
(You may also deploy and configure a fleet through the Twilio REST API.)
Let's move next to the 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:
Follow the 'Configure the Deployment' link:
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.
Deployment handled, next 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.
You can also create and manage a Device through the Twilio API.
Now (in your new Device configuration) click the 'Keys' link. You'll be presented with the following screen:
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!
You can create and delete Device Keys using the REST API. Here's an example:
In Arm Mbed, you'll be copying the Device Key and Device Secret that you just created. Keep them handy for that step.
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).
led key to value
OFF and create a new document
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.
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.
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.
Excellent - that's all you actually need to do to get the demo going!
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.
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.
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.
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
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.
Whatever you send back to Sync should be in JSON format.
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!