
Examples: IOT protocols with transparent mode
Get started with MQTT
Digi XBee® 3 Cellular LTE-M/NB-IoT Global Smart Modem User Guide
77
global clisock, cliaddr, svrsock, ser
# Change this to the COM port your XBee Cellular module is using.
On
# Linux, this will be /dev/ttyUSB#
comport = 'COM44'
# This is the default serial communication speed of the XBee Cellular
# module
comspeed = 115200
buffer_size = 4096
# Default receive size in bytes
debug_on = 0
# Enables printing of debug messages
toval = None
# Timeout value for serial port below
# Serial port object for XBCell modem
ser = serial.Serial(comport,comspeed,timeout=toval)
# Listening socket (accepts incoming connection)
svrsock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# Allow address reuse on socket (eliminates some restart errors)
svrsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
clisock = None
cliaddr = None
# These are first defined before thread creation
addrtuple = ('127.0.0.1', 17300)
# Address tuple for localhost
# Binds server socket to localhost (allows client program connection)
svrsock.bind(addrtuple)
svrsock.listen(1)
# Allow (1) connection
def ComReaderThread():
"""
This thread listens on the defined serial port object ('ser') for data
from the modem, and upon receipt, sends it out to the client over the
client socket ('clisock').
"""
global clisock
while (1):
resp = ser.read()
## Read any available data from serial port
print("Received {} bytes from modem.".format(len(resp)))
clisock.sendall(resp)
# Send RXd data out on client socket
print("Sent {} byte payload out socket to client.".format(len
(resp)))
def SockReaderThread():
"""
This thread listens to the MQTT client's socket and upon receiving a
payload, it sends this data out on the defined serial port ('ser') to
the
modem for transmission.
"""
global clisock
while (1):
data = clisock.recv(4096)
# RX data from client socket
# If the RECV call returns 0 bytes, the socket has closed
if (len(data) == 0):
print("ERROR - socket has closed.
Exiting socket reader
thread.")
return 1
# Exit the thread to avoid a loop of 0-byte receptions
else:
print("Received {} bytes from client via socket.".format(len
(data)))