Difference between revisions of "Python/ltrx apis"

From Lantronix Wiki!
Jump to navigation Jump to search
 
(7 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
Lantronix provides Python modules with APIs to access features of the PremierWave device more easily from your program.
 
Lantronix provides Python modules with APIs to access features of the PremierWave device more easily from your program.
  
Note that these APIs require firmware version 7.10 or later.
+
Note that these APIs require firmware version 7.10 or later. '''As 7.10 is Alpha firmware, APIs are subject to change!'''
 +
 
 +
=== LtrxDsal ===
 +
Access to the Digital input/outputs on the device.
 +
 
 +
<nowiki>
 +
from ltrxlib import LtrxDsal
 +
 
 +
 
 +
dsal = LtrxDsal()
 +
 
 +
print 'Reading Digital Input 1...'
 +
state = dsal.readDigitalInput(1)
 +
print 'state=' + str(state)
 +
 
 +
print 'Reading Digital Input 2...'
 +
state = dsal.readDigitalInput(2)
 +
print 'state=' + str(state)
 +
 
 +
print 'Setting relay to true'
 +
result = dsal.setRelay(1, True)
 +
 
 +
print 'Reading relay 1...'
 +
state = dsal.readRelay(1)
 +
print 'Relay is ' + state
 +
 
 +
print 'Setting relay to false...'
 +
dsal.setRelay(1, False)
 +
print 'Reading Relay...'
 +
state = dsal.readRelay(1,)
 +
print 'Relay is ' + state
 +
 
 +
print 'Reading temperature...'
 +
temp = dsal.readInternalTemperature(1)
 +
print 'Temperature is: ' + str(temp)
 +
</nowiki>
  
 
=== LtrxCellular ===
 
=== LtrxCellular ===
Line 9: Line 44:
  
 
  <nowiki>
 
  <nowiki>
import sys
 
import os
 
 
from ltrxlib import LtrxCellular
 
from ltrxlib import LtrxCellular
  
try:
 
    cell = LtrxCellular()
 
    print 'calling sendSMS()...'
 
    cell.sendSMS("1112223333", "Hello!")
 
  
    print 'calling receiveSMS'
+
cell = LtrxCellular()
    msg = cell.receiveSMS()
+
print 'calling sendSMS()...'
    print 'message: ' + msg
+
cell.sendSMS("1112223333", "Hello!", 0)  # 3rd argument: 0=ASCII 7 bits, 1=ASCII 8 bits, 2=unicode/utf-8
 +
 
 +
print 'calling receiveSMS'
 +
msg = cell.receiveSMS()
 +
print 'message: ' + msg
 
  </nowiki>
 
  </nowiki>
  
=== LtrxDsal ===
+
==== Receiving SMS best practices ====
Access to the Digital input/outputs on the device.
+
The Lantronix firmware does not store SMS messages. At the time that an SMS is received, any process that is waiting to receive SMS will be given the message, and then the message is deleted from the radio.
 
 
<nowiki>
 
import sys
 
import os
 
from ltrxlib import LtrxDsal
 
  
try:
+
To ensure that a message will be received by an application, the application should make the blocking call receiveSMS(). To avoid blocking the rest of the application, the programmer will want to place this on a separate thread or process.
    dsal = LtrxDsal()
 
  
    print 'Reading Digital Input 1...'
+
Below is an example of spawning a new thread just for waiting for SMS messages and putting them into a Queue, where the main thread of the application can check if the Queue has items and read them when needed:
    state = dsal.readDigitalInput(1)
 
    print 'state=' + str(state)
 
  
    print 'Reading Digital Input 2...'
+
<nowiki>
    state = dsal.readDigitalInput(2)
+
#!/usr/bin/python
    print 'state=' + str(state)
+
from ltrxlib import LtrxCellular
 +
from threading import Thread
 +
from Queue import Queue
 +
import time
 +
import serial
 +
 +
def receiveSmsThread(smsQ, cell):
 +
        # Infinite loop to wait for SMS and put them into a Queue
 +
while True:
 +
smsQ.put(cell.receiveSMS())
 +
 +
if __name__ == '__main__':
 +
smsQ = Queue()
 +
       
 +
        # Open serial port 1 on the PremierWave XC HSPA+ to output the
 +
        # messages coming in
 +
ser = serial.Serial('/dev/ttyS1', 115200)
 +
cell = LtrxCellular()
  
    print 'Setting relay to true'
+
        # Define and start the thread to receive the SMS
    result = dsal.setRelay(1, True)
+
t = Thread(target = receiveSmsThread, args = (smsQ,cell))
 +
t.start()
  
    print 'Reading relay 1...'
+
while True:   
    state = dsal.readRelay(1)
+
                # Check if the Queue has messages, and if so remove one and display the
    print 'Relay is' + state
+
                # message information
 +
if (smsQ.empty()!=True):
 +
msg = smsQ.get_nowait()
  
    print 'Setting relay to false...'
+
                        # Note that this uses the new API where the message is a tuple
    dsal.setRelay(1, False)
+
                        # that includes both the message text and the sending number
    print 'Reading Relay...'
+
ser.write('From: '+str(msg[1])+'\rMessage says: '+str(msg[0])+'\r')
    state = dsal.readRelay(1,)
+
else:
    print 'Relay is' + state
+
time.sleep(5)
 
 
    print 'Reading temperature...'
 
    temp = dsal.readInternalTemperature(1)
 
 
  </nowiki>
 
  </nowiki>

Latest revision as of 17:30, 10 April 2015

Overview

Lantronix provides Python modules with APIs to access features of the PremierWave device more easily from your program.

Note that these APIs require firmware version 7.10 or later. As 7.10 is Alpha firmware, APIs are subject to change!

LtrxDsal

Access to the Digital input/outputs on the device.

from ltrxlib import LtrxDsal


dsal = LtrxDsal()

print 'Reading Digital Input 1...'
state = dsal.readDigitalInput(1)
print 'state=' + str(state)

print 'Reading Digital Input 2...'
state = dsal.readDigitalInput(2)
print 'state=' + str(state)

print 'Setting relay to true'
result = dsal.setRelay(1, True)

print 'Reading relay 1...'
state = dsal.readRelay(1)
print 'Relay is ' + state

print 'Setting relay to false...'
dsal.setRelay(1, False)
print 'Reading Relay...'
state = dsal.readRelay(1,)
print 'Relay is ' + state
  
print 'Reading temperature...'
temp = dsal.readInternalTemperature(1)
print 'Temperature is: ' + str(temp)
 

LtrxCellular

Send and receive SMS messages.

from ltrxlib import LtrxCellular


cell = LtrxCellular()
print 'calling sendSMS()...'
cell.sendSMS("1112223333", "Hello!", 0)  # 3rd argument: 0=ASCII 7 bits, 1=ASCII 8 bits, 2=unicode/utf-8

print 'calling receiveSMS'
msg = cell.receiveSMS()
print 'message: ' + msg
 

Receiving SMS best practices

The Lantronix firmware does not store SMS messages. At the time that an SMS is received, any process that is waiting to receive SMS will be given the message, and then the message is deleted from the radio.

To ensure that a message will be received by an application, the application should make the blocking call receiveSMS(). To avoid blocking the rest of the application, the programmer will want to place this on a separate thread or process.

Below is an example of spawning a new thread just for waiting for SMS messages and putting them into a Queue, where the main thread of the application can check if the Queue has items and read them when needed:

#!/usr/bin/python
from ltrxlib import LtrxCellular
from threading import Thread
from Queue import Queue
import time
import serial
	
def receiveSmsThread(smsQ, cell):
        # Infinite loop to wait for SMS and put them into a Queue
	while True:
		smsQ.put(cell.receiveSMS())
		
if __name__ == '__main__':
	smsQ = Queue()
        
        # Open serial port 1 on the PremierWave XC HSPA+ to output the 
        # messages coming in
	ser = serial.Serial('/dev/ttyS1', 115200)
	cell = LtrxCellular()

        # Define and start the thread to receive the SMS
	t = Thread(target = receiveSmsThread, args = (smsQ,cell))
	t.start()

	while True:    
                # Check if the Queue has messages, and if so remove one and display the
                # message information
		if (smsQ.empty()!=True):
			msg = smsQ.get_nowait()

                        # Note that this uses the new API where the message is a tuple
                        # that includes both the message text and the sending number
			ser.write('From: '+str(msg[1])+'\rMessage says: '+str(msg[0])+'\r')
		else:
			time.sleep(5)