Difference between revisions of "Python Wiki"

From Lantronix Wiki!
Jump to navigation Jump to search
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
__NOTOC__ __NOEDITSECTION__
  
== Code samples ==
+
<!-------------------- Welcome Banner -------------------->
=== Determining the type of device ===
+
{| style="width:100%; border:1px solid #ff5800; background:#ff5800;"
Sometimes it is useful to find out what kind of device the script is running on, so that it can be deployed on multiple types of PremierWave devices without modifying.
+
|style="width:280px; text-align:center; white-space:nowrap; color:#000;"|
 +
<div style="font-size:150%; border:none; margin:0; padding:.1em; color:#000;">Welcome to Lantronix Python Developer Wiki</div>
 +
|}
  
The code below creates the pwDevice class. When an instance of this class is created, it will first find out what the MAC address of the device is. This can be useful to use as a unique identifier. It will then determine which type of device it is. As an example, it also creates a property called import_file which is a string with the path of the file used for importing configuration. This will become useful if a program needs to change configuration of the device.
+
== Getting Started ==
  
import os.path
+
The Lantronix Python Developer Wiki is your source to learn more about developing M2M/IoT applications using the Python programming language and deploying it on Lantronix family of Intelligent Gateway products.
class pwDevice:
 
def __init__(self):
 
mac = commands.getoutput("ifconfig eth0| grep HWaddr | awk '{ print $5 }'").strip()
 
self.mac = mac.translate(None,':')
 
if os.path.exists('/ltrx_user/pwxcr/pwxc_hspa_import.xcr'):
 
self.type = "PremierWave XC HSPA+"
 
self.import_file = '/ltrx_user/pwxcr/pwxc_hspa_import.xcr'
 
elif os.path.exists('/ltrx_user/pwxcr/pwxn_import.xcr'):
 
self.type = "PremierWave XN"
 
self.import_file = '/ltrx_user/pwxcr/pwxn_import.xcr'
 
elif os.path.exists('/ltrx_user/pwxcr/pwen_import.xcr'):
 
self.type = "PremierWave EN"
 
self.import_file = '/ltrx_user/pwxcr/pwen_import.xcr'
 
elif os.path.exists('/ltrx_user/pwxcr/pwse1000_import.xcr'):
 
self.type = "PremierWave SE1000"
 
self.import_file = '/ltrx_user/pwxcr/pwse1000_import.xcr'
 
else:
 
self.type = "Unknown"
 
  
This example will create an instance, and log the MAC address to a file in the PremierWave's filesystem:
+
If you are new to Python, please visit http://www.python.org to get started and learn the basics.For a quick and informal introduction to Python, the [https://docs.python.org/2/tutorial/index.html Python Tutorial] is a great start.
import logging
+
<br>
logging.basicConfig(level=logging.DEBUG, filename='pythonDebug.log')
 
if __name__ == '__main__':
 
try:
 
dev = pwDevice()
 
logging.debug(dev.mac)
 
except Exception, ex:
 
logging.exception('Was not able to log')
 
  
=== Rebooting ===
+
Once you have become familiar with the language, referencing the '''[[Lantronix_Python_Programmers_Guide|Programmer's Guide]]''' is a good next step. It describes how to load and run Python programs on Lantronix devices, reviews available Python modules including those that expose Lantronix specific APIs.
The Lantronix application running on the PremierWave device safely handles the reboot cycle. You can kick off a reboot by importing XML into the configuration file that indicates to the application to start a reboot.
 
  
xml = '''<?xml version="1.0" standalone="yes"?>
+
<br>
<!-- Automatically generated XML -->
+
Please visit our [//forums.lantronix.com Support Forums] to share ideas with the community of Lantronix users,  
<!DOCTYPE configrecord [ <!ELEMENTconfigrecord (configgroup+)> 
+
check for solutions to your questions and post your own queries.
<!ELEMENT configgroup (configitem+,configgroup*)> 
+
 
<!ELEMENT configitem(value+)> 
+
== Code Samples ==
<!ELEMENT value (#PCDATA)> 
+
[[Python Code Samples]]<br>
<!ATTLIST configrecord version CDATA #IMPLIED> 
+
[[Websocket Server]]<br>
<!ATTLISTconfiggroup name CDATA #IMPLIED>  
+
[[Lantronix Discovery Protocol]]<br>
<!ATTLIST configgroup instance CDATA #IMPLIED>  
+
[[Weigh Scale MQTT | Sending Data to 2lemetry, Google Analytics & hosting web server]]
<!ATTLIST configitem nameCDATA #IMPLIED>  
+
<!-- [[MQTT Publish Subscribe]]<br> -->
<!ATTLIST configitem instance CDATA #IMPLIED> 
 
<!ATTLIST value name CDATA #IMPLIED>]>
 
<configrecord version = "0.1.0.0T0">
 
<configgroup name = "xml import control">      
 
<configitem name = "reboot">          
 
<value>enable</value>     
 
</configitem> 
 
</configgroup>
 
</configrecord>'''
 
 
dev = pwDevice()
 
try:
 
f = open(dev.import_file,'w')
 
f.write(xml)
 
f.close()
 
except Exception, ex:
 
logging.exception('Reboot')
 

Latest revision as of 22:08, 24 March 2015


Welcome to Lantronix Python Developer Wiki

Getting Started

The Lantronix Python Developer Wiki is your source to learn more about developing M2M/IoT applications using the Python programming language and deploying it on Lantronix family of Intelligent Gateway products.

If you are new to Python, please visit http://www.python.org to get started and learn the basics.For a quick and informal introduction to Python, the Python Tutorial is a great start.

Once you have become familiar with the language, referencing the Programmer's Guide is a good next step. It describes how to load and run Python programs on Lantronix devices, reviews available Python modules including those that expose Lantronix specific APIs.


Please visit our Support Forums to share ideas with the community of Lantronix users, check for solutions to your questions and post your own queries.

Code Samples

Python Code Samples
Websocket Server
Lantronix Discovery Protocol
Sending Data to 2lemetry, Google Analytics & hosting web server