Skip to main content

Setting Up 4G LTE Connectivity for IIoT with MQTT

Introduction

The 4G LTE HAT for Raspberry Pi provides reliable, high-speed cellular connectivity, making it essential for remote and industrial IoT (IIoT) applications. With MQTT, a lightweight messaging protocol, devices can communicate efficiently over cellular networks, even in isolated locations. Using AT commands for configuration, it simplifies the process of connecting IoT devices to the network. This combination of 4G LTE and MQTT enhances real-time data transmission, enabling scalable IIoT solutions with remote management capabilities.

Prerequisites

Hardware Requirements

Raspberry Pi 5Raspberry Pi 4G LTE CAT4 HAT

Software Requirements

Communication drivers and tools

If you have not installed relevant drivers and communication tools, please check the guide first

Mosquitto Explorer

We will be using the Mosquitto broker, specifically the test broker available at https://test.mosquitto.org , which does not require a username or password. For convenience, it is recommended to install Mosquitto directly on your PC for testing purposes.

Using AT commands Let's connect to MQTT Broker

Step 1: Configure receiving mode

AT+QMTCFG="recv/mode",0,0,1

Step 2: Open a network for MQTT client

AT+QMTOPEN=0,"test.mosquitto.org",1883

Step 3: Check MQTT connection status (optional)

AT+QMTOPEN?

Step 4: Connect a client to the Mosquitto MQTT server

AT+QMTCONN=0,"clientExample"
note

Note that the client ID must be unique, so ensure you generate a highly unique one. Mosquitto's public broker does not require a username or password for access.

Step 5: Publish a message to a topic

AT+QMTPUBEX=0,0,0,0,"test/topic",30 

note

when > appear type the massage and press ctrl+z

> This is test data, hello MQTT.

Open Mosquitto Explorer and enter the topic you published to. You will see the updates appear there.

Step 6: Subscribe to a topic

AT+QMTSUB=0,1,"test/topic",2

Open Mosquitto Explorer and enter the topic and massage you want to publish to from the 4G module.

You will then see the published message successfully subscribed to at the 4G module's end.

Step 7: Unsubscribe from a topic

AT+QMTUNS=0,2,"test/topic"

Step 8: Disconnect the client from the MQTT server

AT+QMTDISC=0

Python Code Implementation

Step 1. Prepare the Directory and Virtual Environment

  • Open a terminal on your Raspberry Pi.
  • Create a new project folder and navigate into it:
mkdir mqtt_EX
cd mqtt_EX
  • Set up a Python virtual environment:
python3 -m venv --system-site-packages env
  • Activate the virtual environment:
source env/bin/activate
  • Install the required libraries:
pip install pyserial 

Step 2. Prepare the Python Script

  • Open the Thonny Python IDE (pre-installed on Raspberry Pi).

  • Create a new file in Thonny and paste the provided code into the editor.

  • Update the usb_port parameter to match your Raspberry Pi's serial port for the 4G HAT. Typically, it might be /dev/ttyUSB2 or /dev/ttyUSB3. Example:

usb_port = "/dev/ttyUSB2"
  • Save the file as test_mqtt.py in the mqtt_EX folder.

import serial
import time

# Serial port configuration
SERIAL_PORT = '/dev/ttyUSB2' # Adjust based on your setup
BAUD_RATE = 9600


def send_at_command(ser, command, delay=1):
"""
Sends an AT command to the Quectel module and waits for a response.
"""
ser.write((command + '\r\n').encode())
time.sleep(delay)
response = ser.read_all().decode()
print(f"Command: {command}\nResponse: {response}")
return response


def main():
# Open serial connection
ser = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=5)
if not ser.is_open:
ser.open()

try:
# Configure MQTT receive mode
send_at_command(ser, 'AT+QMTCFG="recv/mode",0,0,1')

# Open MQTT connection
send_at_command(ser, 'AT+QMTOPEN=0,"test.mosquitto.org",1883')
send_at_command(ser, 'AT+QMTOPEN?') # Check connection status

# Connect to the MQTT broker
send_at_command(ser, 'AT+QMTCONN=0,"clientExample"')

# Subscribe to the topic
send_at_command(ser, 'AT+QMTSUB=0,1,"test/topic_subscribe",2')

print("Publishing and subscribing. Press Ctrl+C to stop.")

while True:
try:
# Publish a message
send_at_command(ser, 'AT+QMTPUBEX=0,0,0,0,"test/topic_publish",30')
send_at_command(ser, 'This is test data, hello MQTT.', delay=0.5)

# Read incoming messages
print("Checking for subscribed topic messages...")
incoming = ser.read_all().decode()
if incoming:
print(f"Received: {incoming}")

# Delay between operations
time.sleep(2)
except KeyboardInterrupt:
print("Exiting loop...")
break

# Unsubscribe from the topic
send_at_command(ser, 'AT+QMTUNS=0,2,"test/topic_subscribe"')

# Disconnect from the broker
send_at_command(ser, 'AT+QMTDISC=0')
finally:
# Close serial connection
ser.close()


if __name__ == '__main__':
main()

Step 3. Run the Script

  • Open a terminal, ensure you're in the project directory:
cd mqtt_EX
  • Activate the virtual environment:
source env/bin/activate
  • Run the script using Python:
python test_mqtt.py
  • The output

Resources

Tech Support & Product Discussion

Thank you for choosing our products! We are here to provide you with different support to ensure that your experience with our products is as smooth as possible. We offer several communication channels to cater to different preferences and needs.

Loading Comments...