The Internet of Things (IoT) market is booming. Gartner forecasts that in 2016, 6.4 billion connected things will be in use worldwide, and support total services spending of $235 billion. They went on to predict that the number of connected things will reach 20.8 billion by 2020. The International Data Corporation (IDC) estimates the IoT market will reach $1.7 trillion in 2020. When it comes to connecting new and legacy systems to the Internet, there has never been a better time.
Internet of Things (IoT) solutions have a number of moving parts. At the heart of all these solutions sits the IoT gateway, providing connectivity, scalability, security, and management. This getting started guide will help you understand:
- What an IoT gateway is.
- How it can be used as the hub of commercial and residential IoT solutions.
- How to choose the right gateway for you.
- What software development tools are available.
- How to write a “Hello World” application that will run on your gateway.
Let’s get started!
What is an IoT Gateway?
What is a Gateway For?
An IoT gateway is the heart of an IoT solution that ties all of the pieces together. On one side we have all the “things”, which can include sensors, smart devices, vehicles, industrial equipment, or anything that can be made to be “smart”, and produce data. On the other side we have network and data infrastructure, which stores and processes all the data the things produce. Gateways are the connection between the things and the network and data infrastructure; they are the glue that holds it all together.
However, gateways are so much more than just the glue; they are a solution to the connectivity challenge that all developers face. The connectivity challenge has two major needs:
- Robust and secure access to the Internet or Wide Area Network (WAN).
- Ability to support access to a multitude of devices, many of which have limited to no processing capability.
Connecting a single sensor to the Internet can get complicated and expensive. What happens when you have varying types of sensors, each with a different type of interface, and want to aggregate the data into a single interface?
Gateways help overcome these challenge by providing:
- Communications and connectivity
Communications and Connectivity
Wired and wireless connectivity are standard on many devices. Protocols you’ll find in use include Cellular 2G/3G/4G, Bluetooth*, Serial, USB, Virtual Private Network (VPN), Wi-Fi Access Point, MQ Telemetry Transport (MQTT) messaging protocol, and ZigBee*. These enable you to connect to sensors and systems using a variety of methods. With such a wide range of available protocols, you’ll be hard pressed to find a sensor or device you can’t connect to.
Much like network routers, IoT gateways can be connected together to form even larger solutions. Whether you’re creating a home automation system with a single gateway, or have a multiacre industrial facility with new and legacy systems that need connecting, you can connect everything together into a single system.
Data encryption and software lockdown are a couple of the security features you’ll find on IoT gateways. Additionally, many devices offer whitelisting, change control, secure storage, and secure boot, as well as a wide array of protocols, services, and malware protection. All of these features combine to ensure that your systems and data are kept secure at all times (a critical aspect, as the IoT continues to expand at exponential rates, becoming an even greater target for hackers and thieves).
Manageability refers to the deployment, maintenance and management of your solutions. With the potential of complexity, simplicity in management is key. To help you’ll find web-based interfaces you can securely access to maintain the gateway itself, manage the connected sensors, and control how data flows through it. Many gateways use some form of embedded linux*, so the admin tools you know and love like ssh and scp are available.
In a commercial setting, a gateway can connect a series of sensors (for example: light, temperature, smoke, energy, RFID) and systems (for example: HVAC, vending, security, transportation) to control and monitoring devices such as data stores and servers to be retrieved by laptops, tablets, and smart watches.
Specific examples include:
- Commercial trucking companies collecting GPS and loading information from their fleets. Each truck has an Internet-connected gateway which filters and relays data from the truck’s systems.
- Construction companies monitoring the noise levels on their sites in order to comply with local noise regulations. Each site has noise and vibration sensors connected to one or more gateways which send the data to the onsite supervisors.
The most common residential application of IoT is home automation. In this scenario, a gateway helps provide a single point of control by intelligently connecting your security system, thermostat, lighting controls, smoke detector, and more. Typically, a web interface accessed from within your home or securely over the Internet provides a unified view of all these systems.
Smart meters are another example in common use today; they detect energy consumption information and send it back to the electric company as frequently as every hour, sometimes in even shorter intervals. These meters also allow for two-way communication between the meter and the electric company.
Which Gateway is Right for Me?
With many options available on the market, which is right for you? ultimately that depends mainly on two factors:
- The type of Internet connectivity available (wired, wireless, cellular).
- The types of sensors you’ll be using and the types of interfaces they have (USB, serial, Bluetooth*).
Intel has a large ecosystem of manufacturing partners which provide a variety of options. On the IoT section of the Intel® Developer Zone you’ll find two useful tools: the Solutions Directory and the Gateway Comparison Tool. Using both of these tools you’ll find solutions with the following features:
- Single-core Intel® Quark™ SoC X1000 400 MHz processors
- Single, dual, and quad-core Intel® Atom™ processors
- Single, dual, and quad-core Intel® Core™ processors
Networking and Communications
- Wi-Fi (single and multiple-radio)
- Dual LAN
- CAN bus
- Analog and digital I/O
- Wind River* linux* 7
- Snappy Ubuntu* Core
- Microsoft Windows® 10 IoT
This guide was written using the Advantech* UTX-3115 gateway with an OMEGA* RH USB sensor which measures temperature and relative humidity.
Industry Verticals Applying IoT Technology
We’ve seen a number of applications of IoT gateway technology in both the commercial and residential sectors, but where specifically can this technology be applied?
Here’s a partial breakdown of industries and the verticals where IoT technology is being applied:
|Industry||Verticals||Example Use Cases|
|Public Sector||Cities||City Wi-Fi, parking, traffic|
|Public Safety||Schools, border, law enforcement|
|Manufacturing||Factories||Energy management, security, automation|
|Energy and Minerals||Utilities||Mobile workforce, substation and distribution automation|
|Oil and Gas||Pipeline monitoring, refinery systems, secure operations|
|Mining||Asset visibility and monitoring, predictive maintenance|
|Transportation||Transportation||Roadways, trains, stations|
|Business to Consumer (B2C)||Retail||Remote expert / mobile adviser, digital media store experience|
|Sports and Entertainment||Stadium, stadium Wi-Fi, stadium vision|
|SP & Machine-to-Machine (M2M)||Remote tower management, fleet/asset management|
|Healthcare||Virtual patient observation, patient wayfinding|
|Financial Services Industry||In-branch customer experience, energy management|
Wind River* linux* in the Context of Python*
The operating system of the Intel® IoT Gateway is Wind River* linux*, a commercial embedded linux distribution. Because it’s linux, you can run just about anything on it, including Python*. In fact, the latest version of the Intel® IoT Gateway comes with Python 2.7.3 preinstalled. In addition, you can download updated Python version packages and other applications from the Intel Open Source Technology Center, or using the built-in IoT Gateway Developer Hub that’s running on the gateway.
MRAA / UPM
MRAA (pronounced em-rah) is a low-level library written in C. The purpose of MRAA is to abstract the details associated with accessing and manipulating the basic I/O capabilities of a platforms, into a single, concise API. MRAA serves as a translation layer on top of the linux General Purpose Input/Outputs (GPIO) facilities. Although linux provides a fairly rich infrastructure for manipulating GPIOs, and its generic instructions for handling GPIOs are fairly standard, it can be difficult to use. Having said that, you can use MRAA to communicate with both analog and digital devices. Be sure to check out the MRAA API Documentation.
To install MRAA on your gateway, download the latest version from the Intel Open Source Technology Center using curl, and then use the rpm command to install. As an example, if you’re running the current system version - 126.96.36.199 - the commands would be as follows:
>> curl -O https://download.01.org/iotgateway/rcpl13/x86_64/libmraa0-0.8.0-r0.0.corei7_64.rpm
>> rpm -ivh libmraa0-0.8.0-r0.0.corei7_64.rpm
IDEs and Text Editors
There are a number of IDE’s and text editors available on the market today. If you already have a favorite, go ahead and use that. Otherwise, below are three available options to choose from.
PyCharm* from JetBrains
From the makers of IntelliJ IDEA comes PyCharm*, a fully-featured IDE for developing Python applications. PyCharm packs a ton of features, including:
- Intelligent Python assistance (code completion)
- Support for web development frameworks including Django*, Flask, and Google App Engine* platform
- Integration with Jupyter Notebook
- Ability to run, debug, test, and deploy applications on remote hosts or virtual machines
- A wealth of built-in developer tools
- Even more!
PyCharm comes in two flavors: a free community edition and a more fully-featured professional edition available via a subscription model.
Eclipse* and PyDev
The Eclipse* IDE is an open-source platform that provides an array of convenient and powerful code editing and debugging tools. PyDev is a Python IDE for Eclipse, which can be used for Python, Jython and IronPython development. like PyCharm, PyDev is a fully-featured IDE that includes:
- Django integration
- Code completion
- Type hinting
- Debugger with remote debugging capability
- Application performance analysis using PyVmMonitor
- Even more!
Per the PyDev website, the best way to obtain PyDev is to download and install liClipse.
If you prefer something more lightweight yet still very powerful then a text editor is the way to go. Sublime Text is one such editor. Supporting most programming languages, a handful of featured you’ll see in Sublime Text are:
- Split window editing – edit files side by side, or edit two locations in the one file.
- Distraction free mode – full screen, chrome-free editing, with nothing but your text in the center of the screen.
- Instant project switch – instantly switch between projects.
- Command Palette – search for what you want, without ever having to navigate through the menus or remember obscure key bindings.
- Plugin API – powerful, Python-based plugin API with a built-in Python console to interactively experiment in real-time.
Although Sublime Text is not free, the $70 cost may be worth the investment.
There are very rich development environments built around the Intel® IoT Gateways. These include both desktop and web-based tools. In this section of the guide you’ll learn how to flash the gateway’s operating system, and then program and debug a ‘Hello World’ application using the PyCharm IDE.
These instructions will work on Windows, Mac and linux.
What You Need to Get Going
In order to begin developing on your gateway you’ll need:
- An IoT gateway (for this guide we used the Advantech UTX-3115).
- A USB sensor that measures temperature and relative humidity (for this guide we used the OMEGA Temperature and Humidity USB Sensor).
- An Ethernet cable to plug into your router (this is how the gateway will reach the Internet).
- An IDE or text editor (for this example, we will use the PyCharm IDE).
In order to connect to the gateway itself you’ll need network connectivity. This guide assumes that the gateway is sitting on the same network as your development computer. If network connectivity is unavailable for some reason, you can connect to the gateway via a serial terminal.
Figure 6 shows the setup used in writing this guide:
Getting Started / Hello World
Flashing the OS
In order to upgrade to the latest system version, you’ll need to flash the OS. To do so, follow the steps below:
- Obtain a USB drive that is at least 4 GB in size.
- Download the latest Gateway OS image.
- Unzip the image to a location of your choice on a linux host system.
- Open a terminal window.
- Use the df command to verify the device on which the usb drive is mounted. The df command shows the amount of available disk space being used by file systems.
- Use the following command to copy the OS image to the USB drive:
sudo dd if= <path to recovery image file> of=/dev/sdb bs=4M; sync
- Unplug the USB drive from the host machine and boot the gateway from the USB drive.
- Log in to the gateway (root/root) and execute the following command:
# /sbin/deploytool –d /dev/sda --reset-media -F
- Power off the gateway and then power it back on.
When the gateway comes back up, log in and verify that you are now running the latest version of the OS by logging in to the gateway and viewing the system version number on the dashboard.
First Time Setup
Now that you can log into the gateway, set it up by following the steps below:
XXXX is the last four digits of the MAC address of the gateway’s wireless network adapter (br-lan). You can find this MAC address by booting the gateway, and once it’s up, log in as root and type ‘ifconfig’. Get the last 4 digits from the br-lan adapter.
- Unpack the gateway.
- Plug the Ethernet cable from your router into the necessary Ethernet port. For the Advantech, use the right side (eth0) Ethernet port.
- Connect a VGA or HDMI monitor.
- Optional – connect a mouse and keyboard. (The example in this guide uses a USB hub to plug both into the Advantech. The mouse is completely optional, though a keyboard is recommended).
- Connect the USB Sensor to the gateway.
- Connect the gateway to power and press the power button.
- Once the gateway boots, use your development computer’s wireless network adapter to connect to the gateway’s built-in Wi-Fi using the SSID and password.
- SSID: IDPDK-XXXX
- Password: windriveridp
- Open a browser on your PC – Google Chrome is recommended – and go to http://192.168.1.1. This will open the login page of the Intel® IoT Gateway Developer Hub, a web-based interface to manage sensors and prototype with visual programming tools.
- Log in using the following credentials:
- Username: gwuser
- Password: gwuser
That’s it! You’re ready to develop.
Programming Hello World Using PyCharm
In this section we’ll create a basic “Hello World” application using PyCharm.
- After installing PyCharm, run it. On the splash screen select, “Create New Project”.
- Next, select the location of your project files and the Python interpreter you’ll be using, and click the “Create”button.
Note: It is highly suggested to develop using the same version of Python that is installed on the gateway. By default, both Python 2.7.3 and 3.3.3 are installed on the gateway.
- After you create your project, the project window appears. In this window, right-click the name of your project and select “New File” to create a new blank file. Name the file “hello_world.py”.
- Once the ‘hello_world.py’ file is created, add the following code and save the file:
# encoding: utf-8
"""The standard hello world script."""
print "Hello World!"
- To ensure your Python script runs, right-click the file and select “Run ‘hello_world’”.
- Once the script runs you can view the output in the console that appears at the bottom of the IDE.
Next we want to run the script on the gateway. To do that we will first copy the file from our development computer to the gateway using a secure copy (SCP) client application on the development computer.
- First, download and install the latest version of WinSCP. When installing it, I recommend selecting the Commander user interface style, as it allows drag-and-drop transferring of files.
- At the end of the installation, choose to open WinSCP.
- Log in to the gateway by configuring a login in WinSCP. Ensure that the file protocol you are using is ‘SCP’. Your settings will look similar to this:
- With the connection created, next we need a folder for the script. Click the Login button to log in to the gateway. This will connect you to the root folder on the gateway and open the folder in the right-hand pane.
- Next, right-click the right pane and select New > Directory.
- Name the directory ‘apps’.
- Double-click on the apps directory to open it up and create another directory named “helloworld” using the same steps. Double-click on the “helloworld” directory to open it if it isn’t already open.
- In the left-hand pane of the WinSCP application, navigate to the folder where you create the hello_world.py file. Drag-and-drop the file to the right-hand pane to copy the file. You should see something like this:
- With the file on your gateway, on your development computer, open a command prompt and use the ssh command to securely connect to the gateway:
- Next, change into the helloworld directory you created and use the python command to run the file:
- You should see the output of the script:
Intel provides a number of how-to articles and videos on using the IoT Gateway on the IoT section of the Intel® Developer Zone. The following videos and articles will help you get started:
- Intel® IoT Gateway Developer Hub: Save and Deploy an OS Image – Once IoT Gateway software development is complete, learn how to save a copy of the gateway’s OS and software and deploy it to other gateways.
- Intel® Iot Gateways: Pulling Data From a Temperature Sensor Using a Python Script – create and install a custom Wind River® linux 7 OS image that includes the software and libraries necessary to interface a Phidgets temperature sensor with your Intel® IoT Gateway using Python.
- Intel® IoT Gateways: Publishing Data to an MQTT Broker Using Python – publish data from an Intel® IoT Gateway to an MQTT broker (server) using a Python Script.
Additionally, the following guides contain useful information and instructions:
- Intel® IoT Gateway Technology: Security Best Practices – Learn how to access and manage the security features on your gateway.
- Intel® IoT Gateway Technology: Gateway Installation Guide – Learn how to connect your gateway hardware, connect it to a network, update the evaluation OS, and access the Intel® IoT Gateway Developer Hub.
Debugging Hello World with PyCharm
To debug your Hello World application, first open up the Hello World project you created in a previous step. Run it as before to ensure that everything is in working order, following the steps below:
- To debug the application, create a breakpoint on the line which you want debugging to start by clicking to the left of the code. When the breakpoint is created, you will see a solid red dot beside the line.
- With the breakpoint created, click the Debug button in the top right corner (it’s the button that looks like a bug).
- PyCharm will run your Python application to the breakpoint at which the debug panel becomes available.
- The debug panel has a lot of useful information about the state of your program at which it is stopped, including all of the objects created thus far.
- Once you are finished with the debug panel, click the Resume Program Execution button on the left side of the debug panel, second from the top (it looks like a green arrow pointing to the right).
Where to Go From Here
At this point, you’ve accomplished quite a bit! In this guide you’ve:
- Learned how to select the gateway that’s appropriate for your application.
- Discovered a number of IDE and text editor options.
- Learned how to flash the gateway and upgrade it to the latest system version.
- Set up the gateway for development.
- Written a simple hello world application in Python, copied it to the gateway, ran it and debugged it.
As a next step, read and implement the lessons in the articles and videos listed in the Coding Basics section. These will show you how to pull data from a sensor, as well as publish the data you capture. Once you have the data, there are a wealth of options to both analyze and visualize the data.