---
title: "WIZnet W5300-TOE MQTT publish and Subscribe (micropython)"
url: "https://maker.wiznet.io/scarlet/projects/wiznet-w5300-toe-mqtt-publish-and-subscribe-micropython/"
markdown_url: "https://maker.wiznet.io/scarlet/projects/wiznet-w5300-toe-mqtt-publish-and-subscribe-micropython/md"
type: "UCC: User Created Content"
author: "scarlet"
original_author: "scarlet"
original_url: "https://github.com/Wiznet/W5300-TOE-MicroPython/tree/main/example/MQTT"
published: "2023-02-14"
language: "en"
tags: ["AIoT", "MQTT", "toe-design-contest-ref"]
hardware: ["WIZnet W5300"]
likes: 7
views: 5335
comments: 5
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# WIZnet W5300-TOE MQTT publish and Subscribe (micropython)

> WIZnet W5300-TOE MQTT publish and Subscribe with Nucleo-f429zi

Original author: scarlet (source: https://github.com/Wiznet/W5300-TOE-MicroPython/tree/main/example/MQTT)

## Components

- **WIZnet W5300** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5300))
- Software: **MicroPython** ([docs](http://micropython.org/))

## Documents and links

- [W5300-TOE MQTT example in offisial site](https://github.com/Wiznet/W5300-TOE-MicroPython/tree/main/example/MQTT) (code)
- [WIZNET5K FW for W5300 with STM32F4xx](https://github.com/Wiznet/W5300-TOE-MicroPython/releases/tag/Release_1_0) (code)

## Article

These sections will guide you through a series of steps from configuring development environment to running ethernet examples using the STM32f429zi (nuleo-f429zi) with W5300-TOE

Please refer to the 'Getting Started' guide for the basic settings

https://github.com/Wiznet/W5300-TOE-MicroPython/blob/main/static/GettingStart.md

### **Hardware requirements**

The ethernet examples use **NUCLEO-F429ZI** build on STM32 MCU with WIZnet's [**W5300**](https://docs.wiznet.io/Product/iEthernet/W5300)ethernet chip. If you use other STM board, supported board of STMicroelectronics micropython in [**LIST**](https://micropython.org/stm32/)and check that the board supports FMC 16bit data

### **W5300-TOE**

* W5300-TOE shield's product page is not surported yet, I will add it when it is provided.

W5300 product page is [Here](https://docs.wiznet.io/Product/iEthernet/W5300)

### **NUCLEO-F429ZI**

`**STM32F429ZI**` This document is based on STM32F429ZI. If you use other STM board, Please edit it to your HW configuration.

![](https://hackster.imgix.net/uploads/attachments/1557020/image_74LCywaZcf.png?auto=compress%2Cformat&w=740&h=555&fit=max)

- FMC Data pin (0-8 or 0-16)

- FMC Address pin (0-10)

- FMC Control GPIO pin (NE, NWE, NOE)

- WIZChip Control GPIO pin (INT, RST)

- UART3 pin(connect to ST-LINK) -- Be changed for use the FMC on the Nucleoboard. Pin D8, D9 -> Pin C10, C11

===================================================================

**Wiznet5K (micropython Library for wiznet product)**

W5300 is not included in the FW provided by microthon. So you have to use the FW provided by WIZnet.

Please refer to the getting start on the official site for downloading and editing of W5300 WIZNET5K FW ([Click Here](https://github.com/Wiznet/W5300-TOE-MicroPython/blob/main/static/GettingStart.md))

===================================================================

![](https://hackster.imgix.net/uploads/attachments/1557024/image_NYf9VL7LvA.png?auto=compress%2Cformat&w=740&h=555&fit=max)

**Step 1: Prepare Software**

The following serial terminal program is required for **MQTT Publish** test, download and install from below links.

- [**Thonny**](https://thonny.org/)

- [Mosquitto](https://mosquitto.org/download/)

In Mosquitto versions earlier than 2.0 the default is to allow clients to connect without authentication. In 2.0 and up, you must choose your authentication options explicitly before clients can connect. Therefore, if you are using version 2.0 or later, refer to following link to setup 'mosquitto.conf' in the directory where Mosquitto is installed.

![](https://hackster.imgix.net/uploads/attachments/1557029/image_Sw3fLqXiap.png?auto=compress%2Cformat&w=740&h=555&fit=max)

[**Authentication Methods**](https://mosquitto.org/documentation/authentication-methods/)

### **Step 2: Prepare hardware**

- Combine WIZnet W5300-TOE Ethernet Shield and NUCLEO-F429ZI STM Board

- Connect ethernet cable to W5300-TOE ethernet port.

- Connect NUCLEO-F429ZI to desktop or laptop using 5 pin micro USB cable.

### **Step 3: Setup MQTT Publish Example**

To test the **MQTT Publish example**, minor settings shall be done in code.

get basic code Here: https://github.com/Wiznet/W5300-TOE-MicroPython/tree/main/example/MQTT

Check COMport in [‘Configure interpreter …] and then open **Thonny** Python IDE.

![](https://hackster.imgix.net/uploads/attachments/1557025/thonny-setup_example.png?auto=compress%2Cformat&w=740&h=555&fit=max)

- Copy the *umqttsimple* library code into it. You can access the *umqttsimple* library code in the following link ([Click Here](https://github.com/micropython/micropython-lib/blob/master/micropython/umqtt.simple/umqtt/simple.py))

- If [umqttsimple.py], which is officially open on MicroPython, does not work, use the code in the link ([Click Here](https://github.com/micropython/micropython-lib/blob/master/micropython/umqtt.simple/umqtt/simple.py))

Initialize ethernet interface and configuration. Open the example code to set **[Network Information]** in main()

> nic.ifconfig(('IP Address', 'Netmask', 'Gateway', 'DNS'))

```plaintext
from wiznet_conf import wiznet5k_w5300
		...

def main():
    w5300=wiznet5k_w5300()
    w5300.w5300_set_ip('192.168.11.104','255.255.255.0','192.168.11.1','8.8.8.8')
    ...
```

- In the MQTT configuration, the [broker IP] address is the [IP of your desktop.]

```plaintext
#mqtt config
mqtt_server = '192.168.1.11'
client_id = 'wiz'
topic_pub = b'hello'
topic_msg = b'Hello Wiznet'

last_message = 0
message_interval = 5
counter = 0

def mqtt_connect():
    client = MQTTClient(client_id, mqtt_server, keepalive=60)
    client.connect()
    print('Connected to %s MQTT Broker'%(mqtt_server))
    return client
```

- going to use MQTT Publish.

```plaintext
#MQTT Publish
def main():

    try:
        client = mqtt_connect()
    except OSError as e:
        reconnect()
    
    while True:
        client.publish(topic_pub, topic_msg)
        time.sleep(3)
        
    client.disconnect()
```

### **Step 4: Upload and Run MQTT Publish**

Create broker using mosquitto by executing the following command. If the broker is created normally, the broker's IP address is the current IP of your desktop or laptop, and the port is 1883 by default.

```plaintext
mosquitto -c mosquitto.conf -p 1883 -v
```

![](https://hackster.imgix.net/uploads/attachments/1557026/MQTT_pub1.png?auto=compress%2Cformat&w=740&h=555&fit=max)

If the MQTT publish example works normally on NUCLEO-F429ZI with W5300-TOE, you can see the network information of W5300-TOE, connecting to the broker and publishing the message.

![](https://hackster.imgix.net/uploads/attachments/1557027/MQTT_pub2.png?auto=compress%2Cformat&w=740&h=555&fit=max)

Subscribe to the broker with the above command. Subscribe will receive a message from the broker.

```plaintext
mosquitto_sub -h 192.168.1.11 -t hello
```

![](https://hackster.imgix.net/uploads/attachments/1557028/MQTT_pub3.png?auto=compress%2Cformat&w=740&h=555&fit=max)

### **Step 5: Setup MQTT Subscribe Example**

To test the **MQTT Subscribe example**, minor settings shall be done in code.

***The thonny setup and main() are the same as for publish

going to use MQTT Subscribe.

```plaintext
#subscribe
def main():

    try: 
        client = mqtt_connect()
    except OSError as e:
        reconnect()

    while True:
        client.subscribe(topic_sub)
        time.sleep(1)

    client.disconnect()
```

### **Step 4: Upload and Run MQTT Subscribe**

Create broker using mosquitto by executing the following command. If the broker is created normally, the broker's IP address is the current IP of your desktop or laptop, and the port is 1883 by default.

```plaintext
mosquitto -c mosquitto.conf -p 1883 -v
```

![](https://hackster.imgix.net/uploads/attachments/1557031/MQTT_pub1.png?auto=compress%2Cformat&w=740&h=555&fit=max)

If the MQTT publish example works normally on Raspberry Pi Pico, you can see the network information of Raspberry Pi Pico, connecting to the broker and publishing the message.

![](https://hackster.imgix.net/uploads/attachments/1557032/MQTT_sub_2.png?auto=compress%2Cformat&w=740&h=555&fit=max)

Subscribe to the broker with the above command. Subscribe will receive a message from the broker.

```plaintext
mosquitto_pub -h 192.168.1.20 -t hello -m "Hello Pico"
```

![](https://hackster.imgix.net/uploads/attachments/1557033/MQTT_sub_3.png?auto=compress%2Cformat&w=740&h=555&fit=max)

---

Source: https://maker.wiznet.io/scarlet/projects/wiznet-w5300-toe-mqtt-publish-and-subscribe-micropython/
