Difference between revisions of "Lantronix Python Programmers Guide"

From Lantronix Wiki!
Jump to navigation Jump to search
m
Line 37: Line 37:
 
In your favorite text editor, create a file named ''''helloworld.py'''', with the following contents:
 
In your favorite text editor, create a file named ''''helloworld.py'''', with the following contents:
  
  <nowiki>
+
  # Hello World - a simple text output program
# Hello World - a simple text output program
+
def main():
def main():
+
print 'Hello World!'
print 'Hello World!'
+
 
+
main()
main()</nowiki>
 
  
 
==== Transfer Program Onto Lantronix Device ====
 
==== Transfer Program Onto Lantronix Device ====
Line 80: Line 79:
 
  #'''ftp ''<Lantronix Device IP Address>'''''
 
  #'''ftp ''<Lantronix Device IP Address>'''''
 
  ''-- login --''
 
  ''-- login --''
  ftp> ''put helloworld.py helloworld.py''
+
  ftp> '''put helloworld.py helloworld.py'''
  ftp> ''exit''
+
  ftp> '''exit'''
 +
 
 +
For Python module packages, use FTP to transfer the tar.gz package tarball as shown below.
  
 +
#'''ftp ''<Lantronix Device IP Address>'''''
 +
''-- login --''
 +
ftp> '''put ''<python_package>.tar.gz <python_package>.tar.gz'''''
 +
ftp> '''exit'''
  
 
==== Installing Python Module Packages ====
 
==== Installing Python Module Packages ====
 +
To install the packages, using CLI, please follow the steps provided below.
 +
 +
#'''telnet ''<Lantronix Device IP Address>'''''
 +
''--login (if enabled)—''
 +
> '''enable'''
 +
(enable)# '''configure'''
 +
(config)# '''applications'''
 +
(config-applications)# '''python install ''<zip|tar.gz file>'''''
 +
(config)# '''exit'''
 +
(enable)# '''exit'''
  
 
==== Configure Python Programs ====
 
==== Configure Python Programs ====
 
To configure uploaded Python programs to run, a series of commands needs to be entered in the CLI. The example below shows connecting to the CLI via telnet and assumes that Python program '''''helloworld.py''''' was uploaded using the earlier steps.
 
To configure uploaded Python programs to run, a series of commands needs to be entered in the CLI. The example below shows connecting to the CLI via telnet and assumes that Python program '''''helloworld.py''''' was uploaded using the earlier steps.
  
<tt>
 
 
  #'''telnet ''<Lantronix Device IP Address>'''''
 
  #'''telnet ''<Lantronix Device IP Address>'''''
 
  ''--login (if enabled) --''
 
  ''--login (if enabled) --''
Line 102: Line 116:
 
  (config)# '''exit'''
 
  (config)# '''exit'''
 
  (enable)# '''exit'''
 
  (enable)# '''exit'''
</tt>
 
 
   
 
   
 
==== Running Python Programs ====
 
==== Running Python Programs ====
 +
In order to run the configured Python program the <tt>run python</tt> is used. The ''stdout'' is directed towards the output file (if configured). The Python program is not a sub-process of the CLI process and run in the background. As a result it does not inherit the file descriptors of the CLI process even though the run command is issued via the CLI.
 +
This is generally not considered a limitation as most Python programs for connected applications would run independently and use modules that provide network connectivity protocol implementations or specific hardware abstraction functions.
 +
 
 +
#'''telnet ''<Lantronix device IP addresss>'''''
 +
''--login (if enabled)—''
 +
> '''enable'''
 +
(enable)# '''configure'''
 +
(config)# '''applications'''
 +
(config-applications)# '''run python ''<instance>'''''
 +
(config)# '''exit'''
 +
(enable)# '''exit'''
  
=== Using Web UI ===
+
==== Additional Available Commands ====
 +
There are additional commands available for the following functions:
 +
 
 +
* <code>'''parameters ''<text>'''''</code><br />String of parameters as arguments to the script when it runs
 +
* <code>'''output ''<text>'''''</code><br />Output file name for capturing ''stdout'' and debugging messages (print)
 +
* <code>'''onstart enable'''</code><br />Enable script to run on device bootup/startup
 +
* <code>'''onstart disable'''</code><br />Disable script to run on device bootup/startup
 +
* <code>'''onshutdown enable'''</code><br />Enable script to run on device shutdown
 +
* <code>'''onshutdown disable'''</code><br />Disable script to run on device shutdown
 +
 
 +
 
 +
=== Using Web Configuration UI ===
 +
The Web Configuration UI provides an easy to follow point-and-click mechanism for device configuration. Python programs can be loaded, configured and run through the Web Configuration UI as described below.
 +
 
 +
The Web Configuration UI is accessed via the Web browser by logging in using the configured password for the '''admin''' user.
  
 
==== Load Python Programs ====
 
==== Load Python Programs ====
 +
In order to upload the Python programs or module packages to the Lantronix device:
 +
* Navigate to the '''Filesystem''' menu
 +
* Select the '''Browse''' tab.
 +
* Select and upload Python program using the '''Upload File''' section.
 +
 +
[[File:
  
 
==== Installing Python Module Packages ====
 
==== Installing Python Module Packages ====

Revision as of 05:37, 18 November 2014


Introduction

Overview

This guide introduces developers to the support for the Python runtime on Lantronix line of embedded and ready-to-deploy Intelligent Gateway products. It describes how to load and run Python programs on Lantronix devices, reviews available Python modules including those that expose Lantronix specific APIs and describes sample Python programs for common use cases that provide a head-start in customizing Lantronix Intelligent Gateway platforms.


What is Python?

Python is a programming language that lets you work more quickly and integrate your systems more effectively. It is a dynamically typed, object-oriented programming language offering an elegant syntax and an extendable Python interpreter. This makes it an ideal language for scripting and rapid application development for most applications and specifically connected device applications. The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms (Windows, Linux\Unix, Mac OS X) and on machine-to-machine (M2M) or Internet of Things (IoT) focused Intelligent Gateway platforms from Lantronix.


Additional Python Reference

The Python Programming Language website (http://www.python.org) is the definitive and official source for all Python information. It also contains additional links to other Python references including other sites, books, etc. For a quick and informal introduction to Python, the tutorial at Python Tutorial is a great start.


Supported Lantronix Products

The following Lantronix products provided integrated Python runtime support.

Support for the following Lantronix products is planned (Alpha Software Available)


Getting Started

This section describes how to create a simple Python program and the steps to run the program on a Lantronix device.

First Program "Hello World"

Write Simple Python Program

In your favorite text editor, create a file named 'helloworld.py', with the following contents:

# Hello World - a simple text output program
def main():
print 'Hello World!'

main()

Transfer Program Onto Lantronix Device

  1. Log in to the Web UI for Lantronix Device. Enter the configured username and password. Default values are admin and PASS.
  2. Using the Filesystem menu option, go to the Browse sub-menu and upload the helloworld.py file to the device.


File transfer getting started.jpg

Run The Program

  1. Navigate to the Applications menu option in the Web UI.
  2. For script 1, enter the script name as helloworld.py and mark the Enable checkbox to enable this script.
  3. Enter the output filename as helloworld.out. This file will be created in the filesystem to capture all stdout output from the script.
  4. Hit the Run button for Script 1 to run the transferred program.


Python application getting started.jpg


Open the output file helloworld.out from the Filesystem -> Browse Menu to check the expected Hello World output from the script.

System Requirements

Integrated Python runtime is available from software version 7.9.0.0 onwards on the supported Lantronix products.
If you have products with software version older than 7.9.0.0, please follow the upgrade instructions for the respective products. The software and the upgrade notes are available from the Lantronix support site at www.lantronix.com/support/downloads.

Recommended Python Version

The version of Python supported on Lantronix PremierWave family of Intelligent Gateway products is 2.7.3. We recommend that you install the same version on your development machine (Windows/Linux).

Using Python on Lantronix Devices

Configuration and control of Python programs can be accomplished by accessing the Command Line Interface (CLI) using Telnet/SSH or serial port. Web configuration UI is accessible via the browser on any Windows/Linux host or on smartphone/tablet. Supported browsers are Firefox, Chrome and Internet Explorer.

Using CLI (Command Line Interface)

The CLI provides a quick and easy way for developers used to working in the Command Prompt on Windows or using the Shell on Linux to interact with the Lantronix devices.

Load Python Programs

To upload Python program to Lantronix device, use FTP on the command line. The following is an example of this procedure at the Linux shell prompt.

#ftp <Lantronix Device IP Address>
-- login --
ftp> put helloworld.py helloworld.py
ftp> exit

For Python module packages, use FTP to transfer the tar.gz package tarball as shown below.

#ftp <Lantronix Device IP Address>
-- login --
ftp> put <python_package>.tar.gz <python_package>.tar.gz
ftp> exit

Installing Python Module Packages

To install the packages, using CLI, please follow the steps provided below.

#telnet <Lantronix Device IP Address>
--login (if enabled)—
> enable
(enable)# configure
(config)# applications
(config-applications)# python install <zip|tar.gz file>
(config)# exit
(enable)# exit

Configure Python Programs

To configure uploaded Python programs to run, a series of commands needs to be entered in the CLI. The example below shows connecting to the CLI via telnet and assumes that Python program helloworld.py was uploaded using the earlier steps.

#telnet <Lantronix Device IP Address>
--login (if enabled) --
> enable
(enable)# configure
(config)# applications
(config-applications)# python <instance>
(config-applications-python:<instance>)# file <python program>.py
(config-applications-python:<instance>)# state enable
(config-applications-python:<instance>)# write
(config-applications-python:<instance>)# exit
(config)# exit
(enable)# exit

Running Python Programs

In order to run the configured Python program the run python is used. The stdout is directed towards the output file (if configured). The Python program is not a sub-process of the CLI process and run in the background. As a result it does not inherit the file descriptors of the CLI process even though the run command is issued via the CLI. This is generally not considered a limitation as most Python programs for connected applications would run independently and use modules that provide network connectivity protocol implementations or specific hardware abstraction functions.

#telnet <Lantronix device IP addresss>
--login (if enabled)—
> enable
(enable)# configure
(config)# applications
(config-applications)# run python <instance>
(config)# exit
(enable)# exit

Additional Available Commands

There are additional commands available for the following functions:

  • parameters <text>
    String of parameters as arguments to the script when it runs
  • output <text>
    Output file name for capturing stdout and debugging messages (print)
  • onstart enable
    Enable script to run on device bootup/startup
  • onstart disable
    Disable script to run on device bootup/startup
  • onshutdown enable
    Enable script to run on device shutdown
  • onshutdown disable
    Disable script to run on device shutdown


Using Web Configuration UI

The Web Configuration UI provides an easy to follow point-and-click mechanism for device configuration. Python programs can be loaded, configured and run through the Web Configuration UI as described below.

The Web Configuration UI is accessed via the Web browser by logging in using the configured password for the admin user.

Load Python Programs

In order to upload the Python programs or module packages to the Lantronix device:

  • Navigate to the Filesystem menu
  • Select the Browse tab.
  • Select and upload Python program using the Upload File section.

[[File:

Installing Python Module Packages

Configure Python Programs

Running Python Programs

Development Workflow

This section provides an overview of the development workflow when creating Python programs and packages for use on Lantronix devices.
Since Python is available on a number of platforms, most development for Python can be done on a Windows/Linux host and tested locally too. The Python program can then be transferred to the Lantronix devices for further debugging, testing, packaging and deployment.

The current version of Python installed on Lantronix devices is 2.7.3. We recommend that you install the same version on your development machine. Also, only use the python modules known to be compatible with this version. For download, installation instructions and additional documentation please visit https://www.python.org/.

Command Line Based Workflow

Develop/Test on Windows/Linux Host

Using Python Interpreter Command Line

Using IDE

Create Python Package For Lantronix Devices

Transfer/Run Python Programs on Lantronix Devices

Follow the instructions in the section above on Using Python on Lantronix devices to test and deploy the program on Lantronix devices.

Python Runtime Support

Python Version Supported

Python Language/Runtime

Python Standard Library

Preinstalled Python Packages

PySerial

This module encapsulates the access for the serial port.
More information can be found at: http://pyserial.sourceforge.net/

While using this module for a particular serial port, please ensure that no other applications are using that serial device. Serial port access for standard Lantronix applications, can be disabled using the no protocol command at the 'line menu level in the CLI or via selecting Protocol: None under the Line menu on the Web UI.