Click here to Skip to main content
15,946,342 members
Articles / Internet of Things
Article

Getting Started with Intel® IoT Gateways - Python

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
18 Nov 2016CPOL18 min read 12K   4  
This article was written for Python. To get started using JavaScript, see Getting Started with Intel® IoT Gateways - Python

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

Get access to the new Intel® IoT Developer Kit, a complete hardware and software solution that allows developers to create exciting new solutions with the Intel® Galileo and Intel® Edison boards. Visit the Intel® Developer Zone for IoT.

Introduction

The Internet of Things (IoT) market is booming. Gartner forecasts that in 201t6, 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.

Image 1

Figure 1: A high-level overview of an IoT solution

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:

  1. Robust and secure access to the Internet or Wide Area Network (WAN).
  2. 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:

  1. Communications and connectivity
  2. Scalability
  3. Security
  4. Manageability

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.

Scalability

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.

Security

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

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.

Usage Scenarios

Commercial

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.

Image 2

Figure 2: An Intel example of an end-to-end commercial IoT deployment

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.

Residential

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:

  1. The type of Internet connectivity available (wired, wireless, cellular).
  2. 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:

Processors

  • 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
  • Bluetooth*
  • CAN bus
  • ZigBee*
  • 6LoWPAN
  • GPRS
  • 2G/3G/LTE
  • Analog and digital I/O
  • RS-232

Operating Systems

  • 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  

Software Overview

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 - 7.0.0.13 - 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<br>
>> 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

Image 3

Figure 3: 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

Image 4

Figure 4: 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
  • Refactoring
  • 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.

Sublime Text

Image 5

Figure 5: Sublime Text

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.

Development Environment

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).
  • Power
  • 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:

Image 6

Figure 6: IoT Solution Diagram

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:

  1. Obtain a USB drive that is at least 4 GB in size.
  2. Download the latest Gateway OS image.
  3. Unzip the image to a location of your choice on a linux host system.
  4. Open a terminal window.
  5. 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.
  6. 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
  7. Unplug the USB drive from the host machine and boot the gateway from the USB drive.
  8. Log in to the gateway (root/root) and execute the following command:
    # /sbin/deploytool –d /dev/sda --reset-media -F
  9. 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.

  1. Unpack the gateway.
  2. Plug the Ethernet cable from your router into the necessary Ethernet port. For the Advantech, use the right side (eth0) Ethernet port.
  3. Connect a VGA or HDMI monitor.
  4. 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).
  5. Connect the USB Sensor to the gateway.
  6. Connect the gateway to power and press the power button.
  7. 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
  8. 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.
  9. 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.

  1. After installing PyCharm, run it. On the splash screen select, "Create New Project".

    Image 7

    Figure 7: PyCharm main menu
  2. 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.

    Image 8

    Figure 8: Pycharm new project
  3. 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".

    Image 9

    Figure 9: Pycharm new project > new file
  4. Once the ‘hello_world.py’ file is created, add the following code and save the file:
    #!/usr/bin/env python<br>
    	# encoding: utf-8<br>
    	"""The standard hello world script."""<br>
    	print "Hello World!"
  5. To ensure your Python script runs, right-click the file and select "Run ‘hello_world’".

    Image 10

    Figure 10: Run "hello_world"
  6. Once the script runs you can view the output in the console that appears at the bottom of the IDE.

    Image 11

    Figure 11: hello_world output

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.

  1. 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.

    Image 12

    Figure 12: WinSCP Setup
  2. At the end of the installation, choose to open WinSCP.
  3. 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:

    Image 13

    Figure 13: Configure a Login with WinSCP
  4. 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.
  5. Next, right-click the right pane and select New > Directory.
  6. Name the directory ‘apps’.
  7. 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.
  8. 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:

    Image 14

    Figure 14: WinSCP hello_world.py file folder
  9. 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:
    ssh root@192.168.1.4
  10. Next, change into the helloworld directory you created and use the python command to run the file:
    cd apps\helloworld<br>
    	python hello_world.py
  11. You should see the output of the script:

    Image 15

    Figure 15: Script output

Coding Basics

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:

Additionally, the following guides contain useful information and instructions:

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:

  1. 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.

    Image 16

    Figure 16: hello_world.py breakpoint
  2. With the breakpoint created, click the Debug button in the top right corner (it’s the button that looks like a bug).

    Image 17

    Figure 17: Debugging hello_world.py
  3. PyCharm will run your Python application to the breakpoint at which the debug panel becomes available.

    Image 18

    Figure 18: Running hello_world.py to breakpoint
  4. 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.
  5. 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).

    Image 19

    Figure 19: Resume program execution of hello_world.py

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
You may know us for our processors. But we do so much more. Intel invents at the boundaries of technology to make amazing experiences possible for business and society, and for every person on Earth.

Harnessing the capability of the cloud, the ubiquity of the Internet of Things, the latest advances in memory and programmable solutions, and the promise of always-on 5G connectivity, Intel is disrupting industries and solving global challenges. Leading on policy, diversity, inclusion, education and sustainability, we create value for our stockholders, customers and society.
This is a Organisation

42 members

Comments and Discussions

 
-- There are no messages in this forum --