Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now

Menu

Expand
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?

Programmable Wireless: Raspberry Pi Sixfab IoT Gateway Quickstart

Using these instructions you'll learn how to create an IoT gateway using a Twilio SIM, a Raspberry Pi and the SixFab 3G-4G/LTE Base Shield V2 with a Quectel EC25 modem. This quickstart is intended to demonstrate how to use a Twilio SIM as an IoT waypoint for receiving data as a standalone device or as a gateway for collecting data from multiple IoT devices (mesh-networking).

With an IoT gateway we can build programs that can use other Twilio services and 3rd party libraries. Unlike machine-to-machine examples which use the Commands API this paradigm tracks user data packets using the Data Sessions API. This is intended to be used with a single-board computer such as the Raspberry Pi (SBC) running a Linux operating system with the recommended modem below.

Hardware Prerequisites

Setup & Software Requirements

Not the quickstart you were looking to complete? See our other Programmable Wireless quickstarts or see all Wireless resources.

I'm got everything ready - let's do this!

Already have a Twilio account and SIM card? Skip ahead to the register and activate step.

Before you can connect with Twilio Programmable Wireless you'll need a Twilio account. Once you've signed up, continue with this quickstart.

First, visit the Programmable Wireless Console. On the Orders page, purchase a set of 3 starter SIMs by selecting the SIM Starter Pack... or make a larger custom order by entering a quantity under the Multi-Size SIM Card product.

Order SIM Cards from Twilio

Orders of 10 SIMs or more will be delivered with the SIM cards already tied to your account.

I've got my SIM! What's next?

Register, assign a Rate Plan, and activate your SIM

Once you receive your SIM shipment, visit the Register a SIM page in the Programmable Wireless Console.

SIM Card Registration Code

Follow that up by picking a Unique Name for the SIM.

Choose a Rate Plan

Next, select a Rate Plan from the options provided by Twilio.

By default, there are Rate Plans added which approximate the usage Twilio sees in the field. Rate Plans let you control every aspect of what your device can do its maximum monthly data usage. We've added some reasonable defaults, but feel free to explore options later.

Activate a SIM card

When you're happy with the Rate Plan, 'Activate' the SIM card.

You'll need to activate your SIM for real-world workflows although you can use small amounts of data without activating.

Time to get the hardware ready for connectivity

Connect the peripherals

1. Insert the SD card.

RpiSixfab-SD.png

2. Add the SixFab shield to the Raspberry Pi.

RpiSixfab-Shield.png

3. Carefully attach the cellular antenna to the Quectel modem.

RpiSixfab-Antenna.png

4. Connect the Micro-USB cable to the SixFab board.

RpiSixfab-MicroUSB.png

5. Insert the USB WiFi module.

RpiSixfab-WiFiUSB.png

6. Power the Raspberry Pi with the Micro-USB power supply and connect to the computer. After all the components are connected, it will look like the image below.

RpiSixfab-MicroUSBPower.png

Let's bake!

Flashing the SD

We want to start with a lightweight version of the Raspbian operating system.

Want to use an already built image incorporating Twilio's modifications and scripts?

  1. Download Raspbian Stretch LITE 4.14 with Twilio modifications
  2. Burn an SD card using Etcher
  3. Skip ahead to the next step

This image will not always be up to date with the most recent Raspbian release - be sure to update the image if you choose to install it. Always check the Raspbian download page for the latest image.

  1. Navigate to the Raspbian download page and download the newest distribution of Raspbian Lite
  2. Using the Etcher burning tool (or other option), flash the image to the MicroSD or SD card
Time to log-on!

Setting up SSH

After the SD card has been flashed navigate to the volume that named "boot" using your file manager. Inside this volume add a new file called ssh with no extension. This will make sure the operating system is ready to connect when you boot up the Raspberry Pi for the first time.

Next, plug the Micro-USB cable into the Raspberry Pi and connect the board to the computer. The operating system will take a few moments to power on. After about a minute use ping to find out the IP address of the board.

ping rasperrypi.local

Copy the address and let's SSH into the board.

ssh pi@xxx.xxx.xxx.xxx

When the prompt appears about continuing to the operating system type in yes to enter via SSH.

Setup the internet connection

Go to the Raspberry Pi's network settings to configure the WiFi connection. If the Raspberry Pi is connected to the internet via an ethernet cable this step can be skipped.

sudo raspi-config

Navigate to Network Options

RpiSixfab-NetworkOptions.png

Click WiFi.

RpiSixfab-WiFi.png

Add the network SSID.

RpiSixfab-WiFiSSID.png

Type in the password for the network.

RpiSixfab-WiFipassword.png

Click Ok to connect.

Check that the WiFi is connected.

sudo ifconfig

When connected an IP address will be visable where it says wlan0.

RpiSixfab-ifconfig.png

Great! We are connected to the internet via WiFi. Next we will download and install a few things.

Update, install and connect to the web

Update and install the QMI Interface

When looking into the Raspberry Pi for the first time you will want to do is to update the operating system.

sudo apt-get update

Next install the the Raspberry Pi kernel headers needed for the QMI Interface.

sudo apt-get install raspberrypi-kernel-headers

We now want to see what version of the kernel is installed. Note the version number.

RpiSixfab-KernelHeader.png

Navigate to the repository for Raspberry Pi firmware updates and locate the header version above on the page. In this case we want to find release 4.14.9.

Copy the kernel commit-id by pressing the copy to clipboard button.

RpiSixfab-Github.png

Update the firmware of the Raspberry Pi using the commit number found in the repository relating to the kernel version.

RpiSixfab-UpdateKernel.png

Then reboot the Raspberry Pi.

sudo reboot

SSH back into the Raspberry Pi to continue the installation process.

ssh pi@xxx.xxx.xxx.xxx

Continuing, we need to download the QMI installer file.

wget https://raw.githubusercontent.com/sixfab/Sixfab_RPi_3G-4G-LTE_Base_Shield/master/tutorials/QMI_tutorial/qmi_install.sh

Make the installation file an executable.

sudo chmod +x qmi_install.sh

Run the installation file.

sudo ./qmi_install.sh

After the installation is complete a prompt will appear to restart the board again. Press any button to reboot.

SSH back into the Raspberry Pi to continue the installation process.

ssh pi@xxx.xxx.xxx.xxx

Navigate to the Quectel files that were installed in the previous step.

cd files/quectel-CM

Fire up the modem to connect to the internet using the Twilio APN.

sudo ./quectel-CM -s wireless.twilio.com

Once the executable starts the output should look something like this:

RpiSixfab-ConnectedModem.png

Open a new tab in the terminal and SSH into the pi again.

ssh pi@xxx.xxx.xxx.xxx

Check the network settings again to verify that the modem is connected. The id for the modem should be wwan0.

RpiSixfab-wwan0redacted.png

Turn off the WiFi and ping the world using cellular IoT

In the terminal use the following command to turn off the WiFi connection.

sudo ifconfig wlan0 down

Next ping Twilio to make sure the connection is made.

ping -c 3 www.twilio.com

Success!

Wait, what about Commands?

Commands vs. Data Packets

With an IoT gateway like this Sixfab example we can build programs that rely on cellular connectivity using the Twilio SIM without the need of using the Commands API. The device could be used as a singular device for collecting data or as a waypoint for other IoT devices for mesh-networking.

When a Command is sent over the network they are logged in the Twilio Programmable Wireless console. When we send data over the network it is logged as a "packet" and is defined as being Downloaded or Uploaded. This information can be found under the Connectivity tab of the Twilio SIM selected. The relative location of the cellular activity is also logged and can be viewed using the interactive Google Maps plugin embedded in the console.

RpiSixfab-Data.png

More information regarding account information and packet data can be found within the Programmable Wireless Data Sessions API.

Welcome to cellular IoT!

I'm connected! What should I do next?!

Where to next?

Now that you have seen how to send and receive Machine to Machine commands with the Wio LTE and your development machine, you're ready to build your first TwilioIoT project. Here are some possible ideas to take it to the next level:

Rate this page:

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.