---
title: "stm32f051-w5500-mqtt-pub-ex"
url: "https://maker.wiznet.io/Benjamin/projects/stm32f051-w5500-mqtt-pub-ex/"
markdown_url: "https://maker.wiznet.io/Benjamin/projects/stm32f051-w5500-mqtt-pub-ex/md"
type: "UCC: User Created Content"
author: "Avinash Gupta"
author_url: "https://github.com/avinash-gupta-in/stm32f051-w5500-mqtt-pub-ex"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "Avinash Gupta"
original_url: "https://github.com/avinash-gupta-in/stm32f051-w5500-mqtt-pub-ex"
published: "2024-08-05"
language: "en"
tags: ["W5500", "MQTT"]
hardware: ["STMicroelectronics STM32F051", "WIZnet W5500"]
likes: 0
views: 4465
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# stm32f051-w5500-mqtt-pub-ex

> Example/Demo code for MQTT Publication on STM32F051+W5500 Setup

Original author: Avinash Gupta (source: https://github.com/avinash-gupta-in/stm32f051-w5500-mqtt-pub-ex)

## Components

- **STMicroelectronics STM32F051** x 1
- **WIZnet W5500** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5500))
- Software: **MQTT**
- Software: **STMicroelectronics STM32CubeIDE**

WIZnet parts: W5500 ([Datasheet](https://docs.wiznet.io/Product/Chip/Ethernet/W5500/datasheet?utm_source=maker&utm_medium=project&utm_campaign=w5500), [product hub](https://maker.wiznet.io/products/w5500/))

## Documents and links

- [MQTT Publication](https://github.com/avinash-gupta-in/stm32f051-w5500-mqtt-pub-ex/blob/main/Core/Src/main.c) (code)

## Article

This project demonstrates the implementation of an MQTT publisher using the STM32F051 microcontroller and the WIZnet W5500 Ethernet chip. It provides a robust solution for IoT and M2M communication by publishing messages to an MQTT broker.

#### Project Overview:

- **Microcontroller**: STM32F051 ARM Cortex-M0 based MCU for low power and high performance.![](https://www.st.com/bin/ecommerce/api/image.PF267946.en.feature-description-include-personalized-no-cpn-medium.jpg)

- **Ethernet Chip**: WIZnet W5500 handles TCP/IP stack operations.

#### Features:

- **MQTT Protocol**: Uses Paho MQTT client library for lightweight messaging.

- **Network Configuration**: W5500 ensures stable network connections.

- **Platform**: Built with STM32CubeIDE for integrated development.

#### Functionality:

- **Initialization**: Configures system clocks, GPIO, SPI, and UART.

- **Network Setup**: Initializes W5500 and configures network settings.

- **MQTT Connection**: Connects to MQTT broker and publishes messages periodically.

- **Network Stability**: Checks and maintains Ethernet cable connection.

#### Key Code Snippets:

**MQTT Connection and Publishing**:

```c
connect_data.clientID.cstring = "iotencew55";
NewNetwork(&network, 1);
if (ConnectNetwork(&network, destination_ip, destination_port) != SOCK_OK) while (1);
MQTTClientInit(&mqtt_client, &network, 1000, sendbuff, 256, receivebuff, 256);
if (MQTTConnect(&mqtt_client, &connect_data) != MQTT_SUCCESS) while (1);
while (1) {
    MQTTPublish(&mqtt_client, "test/topic1", &msg);
    HAL_Delay(1000);
}
```

#### Applications:

- **IoT Devices**: For sending data to a central server or other devices.

- **M2M Communication**: Enables devices to send data and commands efficiently.

#### GitHub Repository:

- GitHub repository with source code, schematics, and detailed documentation.

By providing a comprehensive example of integrating the STM32F051 microcontroller with the WIZnet W5500 Ethernet chip for MQTT communication, this project serves as a valuable resource for developers working on IoT and embedded systems projects, particularly those focused on data transmission and communication.

---

Source: https://maker.wiznet.io/Benjamin/projects/stm32f051-w5500-mqtt-pub-ex/
