Intel IoT Gateways: Publishing Data to an MQTT Broker Using Python



Similar documents
Intel Internet of Things (IoT) Developer Kit

Intel IoT Gateway Software Development Kit SK100

iscsi Quick-Connect Guide for Red Hat Linux

Intel Solid-State Drive Pro 2500 Series Opal* Compatibility Guide

The Case for Rack Scale Architecture

with PKI Use Case Guide

Intel Cloud Builder Guide: Cloud Design and Deployment on Intel Platforms

Intel Media SDK Library Distribution and Dispatching Process

Creating Overlay Networks Using Intel Ethernet Converged Network Adapters

Hetero Streams Library 1.0

Intel Unite. User Guide

Intel Network Builders: Lanner and Intel Building the Best Network Security Platforms

Intel SSD 520 Series Specification Update

Intel Identity Protection Technology Enabling improved user-friendly strong authentication in VASCO's latest generation solutions

Intel Identity Protection Technology (IPT)

Intel Retail Client Manager

Intel Retail Client Manager Audience Analytics

Life With Big Data and the Internet of Things

Intel Simple Network Management Protocol (SNMP) Subagent v6.0

System Event Log (SEL) Viewer User Guide

Intel Core i5 processor 520E CPU Embedded Application Power Guideline Addendum January 2011

Intel Remote Configuration Certificate Utility Frequently Asked Questions

How To Reduce Pci Dss Scope

DIY Device Cloud Documentation

BSP for Windows* Embedded Compact* 7 and Windows* Embedded Compact 2013 for Mobile Intel 4th Generation Core TM Processors and Intel 8 Series Chipset

Intel Desktop Board D945GCPE

How to Configure Intel X520 Ethernet Server Adapter Based Virtual Functions on Citrix* XenServer 6.0*

Intel Desktop Board DG41BI

Intel System Event Log (SEL) Viewer Utility

Intel Active Management Technology Embedded Host-based Configuration in Intelligent Systems

Intel Desktop Board DG43RK

Intel Cloud Builder Guide to Cloud Design and Deployment on Intel Xeon Processor-based Platforms

Intel vpro Technology. How To Purchase and Install Symantec* Certificates for Intel AMT Remote Setup and Configuration

Affordable Building Automation System Enabled by the Internet of Things (IoT)

WIND RIVER INTELLIGENT DEVICE PLATFORM XT

Intel Desktop Board D945GCPE Specification Update

Mobility Manager 9.5. Installation Guide

Intel HTML5 Development Environment. Tutorial Building an Apple ios* Application Binary

Intel Desktop Board DG41TY

Intel Desktop Board DG31PR

Intel Desktop Board DP55WB

Intel HTML5 Development Environment Article Using the App Dev Center

Intel and Qihoo 360 Internet Portal Datacenter - Big Data Storage Optimization Case Study

User Experience Reference Design

Intel Desktop Board DQ965GF

Intel Integrated Native Developer Experience (INDE): IDE Integration for Android*

Customizing Boot Media for Linux* Direct Boot

RAID and Storage Options Available on Intel Server Boards and Systems

Allscripts Professional EHR

Intel Solid-State Drive Data Center Tool User Guide Version 1.1

Intel HTML5 Development Environment. Tutorial Test & Submit a Microsoft Windows Phone 8* App (BETA)

Intel HTML5 Development Environment. Article - Native Application Facebook* Integration

EHCI Removal from 6 th Generation Intel Core Processor Family Platform Controller Hub (PCH)

Intel Desktop Board DQ43AP

Intel Storage System SSR212CC Enclosure Management Software Installation Guide For Red Hat* Enterprise Linux

Intel vpro Technology. How To Purchase and Install Go Daddy* Certificates for Intel AMT Remote Setup and Configuration

McAfee Firewall for Linux 8.0.0

Flow Publisher v1.0 Getting Started Guide. Get started with WhatsUp Flow Publisher.

Cloud based Holdfast Electronic Sports Game Platform

Intel Retail Client Manager

Intel: a Thought Leader Helping IoT Scale Out

Intel Desktop Board DG41WV

CA Performance Center

Intel Ethernet and Configuring Single Root I/O Virtualization (SR-IOV) on Microsoft* Windows* Server 2012 Hyper-V. Technical Brief v1.

NFV Reference Platform in Telefónica: Bringing Lab Experience to Real Deployments

This guide explains how to install an Intel Solid-State Drive (Intel SSD) in a SATA-based desktop or notebook computer.

Moxa Device Manager 2.3 User s Manual

RAID and Storage Options Available on Intel Server Boards and Systems based on Intel 5500/5520 and 3420 PCH Chipset

Intel Desktop Board D945GCL

Intel Service Assurance Administrator. Product Overview

Revision History. Revision Revision History Date

Intel Data Center Manager. Data center IT agility and control

Intel Data Direct I/O Technology (Intel DDIO): A Primer >

Intel Small Business Advantage (Intel SBA) Release Notes for OEMs

VNF & Performance: A practical approach

Intel Management Engine BIOS Extension (Intel MEBX) User s Guide

Intel Entry Storage System SS4000-E

Page Modification Logging for Virtual Machine Monitor White Paper

Intel Desktop Board DG965RY

Intel Cyber Security Briefing: Trends, Solutions, and Opportunities. Matthew Rosenquist, Cyber Security Strategist, Intel Corp

Creating a DUO MFA Service in AWS

Intel Desktop Board D101GGC Specification Update

Intel Desktop Board DQ35JO

Specification Update. January 2014

Fast, Low-Overhead Encryption for Apache Hadoop*

Hadoop Applications on High Performance Computing. Devaraj Kavali

Intel RAID RS25 Series Performance

Intel Platform Controller Hub EG20T

Intel Gateway Solutions for the Internet of Things. Intel Quark SoC X1000 Applications Marketing Seminar Anaheim, California Oct.

Intel Media Server Studio Professional Edition for Windows* Server

Intel Data Migration Software

Resetting USB drive using Windows Diskpart command

SyAM Software* Server Monitor Local/Central* on a Microsoft* Windows* Operating System

Different NFV/SDN Solutions for Telecoms and Enterprise Cloud

Intel Cloud Builders Guide to Cloud Design and Deployment on Intel Platforms

Transcription:

Intel IoT Gateways: Publishing Data to an MQTT Broker Using Python Getting Started Guide September 2015 Order No.: 333067-001

By using this document, in addition to any agreements you have with Intel, you accept the terms set forth below. You may not use or facilitate the use of this document in connection with any infringement or other legal analysis concerning Intel products described herein. You agree to grant Intel a non-exclusive, royalty-free license to any patent claim thereafter drafted which includes subject matter disclosed herein. No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted by this document. All information provided here is subject to change without notice. Contact your Intel representative to obtain the latest product specifications and roadmaps. Intel may make changes to specifications and product descriptions at any time, without notice. Designers must not rely on the absence or characteristics of any features or instructions marked "reserved" or "undefined". Intel reserves these for future definition and shall have no responsibility whatsoever for conflicts or incompatibilities arising from future changes to them. The information here is subject to change without notice. Do not finalize a design with this information. The products described in this document may contain design defects or errors known as errata which may cause the product to deviate from published specifications. Current characterized errata are available on request. Copies of documents which have an order number and are referenced in this document, or other Intel literature, may be obtained by calling 1-800-548-4725, or go to: http://www.intel.com/design/literature.htm Intel processor numbers are not a proxy for performance. Processor numbers differentiate features within a processor family, not across different processor families. Learn more at: http://www.intel.com/products/processor_number/ Intel, the Intel logo, Intel Atom, Intel Core and Intel Quark, are trademarks of Intel Corporation in the U.S. and/or other countries. Wind River is a trademark of Wind River Systems, Inc. *Other names and brands may be claimed as the property of others. Copyright 2015, Intel Corporation. All rights reserved. Getting Started Guide September 2015 2 Order No.: 333067-001

Revision History TRISOFT "Program Name" NOT SET Revision History Date Revision Description September 2015 001 Initial public release September 2015 Getting Started Guide Order No.: 333067-001 3

TRISOFT "Program Name" NOT SET Contents Contents Revision History...3 1.0 Introduction... 5 2.0 Development Computer Setup...7 3.0 Gateway Setup...11 4.0 Sending and Receiving MQTT Messages... 13 5.0 Conclusion... 15 6.0 Development Suggestions...16 Getting Started Guide September 2015 4 Order No.: 333067-001

Introduction TRISOFT "Program Name" NOT SET 1.0 Introduction Note: For an online version of this document, see https://did-kahillx.idzdev.or.intel.com/enus/usegateway-mqtt. Applicable to These Intel IoT Gateway Products Intel IoT Gateways based on Intel Atom processors. Intel IoT Gateways based on Intel Core processors. How These Instructions Help You These instructions will guide you through the steps to publish data from an Intel IoT Gateway to an MQTT broker (server) using a Python* Script. On a Development Computer you will subscribe to an MQTT message test topic on an independent MQTT message broker (server). After subscribing, any messages published to the topic on the remote MQTT broker are sent to the Development Computer. You will do this by: 1. Installing Python MQTT software on a Development Computer. 2. Setting up an MQTT broker. 3. Installing Python MQTT software on your Gateway. 4. Creating and running a Python script to send MQTT messages from the Gateway, through the broker, to the Development Computer. The figure below shows the connections between the Gateway, the MQTT broker and the Development Computer. Gateway MQTT Broker Development Computer Publish message to a Topic Subscribe to a Topic Send Message What is MQTT? MQTT is a messaging protocol that can be used to transmit data from a Gateway to a local server or cloud server. The server relays the data to remote clients that are subscribed to receive it. From http://en.wikipedia.org/wiki/mqtt: " MQTT (formerly MQ Telemetry Transport) is a publish-subscribe based light weight messaging protocol for use on top of the TCP/IP protocol. It is designed for connections with remote locations where a small code footprint is required and/or September 2015 Getting Started Guide Order No.: 333067-001 5

TRISOFT "Program Name" NOT SET Introduction network bandwidth is limited. The publish-subscribe messaging pattern requires a message broker. The broker is responsible for distributing messages to interested clients based on the topic of a message. There are several MQTT brokers available. They vary in their feature set and some of them implement additional features on top of the standard MQTT functionality. For a comparison of several brokers, go here: https://github.com/mqtt/mqtt.github.io/wiki/ server-support" What is Mosquitto? Mosquitto is an open source (BSD licensed) message broker that implements the MQTT protocol versions 3.1 and 3.1.1. This guide gives examples of using Mosquitto to setup an MQTT message broker on a local network. See http://mosquitto.org for details. Python and MQTT The Python programming language can be used to generate MQTT messages. You might choose to use Python if it is simple to incorporate the Python code into your primary gateway application, and if you are familiar with Python. Python is sometimes convenient to use because it does not need to be compiled; it is interpreted on the Gateway at run time. For more information on Python, see https://www.python.org/. What is Paho? From: http://www.eclipse.org/paho/clients/python/: "The Paho Python Client provides a client class with support for both MQTT v3.1 and v3.1.1 on Python 2.7 or 3.x. It also provides some helper functions to make publishing one-off messages to an MQTT server very straightforward." Getting Started Guide September 2015 6 Order No.: 333067-001

Development Computer Setup TRISOFT "Program Name" NOT SET 2.0 Development Computer Setup In this section, you will set up your Development Computer to subscribe to an MQTT message test topic on an independent MQTT message broker. After your Development Computer has subscribed to a topic, messages published to the topic are sent to the Development Computer. To setup your Development Computer you must: 1. Install the Paho Python MQTT software. 2. Recommended: Connect your Development Computer to a test MQTT broker to confirm your connections and the functionality of a test script. 3. Choose and configure your MQTT broker. Perform the steps in this section on your Development Computer. Prerequisites Your development environment must be configured. See https:// software.intel.com/en-us/setup-idp-developmenttools. You have an Internet connection from your Development Computer Your Development Computer is on the same network as your MQTT broker if you are using an internal network, or if you are using an existing MQTT broker on the Internet, your Internet firewall must allow traffic on ports 1883, 8883, 8884, and 8885. You understand the options and connection methods for your MQTT broker. In order of preference, your options are: Existing broker on either a local network or on the Internet (Cloud): You will use an existing broker on your network or on the Internet (Cloud). This broker is ready to send and receive messages and you have the ability to add a topic to this broker it a suitable topic is not already available. New broker on a local network using Python Paho: You will use a terminal window on your Development Computer as a local network MQTT broker using Paho Python libraries. New broker on a local network using mosquitto: You will use a terminal window on your Development Computer as a local network MQTT broker using mosquitto. Install the Paho Python MQTT Software Perform these steps on your Development Computer. 1. Download the latest Paho MQTT tar.gz file from http://git.eclipse.org/c/paho/ org.eclipse.paho.mqtt.python.git/. The latest file is at the top of the list under the heading Tag. As of September 1, 2015, this file is called org.eclipse.paho.mqtt.python-1.1.tar.gz. 2. Put the file in a directory of your choice. September 2015 Getting Started Guide Order No.: 333067-001 7

TRISOFT "Program Name" NOT SET Development Computer Setup 3. Extract the file. For example: tar -xf org.eclipse.paho.mqtt.python-1.1.tar.gz 4. The contents extract to a directory with a name similar to the tar.gz file name. Change to this directory, replacing the directory name if necessary. For example: cd org.eclipse.paho.mqtt.python-1.1 5. Run the Python setup and install command: python setup.py install The software is now installed on your Development Computer. Recommended: Perform Initial Connection Tests While not required, it is best to test the Paho Python scripts and their network connections using a MQTT test broker. This is accomplished by subscribing to an active MQTT topic on the test broker. For a simple test, use test.mosquitto.org to confirm a proper network connection and broker subscription. 1. Go to the Paho MQTT Python examples directory: cd org.eclipse.paho.mqtt.python/examples 2. Modify the sub.py script to change the mqttc.connect and mqttc.subscribe lines: mqttc.connect("test.mosquitto.org", 1883, 60) mqttc.subscribe("#", 0) In this script, the following apply: mqttc.connect test.mosquitto.org: Name of MQTT broker you will connect to. 1883: Port used for the MQTT messages. 60: Timeout in seconds. mqttc.subscribe #: Wildcard character that indicates you are subscribing to all topics o the broker. 0: Quality of Service. For information about Quality of Service options, see https://www.eclipse.org/paho/files/mqttdoc/cclient/qos.html. 3. Execute the script: python sub.py If the network connections and the Python scripts are working correctly, then the test.mosquitto.org broker will send multiple lines of data. The content of the data is not relevant. If you do not receive data lines, double-check the network connections and the lines you edited in the script. Getting Started Guide September 2015 8 Order No.: 333067-001

Development Computer Setup TRISOFT "Program Name" NOT SET 4. Press Ctrl-C to stop the script. Choose Your MQTT Broker Choose one of the following options for your MQTT broker: Existing broker on either a local network or on the Internet (Cloud): This document does not provide specific steps for this option since we are not familiar with your specific environment. Work with your network administrators for the name of your broker, the topic your Development Computer should subscribe to, other assistance connecting to this broker. New broker on a local network using Python Paho: You will use a terminal window on your Development Computer as a local network MQTT broker, using Paho Python libraries. For setup instructions, see Set up an MQTT Broker on a Local Network Using Python Paho. New broker on a local network using Mosquitto: You will use a terminal window on your Development Computer as a local network MQTT broker, using mosquitto. For setup instructions, see Set up an MQTT Broker on a Local Network Using Mosquitto. Set up an MQTT Broker on a Local Network Using Python Paho You have three options for your MQTT broker. Use the steps in this section only if your goal is to use your Development Computer as a local network MQQT broker. This option uses a terminal window with the Paho Python libraries that were installed with the Paho Python MQTT software. To start a broker process on the Development Computer: 1. Open a new Linux terminal window. The broker will run in this window. You can still receive MQTT messages on this Development Computer, using a different terminal window. 2. Go to the Paho examples directory: cd org.eclipse.paho.mqtt.python-1.1/examples 3. Modify the sub.py script to change the mqttc.connect and mqttc.subscribe lines: mqttc.connect("localhost",1883, 60) mqttc.subscribe("mytopic", 0) In this script, the following apply: mqttc.connect localhost: An alias for the Development Computer's local loopback IP address (typically 127.0.0.1). 1883: Network port used for MQTT messages. 60: Timeout in seconds. mqttc.subscribe mytopic: Name of the MQTT topic. 0: Quality of Service. September 2015 Getting Started Guide Order No.: 333067-001 9

TRISOFT "Program Name" NOT SET Development Computer Setup 4. Connect the Development Computer to the same network that the broker is on. 5. Run the sub.py script in a different terminal window: python sub.py The local broker is now running and subscribed to mytopic. You can publish messages to it and this broker will send the messages to the subscribers (your Development Computer). Continue to Gateway Setup. Set up an MQTT Broker on a Local Network Using Mosquitto You have three options for your MQTT broker. Use these steps only if your goal is to use your Development Computer as a local network test broker using the open source application called mosquitto. Note: For details about mosquitto use and syntax, see http://mosquitto.org, or use <mosquitto_command> --help, where <mosquitto_command> is the command that you need help with. To start a broker process on the Development Computer: 1. Open a new Linux terminal window. The broker will run in this window. You can still receive MQTT messages on this Development Computer. The MQTT messages will be received in a different terminal window. 2. Install mosquitto if you do not already have it installed. sudo apt-get install mosquitto 3. Create a topic using the mosquitto_sub command. mosquitto_sub -d -h localhost -t mytopic In this script, the following apply: -d: Enable debug messages. -h localhost: An alias to the development computer's local loopback IP address (typically 127.0.0.1). -t mytopic: Name of the MQTT topic. The mosquitto broker is now running and subscribed to mytopic. You can publish messages to it and this broker will send the messages to the subscribers (your Development Computer). Continue to Gateway Setup. Getting Started Guide September 2015 10 Order No.: 333067-001

Gateway Setup TRISOFT "Program Name" NOT SET 3.0 Gateway Setup To setup your Gateway, you must: 1. Add Python and Mosquitto to the Gateway operating system. 2. Install the Paho Python MQTT software on the Gateway. 3. Recommended: Connect your Gateway to a test MQTT broker to confirm your connections and the functionality of a test script. Prerequisites Your Gateway is set up according to https://software.intel.com/en-us/ SetupGateway-hardware. You have an Internet connection from your Gateway. Your Gateway is on the same network as your MQTT broker if you are using an internal network. Your Gateway is connected to the Internet if you are using an MQTT broker on the Internet. Your Internet firewall must allow traffic on ports 1883, 8883, 8884, and 8885. Add Python and Mosquitto to the Gateway Operating System Begin these steps on your Development Computer. 1. When you set up your development environment, you created a script file called config.sh in the $HOME/Project directory on your Development Computer. Edit config.sh to check for and include the following switches. These changes are necessary to include Python and mosquitto in your Gateway operating system build: --enable-addons=wr-idp --with-template=feature/mqtt 2. Configure the project, build the Gateway Operating System, and install it on your Gateway. For instructions, see https://software.intel.com/en-us/setupgatewayhardware. Install the Paho Python MQTT Software Perform these steps directly on your Gateway. 1. Download the latest Paho MQTT tar.gz file from http://git.eclipse.org/c/paho/ org.eclipse.paho.mqtt.python.git/. The latest file is at the top of the list under the heading Tag. As of September 1, 2015, this file is called org.eclipse.paho.mqtt.python-1.1.tar.gz. 2. Put the file in a directory of your choice. 3. Extract the file. For example: September 2015 Getting Started Guide Order No.: 333067-001 11

TRISOFT "Program Name" NOT SET Gateway Setup tar -xf org.eclipse.paho.mqtt.python-1.1.tar.gz 4. The contents extract to a directory with a name similar to the tar.gz file name. Change to this directory, replacing the directory name if necessary. For example: cd org.eclipse.paho.mqtt.python-1.1 5. Run the Python setup and install command: python setup.py install The software is now installed on your Gateway. Recommended: Perform Initial Connection Tests While not required, it is best to test the Paho Python scripts and their network connections using a MQTT test broker. This is accomplished by subscribing to an active MQTT topic on the test broker. For a simple test, use test.mosquitto.org to confirm a proper network connection and broker subscription. 1. Go to the Paho MQTT Python examples directory: cd org.eclipse.paho.mqtt.python/examples 2. Modify the sub.py script to change the mqttc.connect and mqttc.subscribe lines: mqttc.connect("test.mosquitto.org", 1883, 60) mqttc.subscribe("#", 0) In this script, the following apply: mqttc.connect test.mosquitto.org: Name of MQTT broker you will connect to. 1883: Port used for the MQTT messages. 60: Timeout in seconds. mqttc.subscribe #: Wildcard character that indicates you are subscribing to all topics o the broker. 0: Quality of Service. For information about Quality of Service options, see https://www.eclipse.org/paho/files/mqttdoc/cclient/qos.html. 3. Execute the script: python sub.py If the network connections and the Python scripts are working correctly, then the test.mosquitto.org broker will send multiple lines of data. The content of the data is not relevant. If you do not receive data lines, double-check the network connections and the lines you edited in the script. 4. Press Ctrl-C to stop the script. Getting Started Guide September 2015 12 Order No.: 333067-001

Sending and Receiving MQTT Messages TRISOFT "Program Name" NOT SET 4.0 Sending and Receiving MQTT Messages With your Development Computer and Gateway configured for MQTT and your MQTT broker set up, you are ready to create and run a Python script to publish MQTT messages. These steps use Paho Python examples to create an initial script that you can later modify to meet your needs. Create the Python script either directly on your Gateway or on your Development Computer. If you create the script on your Development Computer, copy it to your Gateway to run it. Security Considerations e aware of the following when creating and running your Python scripts: If you use the Python interpreter, IMA security is not an issue. IMA security allows the interpreter to run new scripts. If you do not use the Python interpreter and you have IMA security enabled, then you must sign the script before you can run it. If you have McAfee Embedded Control enabled on your Gateway, you must add your script to your Whitelist. Create and Run a Python MQTT Publish Script 1. Go to the Paho examples directory: cd org.eclipse.paho.mqtt.python-1.1/examples 2. Copy and rename the pub-single.py script: cp pub-single.py my-pub.py 3. Edit my-pub.py to change the last line so it points to the MQTT broker and the topic that you set up in Development Computer Setup: publish.single("mytopic","hello", hostname="localhost") In this script, the following apply: "mytopic": Topic name on the broker. "Hello": Message content. "localhost": MQTT broker. 4. If you edited your Gateway script on your Development Computer, copy it to your Gateway. For help copying scripts to your Gateway, see Link to Software Dev Guide. 5. If you have IMA security enabled and you are not using the Python interpreter, sign the script on your Gateway. September 2015 Getting Started Guide Order No.: 333067-001 13

TRISOFT "Program Name" NOT SET Sending and Receiving MQTT Messages 6. If you have McAfee Embedded Control enabled on your Gateway, add the script to the Whitelist. 7. Run the Python script to publish the message to the "mytopic" topic on your MQTT broker: python my-pub.py Receive MQTT Messages on Your Development Computer When your Gateway publishes a message to a topic on an MQTT broker, the broker sends that message to all computers that are subscribed to the topic on that MQTT broker. You just issued the command python my-pub.py on your Gateway. This resulted in a message being sent to your MQTT broker and from there to your Development Computer. This should now be displayed on your Development Computer as: mytopic 0 Hello In this message, the following apply: mytopic: Topic name. 0: Quality of Service. Hello: The message. Getting Started Guide September 2015 14 Order No.: 333067-001

Conclusion TRISOFT "Program Name" NOT SET 5.0 Conclusion Congratulations! Using the Python programming language, you sent an MQTT message (Hello) from the Gateway, through the topic (mytopic) on the MQTT broker (localhost), and then to a Development Computer that subscribed to that MQTT topic. (mytopic) Continue to Development Suggestions for ideas to extend the exercises in this guide. September 2015 Getting Started Guide Order No.: 333067-001 15

TRISOFT "Program Name" NOT SET Development Suggestions 6.0 Development Suggestions Consider the following ideas to enhance the use of MQTT messages in your environment: Get data from your Gateway sensors and publish sensor data to an MQTT broker. Push messages you receive into a local database or to a Cloud database. Increase the complexity of the messages you send. Experiment with Paho Python functions to send multiple messages with a single command. Add security features to MQTT message communication through encryption, whitelisting, or other means. Getting Started Guide September 2015 16 Order No.: 333067-001