Device development based on MQTT protocol
Modify configuration file
Configuration file path: code/dtu_config.json
。
Based on TCP private server data transparent transmission, configure as follows:
set
systemconfig.cloud
configuration item asmqtt
, which means MQTT transparent mode.{ "system_config": { "cloud": "mqtt" } }
This experiment adopts the MQTT transparent transmission mode, and users need to set the MQTT server domain name, port, client ID, and subscription and publishing topics parameters in the
mqtt_pivate_cloud_config
configuration item according to the actual situation, as follows.{ "mqtt_private_cloud_config": { "server": "mq.tongxinmao.com", "port": 18830, "client_id": "txm_1682300809", "user": "", "password": "", "clean_session": true, "qos": 0, "keepalive": 60, "subscribe": {"down": "/public/TEST/down"}, "publish": {"up": "/public/TEST/up"} } }
The configuration item
uart_config
is a serial port configuration parameter, which defaults to the configuration made based on the current experimental development board and cannot be changed. If users use other development boards, they need to configure according to the actual hardware.
{
"uart_config": {
"port": 2,
"baudrate": 115200,
"bytesize": 8,
"parity": 0,
"stopbits": 1,
"flowctl": 0,
"rs485_config": {
"gpio_num": 28,
"direction": 0
}
}
}
The complete configuration file template is as follows:
{
"system_config": {
"cloud": "tcp"
},
"mqtt_private_cloud_config": {
"server": "mq.tongxinmao.com",
"port": 18830,
"client_id": "txm_1682300809",
"user": "",
"password": "",
"clean_session": true,
"qos": 0,
"keepalive": 60,
"subscribe": {"down": "/public/TEST/down"},
"publish": {"up": "/public/TEST/up"}
},
"socket_private_cloud_config": {
"domain": "112.31.84.164",
"port": 8305,
"timeout": 5,
"keep_alive": 5
},
"uart_config": {
"port": 2,
"baudrate": 115200,
"bytesize": 8,
"parity": 0,
"stopbits": 1,
"flowctl": 0,
"rs485_config": {
"gpio_num": 28,
"direction": 0
}
}
}
Parameter description:
systemconfig. config
: Specify the type of private cloud currently in use. Currently supports TCP and MQTT.mqtt_private_cloud_config
: MQTT private cloud configuration.socket_private_cloud_config
: TCP private cloud configuration.uart_config
: Serial port parameter configuration.
Script import and run
After downloading and installing the QPYCom tool, use the tool to download the script to the QuecPython module.
💡 Tips
QPYCom Installation and usage tutorial: https://python.quectel.com/doc/Application_guide/en/dev-tools/QPYcom/index.html
Debugging
View REPL interaction port logs
After the program runs, the log output can be seen on the REPL interaction page as shown in the following figure. There are two threads in the DTU service that process data: one is used to detect and read serial port data and forward it to the cloud, and the other is used to detect the transmission of downstream data from the cloud to the serial port, as shown in the following figure.
Upstream data transparent transmission
Simulate MCU sending upstream data to the module using serial debugging tools.
- Enter "hello world!" in the
Input String
input box character string. - Click the
Send Command
button to send data through the serial port.
DTU receives serial data and directly transmits it to MQTT cloud.
Cloud based reception of upstream data logs
Downstream data transparent transmission
Send downstream data from the cloud.
- Set the theme for cloud downlink data (consistent with the subscription theme configured for DTU applications).
- Enter downstream data.
- Publish.
DTU downlink data log.
Observe the serial port debugging tool through QCOM to simulate the downstream data of the MCU receiving module.