The W55MH32 uses an infrared detection sensor to realize an industrial counter.
The W55MH32 uses an infrared detection sensor to realize an industrial counter.
1. Introduction
MQTT is a lightweight communication protocol based on TCP/IP, employing a publish-subscribe model, and widely used in the Internet of Things (IoT) field.
MQTT's working principle revolves around three core components: Publishers, Brokers (also known as servers), and Subscribers. Publishers are responsible for sending messages to specific topics, while brokers receive these messages and forward them to all subscribers to that topic. This model allows asynchronous communication between devices, and devices do not need to be directly aware of each other's existence, thus reducing system complexity.
The W55MH32 is a newly launched high-performance Ethernet microcontroller from WIZnet. It uses a high-performance Arm® Cortex-M3 core with a maximum clock speed of 216MHz, and has built-in 1024KB FLASH and 96KB SRAM. Of particular note is its integration of the WIZnet TCP/IP offload engine (TOE), encompassing a full hardware TCP/IP protocol stack, MAC, and PHY. It also features a 32KB independent Ethernet transmit/receive buffer for eight hardware sockets, making it a true all-in-one solution.
2 Project Environment
2.1 Hardware Preparation
- W55MH32 module
- MH-B infrared sensor
- 1.44-inch TFT display
- One network cable
- Several DuPont wires
- Switch or router
2.2 Software Environment
- Example Links: MQTT & Aliyun
- FreeAT_2.0.0.exe
- Aliyun
- Keil5
2.3 Solution Diagram
3. Registering an Alibaba Cloud Account and Creating an Item Model
Account registration will not be detailed here. Below, we'll see how to create an item model.
3.1 Creating a Product
Search for IoT Platform -> Click IoT Platform -> Click Public Instances
Select Device Management -> Click Products -> Click Create Product
Set the product name, category, node type, connection method, and data format.
3.2 Adding a Device
After successfully creating the product, click Add Device -> Enter DeviceName
3.3 Creating an Item Model
Define the IoT model: Select the product -> Click Function Definition -> Click Go to Edit Draft
Click Add Custom Function -> Select Function Type -> Enter Function Name -> Set Identifier -> Set Data Type -> Set Value Range -> Set Step Size -> Set Unit -> Set Read/Write Type
3.4 Obtain Device Connection Information
(1) Click Product -> Select Created Product -> Click Topic Class List -> Select Item Model Communication
Subscribe to Platform Message Topics:
/sys/a1zJnP0k43p/${deviceName}/thing/service/property/set
Device Message Topics:
/sys/a1zJnP0k43p/${deviceName}/thing/event/property/post
Note: {device_id} in the image needs to be changed to your device's ID
1. Click Device Management -> Select Device -> Click View
2. Click View DeviceSecret to obtain the device certificate
{
"ProductKey": "a1zJnP0k43p",
"DeviceName": "Counter",
"DeviceSecret": "58bd729f76540474c04784f4cc5ec40c"
}
3. Obtain MQTT Connection Parameters
4. Example Modification
This example uses MQTT & Aliyun:
Locate the do_mqtt.c file and replace the parameters mentioned above with your own Aliyun parameters. These parameters have been discussed above and can be replaced as needed.
// Define and initialize the MQTT connection parameter structure
mqttconn mqtt_params = {
.mqttHostUrl = "a1zJnP0k43p.iot-as-mqtt.cn-shanghai.aliyuncs.com",
.server_ip = {
0,
}, /*Define the Connection Server IP*/
.port = 1883, /*Define the connection service port number*/
.clientid = "a1zJnP0k43p.Counter|securemode=2,signmethod=hmacsha256,timestamp=1755050066623|", /*Define the client ID*/
.username = "Counter&a1zJnP0k43p", /*Define the user name*/
.passwd = "d2805aed3358d684d451deefc48c9848c51b23313886d1e05b5b855569a289ce", /*Define user passwords*/
.pubtopic = "/sys/a1zJnP0k43p/Counter/thing/event/property/post", /*Define the publication message*/
.subtopic = "/sys/a1zJnP0k43p/Counter/thing/service/property/set", /*Define subscription messages*/
.pubQoS = QOS0, /*Defines the class of service for publishing messages*/
};
First, send the data packet for subscribing to the topic {device_id}:
/sys/a1zJnP0k43p/Counter/thing/service/property/set
After successful subscription, receive the message SUCCESSS from the topic.
case SUB: {
ret = MQTTSubscribe(&c, mqtt_params.subtopic, mqtt_params.subQoS, messageArrived); /* Subscribe to Topics */
printf("Subscribing to %s\r\n", mqtt_params.subtopic);
printf("Subscribed:%s\r\n\r\n", ret == SUCCESSS ? "success" : "failed");
if (ret != SUCCESSS)
{
run_status = ERR;
}
else
{
run_status = PUB_MESSAGE;
}
break;
}Next, proceed with data publishing. In this step, ensure that the Service ID (Temperature) and Attribute Name (CurrentTemperature) are completely identical to the product model you defined on Alibaba Cloud (including capitalization). After successful publishing, update the status to "Keep Connected".
case PUB_MESSAGE: {
// Dynamically build JSON payload
snprintf(payload_buffer, PAYLOAD_BUFFER_SIZE,
"{\"id\":\"123\",\"version\":\"1.0\","
"\"params\":{\"Counter\":%d,},"
"\"method\":\"thing.event.property.post\"}",
count);
pubmessage.qos = QOS0;
pubmessage.payload = payload_buffer;
pubmessage.payloadlen = strlen(payload_buffer);
ret = MQTTPublish(&c, (char *)&(mqtt_params.pubtopic), &pubmessage); /* Publish message */
if (ret != SUCCESSS)
{
run_status = ERR;
}
else
{ printf("publish:%s,%s\r\n\r\n", mqtt_params.pubtopic,payload_buffer);
run_status = KEEPALIVE;
}
break;
}
Completing these two steps will successfully connect to Alibaba Cloud and upload data there.
This project mainly uses two peripherals: an LCD screen and an infrared sensor. First, we initialize the LCD and the sensor. When an obstacle is detected, `infrared_getdata` returns 1; otherwise, it returns 0. Therefore, we can count the changes from 0 to 1 (rising edges). Additionally, to avoid affecting the main function's execution, we use interrupts.
Infrared Sensor Initialization
void infrared_init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(infrared_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = infrared_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(infrared_GPIO_PORT, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource5);
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
After initialization is complete, counting is performed via interrupt.
uint16_t infrared_getdata(void)
{
uint16_t tempData;
tempData = !GPIO_ReadInputDataBit(infrared_GPIO_PORT, infrared_GPIO_PIN);
return tempData;
}
void EXTI9_5_IRQHandler(void) {
if(EXTI_GetITStatus(EXTI_Line5) != RESET) {
delay_ms(2);
if(infrared_getdata() == 1) {
count++;
}
EXTI_ClearITPendingBit(EXTI_Line5);
}
}
The counter value can be refreshed in real time via the LCD screen, and data is uploaded to Alibaba Cloud every 5 seconds.
Finally, initialization is added in the main function, and a function is added in the while loop.
infrared_init(); // Initialize the infrared sensor
LCD_Init(); // Initialize the LCD
LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
LCD_ShowString(32,32,(u8 *)"Counter",BLACK,WHITE,16,0);
while (1)
{
do_mqtt();
LCD_ShowIntNum(48,48,count,2,BLACK,WHITE,16);
}5. Functional Verification
After program burning and hardware connection, as shown in the following figure:
With hardware connection complete, power on and print the following information via serial port assistant:
After successful power-on initialization, the counter will increment by 1 when an obstacle is detected, and the result will be simultaneously displayed on the LCD screen. Data is uploaded to the Alibaba Cloud server every 5 seconds.
6. Summary
This project successfully implemented a network industrial counter using the W55MH32, verifying the feasibility of operating on an Ethernet-based embedded cloud platform. Thank you for your patience in reading! If you have any questions during the reading process, or would like to learn more about this product and its applications, please feel free to leave a message via private message or in the comments section. We will reply to your message as soon as possible to provide you with more detailed answers and assistance!
———————————————— Copyright Notice: This article is an original article by CSDN blogger "Playing with Ethernet," and follows the CC 4.0 BY-SA copyright agreement. Please include the original source link and this statement when reprinting.
Original link: https://blog.csdn.net/2301_81684513/article/details/150493919
