mqtt_raw_transmission_demo
demonstrates TCP/IP TXMODE by publishing a MQTT message¶
MQTT RAW TRANSMISSION DEMO USING Python TCPIP TX MODE¶
This is a limited demo meant to demonstrate that custom protocols can be implemented with the TCP/IP TXMODE feature. The demo will publish up to 100 bytes of data to a test server. It does not support authentication or encryption. Certificates could be used to improve on that, but cannot be shared in a demo like this.
If you are using a version of LinkComm which will not let you set the TXMODE to Python TCPIP (but are running a version of firmware that supports it) you will need to click the “Test Script File…” button and the script will set the necessary fields. If you need to change the setup after that, be sure to click “Test Script File…” button again.
With the following TX1 setup, this script will transmit measurements to the Mosquitto test MQTT server every hour in CSV format:
!TX1 Enable=On
!TX1 Radio Type=Cell
!TX1 Kind=Scheduled
!TX1 Label=MQTT RAW
!TX1 Scheduled Interval=01:00:00
!TX1 Scheduled Time=00:00:00
!TX1 Data Source=Measurement
!TX1 Format=CSV
!TX1 Custom Script Format=Off
!TX1 Mode=Python TCPIP
!TX1 Secure=Off
!TX1 Use Certificate=Off
!TX1 Mode Function=MQTT_PUBLISH
!TX1 Main Server=test.mosquitto.org
!TX1 Backup Server=
!TX1 Server Port=1883
!TX1 Server Username=
!TX1 Server Password=
!TX1 Server Path=
!TX1 Script Path Command=
!TX1 Script Path Acknowledge=
!TX1 Script Path Configuration=
!TX1 Use Shef Codes=Off
!TX1 Append Batt=Off
!TX1 Append Station Name=Off
!TX1 Append Tx Time=Off
!TX1 Append Serial No=Off
!TX1 Append Tx Count=Off
!TX1 Compress Data=None
!TX1 Skip First Missing=On
!TX1 Retransmit=On
!TX1 Max Tx Time=10 min
One way to view the results is to install Eclipse Mosquitto from https://mosquitto.org/download/ and issue the following command from the installation folder:
mosquitto_sub -h test.mosquitto.org -t test/topic
How this script creates MQTT messages¶
``struct.pack``: - This function is used to convert Python values into a bytes object, following a specified format.
Example:
struct.pack("!H", len(protocol_name))
:"!H"
specifies the format:!
means network byte order (big-endian), andH
means an unsigned short (2 bytes).len(protocol_name)
is the value being packed.
Fixed Header: - The fixed header is the first part of an MQTT control packet, containing the packet type and the remaining length of the packet.
Variable Header: - The variable header includes additional information specific to the packet type. For CONNECT packets, it includes the protocol name, version, connect flags, and keep-alive time.
-
mqtt_raw_transmission_demo.
MQTT_PUBLISH
(sock, message, file_name)¶ Main function to handle the MQTT publishing process. It sends CONNECT, PUBLISH, and DISCONNECT packets.
Parameters: Returns: 0 if successful, 1 otherwise.
Return type:
-
mqtt_raw_transmission_demo.
create_connect_packet
(client_id)¶ Creates a CONNECT packet to establish a connection to the MQTT server.
Parameters: client_id (str) – The client ID to use for the connection. Returns: The constructed CONNECT packet. Return type: bytes
-
mqtt_raw_transmission_demo.
create_disconnect_packet
()¶ Creates a DISCONNECT packet to close the connection to the MQTT server.
Returns: The constructed DISCONNECT packet. Return type: bytes
-
mqtt_raw_transmission_demo.
create_publish_packet
(topic, message, message_id)¶ Creates a PUBLISH packet to send a message to the MQTT server.
Parameters: Returns: The constructed PUBLISH packet.
Return type:
-
mqtt_raw_transmission_demo.
receive_packet
(sock, expected_packet_type)¶ Receives a packet from the socket and checks if it matches the expected packet type.
Parameters: - sock – The socket object.
- expected_packet_type (int) – The expected packet type to receive.
Returns: The received packet if it matches the expected type, otherwise None.
Return type: