在树莓派 Pico 2 W 上使用 Rust 开发 Matter Wi-Fi 智能灯泡
A Matter Wi-Fi Light Bulb in Rust on the Raspberry Pi Pico 2 W

原始链接: https://github.com/melastmohican/rust-rpico2-embassy-examples

本仓库提供了一系列适用于 **Raspberry Pi Pico 2 (RP2350)** 的 Rust 示例,利用 **Embassy 异步框架**实现高效、非阻塞的硬件交互。 这些示例展示了多种外设接口及项目实现: * **传感器:** 涵盖了 **HS3003** (I2C)、**ADXL345** 加速度计 (I2C)、**DS18B20** 温度探头 (1-Wire) 和 **DHT11** (数字协议) 的数据采集。注意,对时间敏感的传感器使用了自定义且周期精确的 `PreciseDelay` 实现。 * **显示屏:** 包含 Adafruit 2.2 英寸 TFT LCD 接口,其中一个复杂示例展示了如何利用物理引擎渲染动画雪花,并使用 DMA 实现高性能、非阻塞的显示更新。 * **物联网与连接:** 展示了适用于 Pico 2 W 的 **Matter 兼容 Wi-Fi 灯泡**,支持通过 BLE 配网和 Wi-Fi,实现与 Apple Home、Google Home 和 Home Assistant 等生态系统的安全集成。 * **实用程序:** 包含标准的 GPIO 闪灯示例。 这些项目充分利用了 RP2350 的双核架构,展示了如何在嵌入式 Rust 中管理复杂的时序和异步任务。所有项目均可使用提供的 [pico2-template](https://github.com/ImplFerris/pico2-template.git) 轻松引导启动。

开发者“melastmohican”发布了一个开源项目,展示了如何使用全新的树莓派 Pico 2 W (RP2350) 构建符合 Matter 标准的智能灯泡。通过利用 `rs-matter` 协议栈和 `embassy` 异步框架,该项目实现了一个完全本地化、无需云端的智能家居设备,并可直接集成到 Apple Home、Google Home 和 Home Assistant 等平台中。 主要技术成果包括: * **裸机 Rust:** 该项目完全在 `no_std` 环境下运行,证明了无需官方复杂的 C++ SDK 也能进行 Matter 开发。 * **无线电共存:** 开发者成功解决了在 CYW43439 芯片上同时运行 BLE(用于配网)和 Wi-Fi(用于 Matter 通信)的挑战,包括通过调整时钟分频器来防止更快的 RP2350 出现总线损坏。 * **异步复用:** 该实现采用了稳健的异步运行机制,以安全地管理无线硬件。 对于希望使用 Rust 构建自定义本地智能家居硬件的开发者而言,该项目是一个极具说服力的概念验证。感兴趣的开发者可在 GitHub 上获取源代码,进行物联网硬件的深度定制。
相关文章

原文

This repository contains examples for the Raspberry Pi Pico 2 (RP2350) board, written in Rust using the Embassy async framework.

cargo generate --git https://github.com/ImplFerris/pico2-template.git --name rust-rpico2-embassy-examples

Board: Raspberry Pi Pico 2

  • MCU: RP2350 (Dual-core Arm Cortex-M33 and RISC-V cores)
  • On-board peripherals:

Raspberry Pi Pico 2 Pinout

  • I2C pins:
    • I2C0 SDA: GPIO4
    • I2C0 SCL: GPIO5
    • I2C1 SDA: GPIO2
    • I2C1 SCL: GPIO3
  • UART pins:
    • UART0 TX: GPIO0, UART0 RX: GPIO1
    • UART1 TX: GPIO8, UART1 RX: GPIO9

Reads temperature and humidity from an HS3003 sensor using the Embassy async framework.

cargo run --example hs3003_i2c

Wiring (Arduino Modulino Thermo):

     Modulino -> RPi Pico 2
----------    --------------
GND (black) -> GND
VCC (red)   -> 3.3V
SCL (yellow)-> GPIO5 (Pin 7) (I2C0 SCL)
SDA (blue)  -> GPIO4 (Pin 6) (I2C0 SDA)

About HS3003:

The Renesas HS3003 is a high-performance temperature and humidity sensor:

  • Temperature range: -40°C to +125°C (±0.2°C accuracy)
  • Humidity range: 0% to 100% RH (±1.5% accuracy)
  • 14-bit resolution for both measurements
  • Ultra-low power consumption

Reads accelerometer data from an ADXL345 sensor over I2C0 using Embassy.

cargo run --example adxl345_i2c

Wiring:

     ADXL345 -> RPi Pico 2
----------    --------------
GND (black) -> GND
VCC (red)   -> 3.3V
SCL (yellow)-> GPIO5 (Pin 7) (I2C0 SCL)
SDA (blue)  -> GPIO4 (Pin 6) (I2C0 SDA)

About ADXL345:

The ADXL345 is a small, thin, low power, 3-axis accelerometer with high resolution (13-bit) measurement at up to ±16 g. Digital output data is formatted as 16-bit twos complement and is accessible through either an SPI (3- or 4-wire) or I2C digital interface.

Displays a 320x240 image of Zermatt on the Adafruit 2.2" TFT LCD display in landscape mode.

cargo run --example zermatt

Wiring (Eye-SPI Breakout):

     Raspberry Pi Pico 2              Eye-SPI Breakout
   +-----------------------+      +---------------------------+
   |                       |      |                           |
   |  3V3 (Pin 36) --------+------+-> VIN   (Red Wire)        |
   |  GND (Pin 38) --------+------+-> GND   (Black Wire)      |
   |  GPIO18 (Pin 24) -----+------+-> SCK   (Blue Wire)       |
   |  GPIO19 (Pin 25) -----+------+-> MOSI  (Green Wire)      |
   |  GPIO16 (Pin 21) -----+------+-> MISO  (Yellow Wire)     |
   |  GPIO20 (Pin 26) -----+------+-> DC    (White Wire)      |
   |  GPIO21 (Pin 27) -----+------+-> RST   (Orange Wire)     |
   |  GPIO17 (Pin 22) -----+------+-> TCS   (Blue Wire)       |
   |                       |      |                           |
   +-----------------------+      +---------------------------+

Displays a 320x240 image of Zermatt on the Adafruit 2.2" TFT LCD display with animated falling snow, utilizing a physics engine and the Embassy async framework to draw to an off-screen lcd-async framebuffer and dispatch via DMA without blocking the CPU.

cargo run --example zermatt_snow

Wiring is identical to the zermatt example.

Reads temperature from a DS18B20 waterproof temperature sensor probe over a 1-Wire bus using Embassy. It utilizes a custom, cycle-accurate PreciseDelay implementation to achieve jitter-free sub-microsecond timing required by the 1-Wire protocol on the RP2350's Cortex-M33 core.

cargo run --example ds18b20

Wiring Schematic:

                              Raspberry Pi Pico 2
                           +-----------------------+
                           |                       |
                           | [ ] 1      40 [ ] USB |
                           | [ ] 2      39 [ ]     |
                           | [ ] 3      38 [G]ND --+-------+ (black)
                           | [ ] 4      37 [ ]     |       |
                           | [ ] 5      36 [3]V3 --+---+   |
                           |  ...        ...       |   |   |
                           | [ ] 20     21 [ ] ----+---+---|---+ (white, GPIO16)
                           +-----------------------+   |   |   |
                                                       |   |   |
                                                       |   |   |
                   +-----------------------------+     |   |   |
                   |     DS18B20 Sensor / Probe  |     |   |   |
                   |      (Bottom/Flat Side)     |     |   |   |
                   |                             |     |   |   |
                   |     [GND]   [DAT]   [VCC]   |     |   |   |
                   +-------|-------|-------|-----+     |   |   |
                           |       |       |           |   |   |
                           |       +-------+--[5K1]----+   | (Pull-Up Resistor
                           |       |       |   Resistor    |  between DAT & VCC)
                           |       |       +---------------+ (red)
                           +-------|-----------------------+ (black)
                                   |
                                   +--------------------------- (white)

Breadboard Layout:

DS18B20 Breadboard Wiring Layout

About DS18B20:

The DS18B20 is a 1-Wire digital thermometer that provides 9-bit to 12-bit Celsius temperature measurements. It communicates over a 1-Wire bus, requiring only one data line (and ground) to interface with the microcontroller. It has a temperature range of -55°C to +125°C with ±0.5°C accuracy from -10°C to +85°C.

Reads temperature and humidity from a DHT11 sensor using the Embassy async framework. It utilizes the async API of the dht-sensor crate combined with our cycle-accurate PreciseDelay implementation.

cargo run --example dht11 --release

Note: Due to timing sensitivity of the DHT11 protocol during the bit-read phase, you must run this example in release mode.

Wiring Schematic:

                     Raspberry Pi Pico 2             DHT11 Module
                   +---------------------+      +---------------------+
                   |                     |      |                     |
                   | GND (Pin 38) -------+----->| GND                 |
                   | 3V3 (Pin 36) -------+----->| VCC                 |
                   | GPIO16 (Pin 21) ----+----->| DAT (Data)          |
                   |                     |      |                     |
                   +---------------------+      +---------------------+

Important

Pull-up Resistor:

  • If using a DHT11 module board: It likely already has a built-in pull-up resistor. No extra component is needed.
  • If using a bare 4-pin DHT11 sensor: You must add an external 4.7kΩ to 10kΩ pull-up resistor between the DAT (Data) and VCC lines.

About DHT11:

The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed). It has a temperature range of 0°C to 50°C (±2°C accuracy) and humidity range of 20% to 90% RH (±5% accuracy).

Implements a Matter-compatible Wi-Fi light bulb using the rs-matter stack. It uses BLE for commissioning and Wi-Fi for network connectivity, allowing you to add the Pico 2 W directly into Apple Home, Google Home, or Home Assistant! When toggled from your smart home app, it turns an external LED on and off.

cargo run --example matter_wifi_light --release

Provisioning in Home Assistant:

  1. Run the example on your Pico 2 W. It will begin advertising over Bluetooth.

  2. Open the Home Assistant companion app on your smartphone.

  3. Go to Settings -> Devices & Services -> Add Integration -> Add Matter device.

  4. When prompted for a setup code, enter the default 3497-0112-332 (or scan the QR code link printed in the terminal logs).

  5. Home Assistant will connect to the Pico 2 W over BLE, ask for your Wi-Fi credentials, and securely transmit them to the device.

    Home Assistant Provisioning
  6. The Pico 2 W will connect to your Wi-Fi network and immediately appear as a standard light bulb. You can use the Home Assistant interface to toggle the light on and off!

    Home Assistant Toggle
  7. The external LED wired to your Pico 2 W will instantly mirror the state!

    Matter Wi-Fi Light Circuit

Wiring Schematic:

           Raspberry Pi Pico 2 W                   External Components
         +------------------------+
         |                        |
         |         GP15 (Pin 20)  |---------[ 220-330 Ohm Resistor ]-----+
         |                        |                                      |
         |         GND (Pin 18)   |------------------[ LED - ] <---+     |
         |                        |                                |     |
         +------------------------+                     (Cathode / |     |
                                                         Short Leg) |     |
                                                                   |     |
                                                        [ LED + ] -+-----+
                                                        (Anode /
                                                         Long Leg)

Blinks an external LED connected to GPIO15. This is useful for boards like the Raspberry Pi Pico 2 W, where the onboard LED is connected to the wireless chip rather than a standard microcontroller GPIO.

cargo run --example blinky

Wiring: Same wiring as the matter_wifi_light example.

联系我们 contact @ memedata.com