Getting Started with Twilio Programmable Wireless on the LinkIt ONE

May 24, 2016
Written by

cover

At SIGNAL we launched Twilio Programmable Wireless which allows you to add cellular data to your IoT projects using a Twilio SIM card. The LinkIt ONE dev board in the SIGNAL hackpack is a perfect place to try out these new capabilities. In the next 5 minutes you’ll learn how to use the cellular functionality of this device using your Twilio SIM card.

What You’ll Need

Before we get to hacking there are a few things we need to get set up. First, we need to set up the LinkIt ONE board and make sure it is programmable using the Arduino IDE and the MediaTek LinkIt ONE SDK. The steps for getting this set up are outlined in this blog post. Once you’ve completed those steps you’ll want to also make sure to attach the cellular antenna if you haven’t already because it is needed for Twilio Programmable Wireless.

Next, we need to activate the Twilio SIM in our account. If you received a SIM card with a hackpack at SIGNAL your SIM is already activated so you can skip this activation step. Otherwise, you can activate your SIM card in the Twilio Programmable Wireless portion of the console by entering its
ICCID into the text field as shown on this screenshot:

Once you’ve activated your SIM card, plug it into the LinkIt ONE’s SIM card slot as shown in this photo:

That’s all for hardware setup.

Can I Make a Request?

The MediaTek LinkIt ONE SDK includes a library for working with the cellular functionality of the LinkIt ONE development board. To include this library in our project we’ll add it from the Library Manager in the Arduino IDE. Create a new Sketch and under the Sketch menu select Include Library > LGPRS. This will add four header files. We only need two of them: LGPRS.h and LGPRSClient.h. The top of your Sketch should look like this:

#include <LGPRS.h>
#include <LGPRSClient.h>

The LGPRS class allows us to attach to a GPRS network. The LGPRSClient class is what we’ll use to make an HTTP request using the LGPRS connection we’ve established using LGPRS. Add the following code underneath the two #include lines:

#define SITE_URL "putsreq.com"
LGPRSClient client;

For this tutorial I’ve set up a URL on putsreq.com that returns “Hello world”. If you want to see it in action in your browser you can click on this link. In the code above we #define the base URL for this site in SITE_URL. We’ll use this later when we connect to the site and make an HTTP request. The next line declares an LGPRSClient variable that we’ll use to write our HTTP GET request.

All Arduino projects consist of a setup() function that initializes the project and a loop() function that runs indefinitely and does the main work for the project. We’ll start in the setup() method by turning on the cellular radio and connecting it to Twilio Programmable Wireless. Replace your setup() function with the following code:

void setup() {
   Serial.begin(9600);
 
   // Connect to cellular network using Twilio Wireless
   while(!LGPRS.attachGPRS("wireless.twilio.com", NULL, NULL)) {
      Serial.println("Waiting for GPRS signal...");
      delay(1000);
    }
    Serial.println("GPRS attached.");
}

'>Now would be a great time to make sure everything is working. Click the <code>Upload</code> button in the Arduino IDE and open up the <code>Serial Monitor</code> from the <code>Tools</code> menu. If everything is working correctly you should see “GPRS attached.” printed out in the <code>Serial Monitor</code>.</p>
<p>Next use the <code>LGPRSClient</code> to make a connection to <code>SITE_URL</code> so that we can send requests to our putsreq.com sample. Add the following code to the end of your <code>setup()</code> function:</p><!-- Crayon Syntax Highlighter v_2.7.2_beta -->

		<div>
javascript // Connect client to the test site Serial.println("Connecting to: " SITE_URL "..."); if(!client.connect(SITE_URL, 80)) { Serial.println("Could not connect to: " SITE_URL);    return; } else { Serial.println("Connection successful."); }
<!-- [Format Time: 0.0011 seconds] -->
<p></p>
<p class='"lang:end<br '>We first attempt to connect to the site on port 80. If we are successful, “Connection successful” is printed in the <code>Serial Monitor</code>. If not, we return from <code>setup()</code> and print an error message. Now that we’re connected to the site we can make our HTTP GET request. Add this code to the end of your <code>setup()</code> function:</p>
<p></p><!-- Crayon Syntax Highlighter v_2.7.2_beta -->

		<div>
javascript Serial.println("Sending test GET request..."); // Make GET request to test site. Returns "Hello world!" // We'll display the response in loop() client.println("GET /m3JKNFhGxaeu0KsXcdLl HTTP/1.1"); client.println("Host: " SITE_URL); client.println(); Serial.println("Waiting for response from test site..."); Serial.println("————————————————————————");
<!-- [Format Time: 0.0009 seconds] -->
<p>This code writes out the HTTP GET request lines using the LGPRS <code>client</code>. The response to this request will be available in the <code>loop()</code> function.</p>
<p>Write the <code>loop()</code> function thatreads the results of our HTTP GET request to <a href="http://putsreq.com/m3JKNFhGxaeu0KsXcdLl" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://putsreq.com/m3JKNFhGxaeu0KsXcdLl', 'http://putsreq.com/m3JKNFhGxaeu0KsXcdLl');">http://putsreq.com/m3JKNFhGxaeu0KsXcdLl</a>. Replace the <code>loop()</code>function in your Sketch with this code:</p><!-- Crayon Syntax Highlighter v_2.7.2_beta -->

		<div>
javascript void loop() {   if (client.available())    {       char c = client.read();       Serial.print(c);    } }