Wiznet makers

ronpang

Published August 21, 2025 © Apache License 2.0 (Apache-2.0)

132 UCC

75 WCC

32 VAR

0 Contests

1 Followers

0 Following

Hardwired TCP/IP Chapter 22: W55MH32 MQTT & Aliyun Example

Hardwired TCP/IP Chapter 22: W55MH32 MQTT & Aliyun Example

COMPONENTS
PROJECT DESCRIPTION

Hardwired TCP/IP Chapter 22: W55MH32 MQTT & Aliyun Example

In this article, we will provide a detailed explanation on how to implement the MQTT protocol on the W55MH32 chip. Through practical examples, we will also explain to you how to use the MQTT protocol of the W55MH32 to connect to the Alibaba Cloud platform and achieve data interaction with the Alibaba Cloud object model.

Other network protocols used in this example, such as DHCP and DNS, please refer to the relevant sections. Regarding the initialization process of the W55MH32, please refer to the Network Install section. We will not elaborate on it here.

1 MQTT Introduction

MQTT (Message Queuing Telemetry Transport, a lightweight message transmission protocol based on the publish/subscribe model) is widely used in the Internet of Things (IoT) field, especially in environments with limited network bandwidth and limited device resources. It was proposed by IBM in 1999 and is mainly used for device communication in remote monitoring and control systems. The MQTT protocol has the advantages of low bandwidth, low power consumption, and low latency, and is particularly suitable for communication in embedded systems and IoT devices.

2 MQTT Features

  • Lightweight: The MQTT protocol employs a minimalist protocol header, reducing the size of the message header, making it suitable for devices with limited bandwidth and computing resources.
  • Reliability: MQTT supports three Quality of Service (QoS) levels, ensuring reliable delivery of data.
  • Real-time: MQTT is suitable for low-latency application scenarios, with messages being pushed to subscribers as soon as possible in real time.
  • Retained Messages: MQTT supports the "retained message" feature, where the broker saves the last published message. When a new device subscribes to a certain topic, the broker will immediately send the retained message.
  • Persistent Sessions: MQTT allows clients to resume session states after disconnection. Messages sent during the disconnection period can continue to be received after the client reconnects.

3 MQTT application scenarios

  • Industrial Automation: In industrial environments, equipment, sensors, controllers, etc. all need to exchange data. The MQTT protocol is suitable for providing real-time communication in complex industrial automation systems.
  • Smart Grid: In smart grid applications, power equipment (such as substations, smart meters, switchgear, etc.) exchange real-time data with the control center via Ethernet and use the MQTT protocol for remote monitoring and control.
  • Remote Equipment Monitoring and Management: Applicable to devices that require remote monitoring and management, such as remote weather stations, environmental monitoring equipment, etc. Real-time sensor data of the equipment can be obtained through the MQTT protocol.

4 MQTT Publish/Subscribe Model

The Publish-Subscribe Pattern is a messaging delivery mode that decouples the client that sends the message (the publisher) from the client that receives the message (the subscriber), eliminating the need for a direct connection between them and for them to know about each other's existence.

The essence of the MQTT Publish/Subscribe pattern lies in the fact that an intermediary role called a broker is responsible for all the routing and distribution of messages. The publisher sends messages with a topic to the broker, and the subscriber subscribes to the topic to receive the messages of interest.

In MQTT, topics and subscriptions cannot be registered or created in advance, so the broker cannot predict whether there will be subscribers for a certain topic and how many subscribers there will be. Therefore, messages can only be forwarded to the current subscribers. If there are no subscribers at all, the messages will be discarded directly.

The MQTT Publish/Subscribe pattern consists of four main components: the publisher, the subscriber, the broker, and the topic.

  • Publisher: Responsible for publishing messages to the topic. The publisher can only send data to one topic at a time and does not need to care whether the subscribers are online when publishing messages.
  • Subscriber: Subscribers receive messages by subscribing to the topic and can subscribe to multiple topics at once.
  • Broker: Responsible for receiving messages from the publisher and forwarding them to the eligible subscribers. Additionally, the broker also needs to handle requests initiated by the client such as connection, disconnection, subscription, and cancellation of subscription.
  • Topic: The topic is the basis for message routing in MQTT. It is similar to a URL path and uses slashes (/) for layering, for example, sensor/1/temperature. A topic can have multiple subscribers, and the broker will forward the messages under that topic to all subscribers; a topic can also have multiple publishers, and the broker will forward the messages in the order they arrive.

The communication process of the MQTT protocol is as shown in the following figure:

5 MQTT Quality of Service (QoS) Explanation

QoS 0: Messages are transmitted at most once, they may be lost, and no retry is performed. This is the most basic QoS level, which does not guarantee the delivery or sequence of messages.

Usage scenario: Real-time updated data such as temperature and humidity. The loss of messages has a relatively small impact on the application.

QoS 1: Messages are transmitted at least once. To ensure the message arrives, the sender will repeatedly send the message until it receives a confirmation response (PUBACK) from the receiver. This may result in duplicate message reception.

Usage scenario: Power monitoring data, or scenarios where accurate transmission of status information is required.

QoS 2: Messages are guaranteed to be transmitted only once and only once. This level ensures the reliability, uniqueness, and sequence of messages. Through a four-way handshake process, it ensures that messages are not lost and will not be duplicated.

Applicable scenarios: Applications with high security requirements, such as payment systems, important equipment control, etc. These cannot tolerate message duplication or loss.

6 MQTT message

In MQTT, the client can register a will message with the server during the connection process, similar to regular messages. We can set the topic, payload, etc. of the will message. When this client unexpectedly disconnects, the server will send this will message to other clients that have subscribed to the corresponding topic. These receivers can thus take timely actions. This feature is typically used to detect and respond to client failures or disconnection events, and is particularly suitable for use in IoT systems with high reliability and real-time monitoring scenarios.

For the messages of the MQTT protocol, you can refer to:Introduction · MQTT Protocol Chinese Version . This article will not elaborate on it again.

8 Introduction to Alibaba Cloud Object Model

The Alibaba Cloud IoT Model is an abstract model provided by Alibaba Cloud's IoT Platform for describing and managing the functions of devices. The IoT Model defines the attributes, services, and events of devices, helping developers uniformly describe the capabilities of devices and simplifying the device management and data interaction processes. The Alibaba Cloud IoT Model is one of the core concepts of device digital management and is widely applied in smart cities, industrial IoT, smart homes and other fields.

7 Introduction to Alibaba Cloud IoT Platform

The Alibaba Cloud IoT Platform is a comprehensive service platform for the IoT industry launched by Alibaba Cloud. It aims to help enterprises achieve various functions such as connection, management, monitoring, and data analysis of IoT devices. Through this platform, enterprises can connect various intelligent devices to the cloud and conduct unified management and data processing.

In terms of device connection, it supports multiple protocols such as MQTT and CoAP, and offers access methods including direct connection and gateway proxy, enabling global near-millisecond proximity access. In device management, it covers the entire lifecycle of devices, using device shadows to ensure consistent status in weak networks, and allowing devices to flexibly change their ownership. In data services, it implements cold and hot storage separation and supports visual data parsing and EB-level offline analysis. The monitoring and operation capabilities are strong, providing nearly a hundred monitoring indicators, enabling one-click tracking of message flow, and ensuring the success rate of OTA upgrades.

9 MQTT connection process for sending and receiving data with Alibaba Cloud

1. Preparation stage

Registration and real-name verification: Users need to register an account on the Alibaba Cloud platform and complete real-name verification.

Create products and add object models: Log in to the Alibaba Cloud Internet of Things platform, create a product, and add the following object model functions under the product.

Create Device: Create a device under the newly created product.

2. Record Parameters

Connection Parameters: Locate the MQTT connection parameters on the newly created device details page.

Subscription topic: /sys/ieojgBm5q2c/${deviceName}/thing/service/property/set (Property Setting Topic)

Publish topic: /sys/ieojgBm5q2c/${deviceName}/thing/event/property/post (Reporting Message Topic)

Note: In the above two topics, ${deviceName} needs to be replaced with the device name.

3. Connecting, subscribing and publishing messages

Next, we can use the connection parameters recorded above to establish a connection. Once the connection is successful, subscribe to the subscription topic mentioned above. And publish the object model data through the topic.

On the Alibaba Cloud platform, if the data format selected during the product creation stage is the Alink JSON format, both the receiving and sending data formats will follow the following format:

{
    "method": "thing.event.property.post", 
    "id": "2241348", 
    "params": {
        "prop_float": 1.25, 
        "prop_int16": 4658, 
        "prop_bool": 1
    }, 
    "version": "1.0"
}

The "method" indicates that the operation type of this message is to report device attribute events; id: The value is "2241348", which is a unique identifier; params is an object containing device attribute data for reporting object model data; version: The value is "1.0", indicating the protocol version followed by this message.

4. Message Reception Processing

Message Reception: When receiving the message, we only need to parse it according to the above JSON format and then perform the corresponding processing.

10 The implementation process

Next, let's see how to establish an MQTT connection to Alibaba Cloud on the W55MH32, and perform subscription, message publishing, and message reception processing.

Note: Since this example requires internet access, please ensure that the network environment and configuration of the W55MH32 can access the internet normally.

Step 1: Register the MQTT timed interrupt function MilliTimer_Handler() to the 1ms timer interrupt.

 1. void TIM3_IRQHandler(void)
 2. {
 3.     static uint32_t tim3_1ms_count = 0;
 4.     if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
 5.     {
 6.         tim3_1ms_count++;
 7.         MilliTimer_Handler();
 8.         if (tim3_1ms_count >= 1000)
 9.         {
10.             DHCP_time_handler();
11.             DNS_time_handler();
12.             tim3_1ms_count = 0;
13.         }
14.         TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
15.     }
16. }

Step 2: MQTT Initialization

 1. void mqtt_init(uint8_t sn, uint8_t *send_buf, uint8_t *recv_buf)
 2. {
 3.     wiz_NetInfo get_info = {0};
 4.     wizchip_getnetinfo(&get_info);
 5.     /* DNS parsing */
 6.     if (do_dns(send_buf, (uint8_t *)mqtt_params.mqttHostUrl, mqtt_params.server_ip))
 7.     {
 8.         while(1)
 9.                                                                                       {
10.                                                                                      }
11.     }
12.     NewNetwork(&n, sn);                                                           /*Obtain network configuration information*/
13.     ConnectNetwork(&n, mqtt_params.server_ip, mqtt_params.port);                   /*Connect to the MQTT server*/
14.     MQTTClientInit(&c, &n, 1000, send_buf, MQTT_ETHERNET_MAX_SIZE, recv_buf, MQTT_ETHERNET_MAX_SIZE);
15.                                        data.will=willdata;
16.     data.willFlag                    = 0;                                         /* will flag: If the will annotation bit is 0, the following will-related settings are invalid*/
17.     willdata.qos                     = mqtt_params.willQoS;                       /* will QoS */
18.     willdata.topicName.lenstring.data = mqtt_params.willtopic;                     /* will topic */
19.     willdata.topicName.lenstring.len  = strlen(willdata.topicName.lenstring.data); /* will topic len */
20.     willdata.message.lenstring.data   = mqtt_params.willmsg;                       /* will message */
21.     willdata.message.lenstring.len    = strlen(willdata.message.lenstring.data);   /* will message len */
22.     willdata.retained                = 0;
23.     willdata.struct_version          = 3;
24.     data.MQTTVersion                  = 4;
25.     data.clientID.cstring             = mqtt_params.clientid;
26.     data.username.cstring             = mqtt_params.username;
27.     data.password.cstring             = mqtt_params.passwd;
28.     data.keepAliveInterval           = 30;
29.     data.cleansession                = 1;
30. }

In this function, the socket number to be used and the receive/send buffer arrays need to be passed in. After entering the initialization function, it will first use DNS to resolve the domain name of the MQTT server, and then initialize the MQTT parameters. The connection parameters will be filled into the data structure. It should be noted here that when data.willFlag = 0, the will topic function is turned off, and vice versa it is turned on.

The data structure is as follows:

 1. typedef struct
 2. {
 3.                   /** The eyecatcher for this structure. must be MQTC. */
 4.                   char struct_id[4];
 5.                   /** The version number of this structure.  Must be 0 */
 6.                   int struct_version;
 7.                   /** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1
 8.                     */
 9.                   unsigned char MQTTVersion;
10.                  MQTTString clientID;
11.                  unsigned short keepAliveInterval;
12.                  unsigned char cleansession;
13.                  unsigned char willFlag;
14.                  MQTTPacket_willOptions will;
15.                  MQTTString username;
16.                  MQTTString password;
17. } MQTTPacket_connectData;

Step 3: Execute the do_mqtt() function within the main loop

 1. void do_mqtt(void)
 2. {
 3.     uint8_t ret;
 4.     switch (run_status)
 5.     {
 6.     case CONN: {
 7.         ret = MQTTConnect(&c, &data); /* Connect to the MQTT server */
 8.         printf("Connect to the MQTT server: %d.%d.%d.%d:%d\r\n", mqtt_params.server_ip[0], mqtt_params.server_ip[1], mqtt_params.server_ip[2], mqtt_params.server_ip[3], mqtt_params.port);
 9.         printf("Connected:%s\r\n\r\n", ret == SUCCESSS ? "success" : "failed");
10.         if (ret != SUCCESSS)
11.         {
12.             run_status = ERR;
13.         }
14.         else
15.         {
16.             run_status = SUB;
17.         }
18.         break;
19.     }
20.     case SUB: {
21.         ret = MQTTSubscribe(&c, mqtt_params.subtopic, mqtt_params.subQoS, messageArrived); /* Subscribe to Topics */
22.         printf("Subscribing to %s\r\n", mqtt_params.subtopic);
23.         printf("Subscribed:%s\r\n\r\n", ret == SUCCESSS ? "success" : "failed");
24.         if (ret != SUCCESSS)
25.         {
26.             run_status = ERR;
27.         }
28.         else
29.         {
30.             run_status = PUB_MESSAGE;
31.         }
32.         break;
33.     }
34.     case PUB_MESSAGE: {
35.         pubmessage.qos        = QOS0;
36.         pubmessage.payload    = "{\"method\":\"thing.service.property.set\",\"id\":\"12345\",\"version\":\"1.0\",\"params\":{\"Light\":56255,\"Temp\":26.7,\"Led\":1}}";
37.         pubmessage.payloadlen = strlen(pubmessage.payload);
38.         ret                   = MQTTPublish(&c, (char *)&(mqtt_params.pubtopic), &pubmessage); /* Publish message */
39.         if (ret != SUCCESSS)
40.         {
41.             run_status = ERR;
42.         }
43.         else
44.         {
45.             printf("publish:%s,%s\r\n\r\n", mqtt_params.pubtopic, (char *)pubmessage.payload);
46.             run_status = KEEPALIVE;
47.         }
48.         break;
49.     }
50.     case KEEPALIVE: {
51.         if (MQTTYield(&c, 30) != SUCCESSS) /* keepalive MQTT */
52.         {
53.             run_status = ERR;
54.         }
55.         delay_ms(100);
56.     }
57.     case RECV: {
58.         if (mqtt_recv_flag)
59.         {
60.             mqtt_recv_flag = 0;
61.             json_decode(mqtt_recv_msg);
62.         }
63.         delay_ms(100);
64.         break;
65.     }
66.     case ERR: /* Running error */
67.         printf("system ERROR!");
68.         delay_ms(1000);
69.         break;
70.  
71.     default:
72.         break;
73.     }
74. }

The do_mqtt() function will execute a state machine and operate according to the following steps:

  1. CONN: Perform the connection operation. Use the parameters we have initialized to execute the connection to the server using the MQTTConnect() function. Once the connection is successful, proceed to the step of subscribing to the topic.
  2. SUB: In this step, we will use the MQTTSubscribe() function to subscribe to the Alibaba Cloud's object model setting attribute topic. In this function, the client structure, the topic name to subscribe to, the QoS level for subscription, and the callback function messageArrived() for the subscribed topic need to be passed in. After the subscription is successful, proceed to the step of publishing messages.

After receiving the message of the subscription topic, the callback function messageArrived() will be executed. Here, we print out the received topic and message content, and copy the message into the mqtt_recv_msg array. The specific function content is as follows:

 1. void messageArrived(MessageData *md)
 2. {
 3.     char topicname[64] = {0};
 4.     char msg[512]      = {0};
 5.     sprintf(topicname, "%.*s", (int)md->topicName->lenstring.len, md->topicName->lenstring.data);
 6.     sprintf(msg, "%.*s", (int)md->message->payloadlen, (char *)md->message->payload);
 7.     printf("recv:%s,%s\r\n\r\n", topicname, msg);
 8.  
 9.     mqtt_recv_flag = 1;
10.     memset(mqtt_recv_msg, 0, sizeof(mqtt_recv_msg));
11.     memcpy(mqtt_recv_msg, msg, strlen(msg));
12. }
  1. PUB_MESSAGE: At this step, we will publish the current temperature to the object model on Alibaba Cloud. Once successful, we will proceed to the heartbeat and data reception sections.
  2. KEEPALIVE and RECV: At this step, we will continuously perform the heartbeat and data reception operations. Once data is received, it will enter the json_decode() function for processing.

The json_decode() function mainly uses CJSON to parse the received data and then perform the corresponding operations. The details are as follows:

 1. void json_decode(char *msg)
 2. {
 3.     cJSON *jsondata = NULL;
 4.     cJSON *params   = NULL;
 5.     cJSON *LED      = NULL;
 6.     jsondata        = cJSON_Parse(msg);
 7.     if (jsondata == NULL)
 8.     {
 9.         printf("json parse fail.\r\n");
10.         return;
11.     }
12.     params = cJSON_GetObjectItem(jsondata, "params");
13.     LED   = cJSON_GetObjectItem(params, "LEDSwitch");
14.     if (LED->valueint == 1)
15.     {
16.         printf("LED ON\r\n");
17.     }
18.     else
19.     {
20.         printf("LED OFF\r\n");
21.     }
22.     cJSON_Delete(jsondata);
23. }

11 Run results

After the burning routine was executed, the PHY link was first detected, then the network information was set through DHCP, and finally a loopback test was conducted by connecting to the MQTT server, as shown in the following figure:

图片1

12 Summary

This article explains how to implement the MQTT protocol on the W55MH32 chip and connect to the Alibaba Cloud platform. Through practical examples, it demonstrates the complete process from preparation, connection configuration, message subscription, publishing, and reception processing. The article details the concepts, characteristics, application scenarios, publish/subscribe mode, QoS levels of the MQTT protocol, as well as knowledge related to the Alibaba Cloud Internet of Things platform and object models, helping readers understand its practical application value in the interaction between IoT devices and cloud data.

The next article will explain how to implement the MQTT protocol on the W55MH32 and connect to the OneNET platform, and achieve data interaction with the OneNET object model. Stay tuned! 

 

 

WIZnet is a non-fabrication semiconductor company founded in 1998. Its products include the Internet processor iMCU™, which adopts TOE (TCP/IP Offloading Engine) technology and is based on a unique patented fully hardwired TCP/IP. iMCU™ is designed for embedded Internet devices in various applications.

WIZnet has over 70 distributors worldwide, with offices in Hong Kong, South Korea, and the United States, providing technical support and product marketing.

The region managed by the Hong Kong office includes: Australia, India, Turkey, and Asia (excluding South Korea and Japan).

Documents
Comments Write