Skip to main content

Working with MicroPython

This chapter contains the following sections. Please read as needed:

MicroPython Getting Started Tutorial

New to Pico MicroPython development and want to get started quickly? We have prepared a general introductory tutorial for you. This tutorial is designed to help developers quickly become familiar with Thonny IDE and start developing. It covers environment setup, project creation, component usage, and peripheral programming, helping you take the first step in MicroPython programming.

Setting Up the Development Environment

Please refer to the Install and Configure Thonny IDE Tutorial to download and install the Thonny IDE.

Example

The MicroPython examples are located in the examples/MicroPython directory of the example package.

ExampleBasic Program DescriptionDependency Library
01_MQTTMQTT example-

01_MQTT

Example Description

  • This example demonstrates how the RP2350-POE-ETH uses the W6300 Ethernet to connect to a public MQTT broker (broker.emqx.io:1883 by default) under MicroPython, and implements:
    • Subscribe topic: Sub/ed229f15
    • Publish topic: Pub/ed229f15
    • Publish a device status JSON once on power‑up, and then every 10 seconds thereafter (including CPU frequency, temperature, VSYS voltage, USB/POE insertion status, uptime, etc.)
  • Open-source usage notes (strongly recommended to modify on first use to avoid conflicts among multiple people/devices):
    • MQTT Brokers typically require that the Client ID be globally unique at any given time: if two devices use the same MQTT_CLIENT_ID to log in, the later one will kick off the earlier one, resulting in frequent disconnections.
    • If topics use fixed values, data from multiple devices will be mixed together and can easily be accidentally subscribed to or controlled by others.
    • It is recommended to modify at least: MQTT_CLIENT_ID, MQTT_SUB_TOPIC, and MQTT_PUB_TOPIC. If you set up your own broker or enable authentication, also modify MQTT_USERNAME and MQTT_PASSWORD.

In the source code for this example, the relevant configuration is located in:

  • main.py: Topic definitions + reading MQTT parameters from config.py
# Constants for MQTT Topics
MQTT_SUB_TOPIC = 'Sub/ed229f15'
MQTT_PUB_TOPIC = 'Pub/ed229f15'

# MQTT Parameters
MQTT_SERVER = config.mqtt_server
MQTT_PORT = config.mqtt_port
MQTT_USERNAME = config.mqtt_username
MQTT_PASSWORD = config.mqtt_password
MQTT_CLIENT_ID = config.mqtt_client_id
MQTT_KEEPALIVE = config.mqtt_keepalive
  • config.py: MQTT broker and account information
mqtt_server = b'broker.emqx.io'
mqtt_port = 1883
mqtt_client_id = b'ed229f15'
mqtt_keepalive = 60
mqtt_username = b'w6300_test'
mqtt_password = b'0123456789'

Hardware Connection

  • Connect the development board to your computer via USB (for power, flashing, and serial log viewing).
  • Plug an Ethernet cable into the RJ45 port and ensure the network can reach the public internet (able to access broker.emqx.io:1883).
  • Optional: Use a PoE Ethernet cable to power the board (the program will reflect the poe_inserted status in the reported JSON).

Code Analysis

  • Entry file: main.py
    • Ethernet initialization ethernet_init()
      • network.WIZNET6K() initializes the Ethernet interface
      • nic.ifconfig('dhcp') enables DHCP to obtain an IP address and waits for nic.isconnected()
    • MQTT connection mqtt_connect()
      • Uses from umqtt.simple import MQTTClient
      • After successful connection, sets the callback client.set_callback(mqtt_recv_callback) and subscribes to MQTT_SUB_TOPIC
    • Publish and subscribe topics (defined by default as constants at the top of main.py):
MQTT_SUB_TOPIC = 'Sub/ed229f15'
MQTT_PUB_TOPIC = 'Pub/ed229f15'
  • Reporting (once at startup + every 10 seconds)
    • PUBLISH_INTERVAL = 10
    • The published JSON consists of three parts: device/system/message (note: get_network_info() is implemented in the current code but not yet included in the payload; you can extend it as needed):
{
"device": { "chip": "RP2350A", "client_id": "ed229f15" },
"system": {
"cpu_mhz": 150,
"ram_kb": 520,
"temp_c": 36.2,
"vsys_voltage_v": 5.021,
"vsys_adc_raw": 2048,
"usb_inserted": true,
"poe_inserted": false,
"uptime": "0:00:01:23"
},
"message": "Hello, waveshare!"
}
  • Subscription message callback mqtt_recv_callback(topic, message)
    • Upon receiving a message on the subscribed topic, it attempts to parse the payload as JSON and executes commands according to the fields (supports multiple fields in a single payload):
      • {"adc": 1}: reads VSYS voltage (GPIO29 ADC) and replies to Pub/...
      • {"led": 2}: sets WS2812 color (GPIO25, values 0-8)
      • {"io_dect": 1}: reads USB/POE insertion status (GPIO24/23) and replies to Pub/...
    • Reply examples:
{"vsys_voltage":5.021,"vsys_adc_raw":2048}
{"led_color":2,"led_name":"RED"}
{"usb_inserted":true,"poe_inserted":false}
  • MQTT library lib/umqtt/
    • simple.py: basic MQTT protocol implementation (connect/publish/subscribe/wait_msg/check_msg)
    • robust.py: adds automatic reconnection logic on top of simple.py (this example currently uses umqtt.simple; you can switch to robust for more resilience if needed)

Operation Result

Before running, please complete the Hardware Connections and follow the steps below for programming and deployment:

  1. Flash the MicroPython Firmware (UF2)
    • After entering BOOT mode, drag-and-drop/copy the RP2350-POE-ETH.uf2 file from the firmware/MicroPython directory onto the development board's USB drive to complete programming.
  2. Upload the example code to the board's file system and run main.py in Thonny
    • Files in the examples/MicroPython/01_MQTT directory:
      • main.py (MicroPython auto‑start entry on power‑up)
      • config.py
      • lib/umqtt/simple.py
      • lib/umqtt/robust.py
    • Note: The lib/ directory structure must be preserved; the final layout on the board should resemble:
/
main.py
config.py
lib/
umqtt/
simple.py
robust.py
  1. After programming and uploading the files, reset the board. The serial port will print logs similar to the following (may vary slightly depending on the network environment):
Initialize Ethernet (W6300)
Waiting for Ethernet connection...
Ethernet connection successful!
IP address: 192.168.1.100
Subscribe to topic: Sub/ed229f15
Published: {...} -> Pub/ed229f15
...
  1. Verify with any MQTT client (MQTTX recommended)

    • MQTTX client settings:

    photo0

    • RP2350-POE-ETH connection parameters:
      • Host: broker.emqx.io
      • Port: 1883
      • Username: w6300_test
      • Password: 0123456789
      • Client ID: ed229f15
    • Subscribe to topic: Pub/ed229f15 to receive the JSON reported by the board every 10 seconds.
    • Publish control commands (payload as a JSON string) to the topic Sub/ed229f15, for example:
{"led":2}
{"adc":1,"io_dect":1}

photo