Digi International Inc XB3C1 Manual Do Utilizador

Página de 146
Getting started with the XBee Smart Modem Development Kit
Get started with MQTT
Digi XBee3 Cellular LTE Cat 1 Smart Modem User Guide
37
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)))
print("Sending payload to modem...")
bytes_wr = ser.write(data)
# Write payload to modem via
UART/serial
print("Wrote {} bytes to modem".format(bytes_wr))
def main():
setup()
# Setup the serial port and socket
global clisock, svrsock
if (not clisock):
# Accept a connection on 'svrsock' to open 'clisock'
print("Awaiting ACCEPT on server sock...")
(clisock,cliaddr) = svrsock.accept()
# Accept an incoming
connection
print("Connection accepted on socket")
# Make thread for ComReader
comthread = threading.Thread(target=ComReaderThread)
comthread.start()
# Start the thread
# Make thread for SockReader
sockthread = threading.Thread(target=SockReaderThread)
sockthread.start()
# Start the thread
main()
Note
This script is a general TCP-UART proxy, and can be used for other applications or scripts that
use the TCP protocol. Its functionality is not limited to MQTT.
Note
You can easily copy and paste code from the online version of this Guide. Use caution with the
PDF version, as it may not maintain essential indentations.
This proxy script waits for an incoming connection on localhost (127.0.0.1), on port 17300. After
accepting a connection, and creating a socket for that connection (clisock), it creates two threads,
one that reads the serial or COM port that the XBee Smart Modem is connected to, and one that
reads the socket (clisock), that the MQTT client is connected to.
With:
n
The proxy script running
n
The MQTT client connected to the proxy script via localhost (127.0.0.1)
n
The XBee Smart Modem connected to the machine via USB and properly powered
n
APDL, and DE set correctly
the proxy acts as an intermediary between the MQTT client and the XBee Smart Modem, allowing the
MQTT client to use the data connection provided by the device.
Think of the proxy script as a translator between the MQTT client and the XBee Smart Modem. The
following figure shows the basic operation.