Python/ltrx apis

From Lantronix Developer Wiki
Jump to: navigation, search

Contents

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)
 
Personal tools
Namespaces

Variants
Actions
Navigation
Tools