Digi XBee 3 Cellular LTE-M User Manual page 77

Smart modem
Table of Contents

Advertisement

Examples: IOT protocols with transparent mode
global clisock, cliaddr, svrsock, ser
# Change this to the COM port your XBee Cellular module is using.
# 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
debug_on = 0
toval = None
# 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
addrtuple = ('127.0.0.1', 17300)
# Binds server socket to localhost (allows client program connection)
svrsock.bind(addrtuple)
svrsock.listen(1)
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()
print("Received {} bytes from modem.".format(len(resp)))
clisock.sendall(resp)
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)
# If the RECV call returns 0 bytes, the socket has closed
if (len(data) == 0):
thread.")
else:
(data)))
Digi XBee® 3 Cellular LTE-M/NB-IoT Global Smart Modem User Guide
# Default receive size in bytes
# Enables printing of debug messages
# Timeout value for serial port below
# These are first defined before thread creation
# Allow (1) connection
## Read any available data from serial port
# Send RXd data out on client socket
print("ERROR - socket has closed.
return 1
# Exit the thread to avoid a loop of 0-byte receptions
print("Received {} bytes from client via socket.".format(len
# Address tuple for localhost
# RX data from client socket
Exiting socket reader
Get started with MQTT
On
77

Advertisement

Table of Contents
loading

This manual is also suitable for:

Xbee 3 cellular nb-iot

Table of Contents