When device debugging becomes difficult during IoT development, it is imperative to use the network packet capturing tool Wireshark for analyzing device behaviors and subsequently troubleshooting problems. Let's consider an example that shows how to use Wireshark to analyze the communication between a device and Alibaba Cloud IoT Platform.
On Alibaba Cloud IoT Platform, create and register a device and obtain trituple information as shown below.
In a computer, develop a device simulation program using Node.js and complete the following operations using the subsequent code:
/**
* node aliyun-iot-device.js
*/
const mqtt = require('aliyun-iot-mqtt');
//设备身份三元组+区域
const options = {
"productKey": "设备PK",
"deviceName": "设备DN",
"deviceSecret": "设备Secret",
"regionId": "cn-shanghai"
};
//1.建立连接
const client = mqtt.getAliyunIotMqttClient(options);
//2.订阅主题
setTimeout(function() {
client.subscribe(`/${options.productKey}/${options.deviceName}/user/get`)
}, 3 * 1000);
//3.发布消息
setTimeout(function() {
client.publish(`/${options.productKey}/${options.deviceName}/user/update`, getPostData(),{qos:1});
}, 5 * 1000);
//4.关闭连接
setTimeout(function() {
client.end();
}, 8 * 1000);
function getPostData() {
const payloadJson = {
temperature: Math.floor((Math.random() * 20) + 10),
humidity: Math.floor((Math.random() * 20) + 10)
}
console.log("payloadJson " + JSON.stringify(payloadJson))
return JSON.stringify(payloadJson);
IoT Platform uses the MQ Telemetry Transport (MQTT) protocol for communication. Therefore, configure the rule: TCP and port 1883.
Finally, initiate the device simulation program to get started.
Wireshark captures the complete process of MQTT network interaction post the simulation script execution. The device IP address is marked as "device" for the sake of convenience, whereas the IP address of the connected IoT Platform remains unchanged.
The red box in the preceding figure shows a TCP three-way handshake, which is initiated by the "device" IP address. The used device port is port 56150.
The following figure shows the MQTT CONNECT behavior.
Click the Connect record to view the packet details appear in the lower part of the window. The client ID, user name, and password are used to authenticate the device during this CONNECT operation.
IoT Platform returns CONNACK in response to CONNECT after device authentication.
The following figure shows the process where the "device" IP address subscribes to a topic from the IoT Platform. The red box shows the topic subscribed to by the device.
The following figure shows the process where IoT Platform responds to the SUBSCRIBE behavior of the device.
The following figure shows the process where the "device" IP address publishes a message with QoS equal to 1 to IoT Platform. The packet includes the topic and payload of the message.
IoT Platform returns a PUBACK message to the "device" IP address based on the QoS value 1.
Also, find this log entry on the Device Log page of the IoT Platform console as shown below.
The following figure shows the process where the "device" IP address initiates the DISCONNECT command to disconnect the MQTT connection channel.
The red box in the following figure shows a TCP four-way handshake.
To find complete log entries about devices online and offline navigate to the Device Log page of the IoT Platform console.
This article describes the basic skills for using Wireshark to analyze the network communication between a device and Alibaba Cloud IoT Platform. We hope that these skills are helpful for your IoT development.
The following table lists the Identifiers at the TCP layer.
SYN | A connection is established. |
---|---|
FIN | A connection is terminated. |
ACK | A response is returned. |
PSH | Data is transmitted. |
RST | A connection is reset. |
How to Identify Frequently Dropped and Constant Offline Devices among 50,000 Devices
Dikky Ryan Pratama - May 22, 2023
Alibaba Clouder - June 10, 2019
PM - C2C_Yuan - September 10, 2021
GXIC - February 20, 2020
Alibaba Clouder - December 21, 2018
Tim Chen - May 22, 2019
Provides secure and reliable communication between devices and the IoT Platform which allows you to manage a large number of devices on a single IoT Platform.
Learn MoreConnect your business globally with our stable network anytime anywhere.
Learn MoreA cloud solution for smart technology providers to quickly build stable, cost-efficient, and reliable ubiquitous platforms
Learn MoreEstablish high-speed dedicated networks for enterprises quickly
Learn MoreMore Posts by GXIC