Introducing Swift Matter Examples

Hi Swift Community!

I'm thrilled to announce, along with @kubamracek and @Philippe_Hausler, the release of Swift Matter Examples, a new example project that demonstrates how to build a Matter smart light accessory using Embedded Swift on an ESP32-C6 devkit.

Check out detailed tutorials and setup instructions here and watch WWDC24 session 10197: Go small with Embedded Swift.

Please give the project a try and feel free to share your feedback and creations in this thread!

31 Likes

Love that embedded swift is now a topic at WWDC :tada:

2 Likes

I got the code for the matter smart light to work fine, thanks.

Where is the setup pairing code (2020-2021) specified? Or other HomeKit fields such as Manufacturer (currently, TEST_VENDOR) or Model (currently , TEST_PRODUCT).

This is all handled in the ESP Matter SDK, see the setup guide at https://docs.espressif.com/projects/esp-matter/en/latest/esp32c6/developing.html which documents these default values, and also how to change them.

Awesome! I got the Blink demo to work on an ESP32-C6 Thing Plus from SparkFun.

I had to customize the input GPIO from 8 to 23. I think we should have that be a parameter to pass into the LED.

  var config = led_driver_get_config()
  config.gpio = 23
  let handle = led_driver_init(&config)
  // config: gpio: 8 channel: 0)
  print("config: gpio: \(config.gpio) channel: \(config.channel))")
  led_driver_set_hue(handle, 40) // 240 blue // UInt16
  led_driver_set_saturation(handle, 100) //UInt8 0-100
  led_driver_set_brightness(handle, 5)
  led_driver_set_power(handle, true)
4 Likes

Nice! I'm trying to get all working on the same Things plus card but have problems getting everything in place. Did you go the docker path or "Get started on macOS"? I failed with docker - everything builds ok, but I can't flash the card from docker whatever I do, and, when trying the non-docker path, esp-matter fail to be cloned correctly due to "unable to checkout silabs/gecko_sdk". Tearing my hair...

"unable to checkout silabs/gecko_sdk"

That sounds like a temporary networking/server problem, there's a large amount of submodules involved when cloning esp-matter, it might take several tries to get it to clone fully.

I can't flash the card from docker whatever I do

Do you get any error messages? What's the exact symptom of the problem?

@kubamracek I can't figure out how to configure my Wi-Fi, which I assume is needed to run the Smart Light example.

It looks like a similar issue to this Github issue.

  1. Do I need to configure it separately from the Home app?
Log output
W (4430) wifi:Haven't to connect to a suitable AP now!
e[0;32mI (4430) chip[DL]: Attempting to connect WiFi station interfacee[0m
e[0;32mI (4440) chip[DL]: WiFi station state change: NotConnected -> Connectinge[0m
e[0;32mI (4440) chip[DL]: Done driving station state, nothing else to do...e[0m
W (4450) wifi:Haven't to connect to a suitable AP now!
e[0;31mE (4450) chip[DL]: Failed to get configured network when updating network status: Error ESP32:0x0500300Fe[0m
onfiguratione[0m
e[0;32mI (632) esp_core_dump_flash: Init core dump to flashe[0m
e[0;32mI (638) esp_core_dump_flash: Found partition 'coredump' @ 3e6000 65536 bytese[0m
e[0;32mI (648) esp_core_dump_flash: Core dump data checksum is correcte[0m
e[0;32mI (652) esp_core_dump_flash: Found core dump 3012 bytes in flash @ 0x3e6000e[0m
e[0;32mI (659) coexist: coex firmware version: d96c1e51fe[0m
e[0;32mI (665) coexist: coexist rom version 5b8dcfae[0m
e[0;32mI (670) main_task: Started on CPU0e[0m
e[0;32mI (670) main_task: Calling app_main()e[0m
🏎️   Hello, Embedded Swift! (Smart Light)
e[0;32mI (680) led_driver_ws2812: Initializing light drivere[0m
e[0;32mI (680) led_driver_ws2812: led set r:0, g:0, b:0e[0m
e[0;32mI (720) pp: pp rom version: 5b8dcfae[0m
e[0;32mI (720) net80211: net80211 rom version: 5b8dcfae[0m
I (730) wifi:wifi driver task: 4083d1d8, prio:23, stack:6656, core=0
I (730) wifi:wifi firmware version: 3e0076f
I (730) wifi:wifi certification version: v7.0
I (730) wifi:config NVS flash: enabled
I (740) wifi:config nano formating: disabled
I (740) wifi:mac_version:HAL_MAC_ESP32AX_761,ut_version:N
I (740) wifi:Init data frame dynamic rx buffer num: 32
I (750) wifi:Init static rx mgmt buffer num: 5
I (750) wifi:Init management short buffer num: 32
I (760) wifi:Init dynamic tx buffer num: 32
I (760) wifi:Init static tx FG buffer num: 2
I (770) wifi:Init static rx buffer size: 1700
I (770) wifi:Init static rx buffer num: 10
I (770) wifi:Init dynamic rx buffer num: 32

I see lots of Wi-Fi errors, but I don't know if I should be ignoring them.

  1. I am having trouble adding it as an accessory. I did it one time, and I think it may have received messages, but then subsequent attempts to re-add the Acessory just spin forever on "Connecting..."
Connected logs from previous Matter Light
e[0;32mI (1440) chip[DL]: Starting ESP WiFi layere[0m
W (1450) wifi:ACK_TAB0   :0x   90a0b, QAM16:0x9 (24Mbps), QPSK:0xa (12Mbps), BPSK:0xb (6Mbps)
W (1450) wifi:CTS_TAB0   :0x   90a0b, QAM16:0x9 (24Mbps), QPSK:0xa (12Mbps), BPSK:0xb (6Mbps)
W (1460) wifi:(agc)0x600a7128:0xd21acae6, min.avgNF:0xce->0xd2(dB), RCalCount:0x1ac, min.RRssi:0xae6(-81.62)
W (1470) wifi:(TB)WDEV_PWR_TB_MCS0:19
W (1470) wifi:(TB)WDEV_PWR_TB_MCS1:19
W (1480) wifi:(TB)WDEV_PWR_TB_MCS2:19
W (1480) wifi:(TB)WDEV_PWR_TB_MCS3:19
W (1480) wifi:(TB)WDEV_PWR_TB_MCS4:19
W (1490) wifi:(TB)WDEV_PWR_TB_MCS5:19
W (1490) wifi:(TB)WDEV_PWR_TB_MCS6:18
W (1490) wifi:(TB)WDEV_PWR_TB_MCS7:18
W (1500) wifi:(TB)WDEV_PWR_TB_MCS8:17
W (1500) wifi:(TB)WDEV_PWR_TB_MCS9:15
W (1500) wifi:(TB)WDEV_PWR_TB_MCS10:15
W (1510) wifi:(TB)WDEV_PWR_TB_MCS11:15
I (1510) wifi:11ax coex: WDEVAX_PTI0(0x55777555), WDEVAX_PTI1(0x00003377).

I (1520) wifi:mode : sta (40:4c:ca:4d:2a:80)
I (1520) wifi:enable tsf
W (1530) wifi:Haven't to connect to a suitable AP now!
e[0;32mI (1530) chip[DL]: Attempting to connect WiFi station interfacee[0m
e[0;32mI (1540) chip[DL]: WiFi station state change: NotConnected -> Connectinge[0m
e[0;32mI (1540) chip[DL]: Done driving station state, nothing else to do...e[0m
e[0;32mI (1550) chip[SVR]: Initializing subscription resumption storage...e[0m
e[0;32mI (1560) chip[SVR]: Server initializing...e[0m
e[0;32mI (1560) chip[TS]: Last Known Good Time: 2024-07-19T17:10:06e[0m
e[0;32mI (1570) chip[FP]: Fabric index 0x1 was retrieved from storage. Compressed FabricId 0x9F2C513D85FAD054, FabricId 0x00000000FE726992, NodeId 0x000000005B4E25A3, VendorId 0x1349e[0m
e[0;32mI (1590) chip[FP]: Fabric index 0x2 was retrieved from storage. Compressed FabricId 0x1189E72913B4E124, FabricId 0x00000000AE86A621, NodeId 0x000000008AE99CF7, VendorId 0x1384e[0m
e[0;32mI (1600) chip[DMG]: AccessControl: initializinge[0m
e[0;32mI (1610) chip[DMG]: Examples::AccessControlDelegate::Inite[0m
e[0;32mI (1610) chip[DMG]: AccessControl: settinge[0m
e[0;32mI (1620) chip[DMG]: DefaultAclStorage: initializinge[0m
e[0;32mI (1620) chip[DMG]: DefaultAclStorage: 2 entries loadede[0m
e[0;32mI (1650) chip[ZCL]: Using ZAP configuration...e[0m
e[0;32mI (1650) esp_matter_cluster: Cluster plugin init common callbacke[0m
e[0;32mI (1650) chip[DMG]: AccessControlCluster: initializinge[0m
e[0;32mI (1660) chip[ZCL]: 0x42146a1cep 0 clus 0x0000_0030 attr 0x0000_0000 not supportede[0m
e[0;32mI (1670) chip[ZCL]: Initiating Admin Commissioning cluster.e[0m
e[0;32mI (1670) chip[SVR]: Fabric already commissioned. Disabling BLE advertisemente[0m
e[0;32mI (1680) chip[IN]: CASE Server enabling CASE session setupse[0m
e[0;32mI (1690) chip[SVR]: Joining Multicast groupse[0m
e[0;32mI (1690) chip[SVR]: Server Listening...e[0m
e[0;32mI (1700) esp_matter_core: Dynamic endpoint 0 addede[0m
e[0;32mI (1700) esp_matter_attribute: ********** W : Endpoint 0x0001's Cluster 0x00000003's Attribute 0x00000001 is 0 **********e[0m
lightEndpoint.eventHandler:
unknown
0
e[0;32mI (1720) esp_matter_attribute: ********** W : Endpoint 0x0001's Cluster 0x00000004's Attribute 0x00000000 is 128 **********e[0m
lightEndpoint.eventHandler:
unknown
128
e[0;32mI (1730) esp_matter_attribute: ********** W : Endpoint 0x0001's Cluster 0x00000004's Attribute 0x0000FFFC is <invalid type: 0> **********e[0m
e[0;32mI (1750) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000006's Attribute 0x0000FFFC is 1 **********e[0m
e[0;32mI (1760) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000006's Attribute 0x00004003 is 0 **********e[0m
e[0;32mI (1770) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000006's Attribute 0x00000000 is 0 **********e[0m
e[0;32mI (1780) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000006's Attribute 0x00000000 is 0 **********e[0m
e[0;32mI (1790) chip[ZCL]: Endpoint 1 On/off already set to new valuee[0m
e[0;32mI (1800) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000008's Attribute 0x00000002 is 1 **********e[0m
e[0;32mI (1810) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000008's Attribute 0x00000003 is 254 **********e[0m
e[0;32mI (1820) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000008's Attribute 0x0000FFFC is 3 **********e[0m
e[0;32mI (1830) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000008's Attribute 0x00000000 is 64 **********e[0m
e[0;32mI (1850) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000008's Attribute 0x00004000 is 64 **********e[0m
e[0;32mI (1860) esp_matter_attribute: ********** W : Endpoint 0x0001's Cluster 0x00000008's Attribute 0x00000000 is 64 **********e[0m
lightEndpoint.eventHandler:
levelControl
64
e[0;32mI (1870) led_driver_ws2812: led set r:63, g:63, b:63e[0m
e[0;32mI (1880) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000300's Attribute 0x00004010 is 250 **********e[0m
e[0;32mI (1890) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000300's Attribute 0x00000007 is 250 **********e[0m
e[0;32mI (1900) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000300's Attribute 0x0000400B is 0 **********e[0m
e[0;32mI (1910) esp_matter_attribute: ********** R : Endpoint 0x0001's Cluster 0x00000300's Attribute 0x0000400C is 65279 **********e[0m
e[0;32mI (1930) esp_matter_attribute: ********** W : Endpoint 0x0001's Cluster 0x00000300's Attribute 0x00000007 is 250 **********e[0m
lightEndpoint.eventHandler:
colorControl(colorTemperatureMireds)
250
e[0;32mI (1940) led_driver_ws2812: led set r:63, g:51, b:40e[0m
e[0;32mI (1950) esp_matter_attribute: ********** W : Endpoint 0x0001's Cluster 0x00000300's Attribute 0x00000008 is 2 **********e[0m
lightEndpoint.eventHandler:
colorControl(colorMode)
2
e[0;32mI (1970) esp_matter_attribute: ********** W : Endpoint 0x0001's Cluster 0x00000300's Attribute 0x00004001 is 2 **********e[0m
e[0;32mI (1980) esp_matter_core: Dynamic endpoint 1 addede[0m
e[0;32mI (1990) chip[DL]: WIFI_EVENT_STA_STARTe[0m
W (1990) wifi:Haven't to connect to a suitable AP now!
e[0;32mI (1990) chip[DL]: Attempting to connect WiFi station interfacee[0m
E (2000) wifi:sta is connecting, return error
e[0;31mE (2000) chip[DL]: esp_wifi_connect() failed: ESP_ERR_WIFI_CONNe[0m
W (2010) wifi:Haven't to connect to a suitable AP now!
e[0;31mE (2020) chip[DL]: Failed to get configured network when updating network status: Error ESP32:0x0500300Fe[0m
e[0;32mI (2030) NimBLE: GAP procedure initiated: stop advertising.
e[0m
e[0;32mI (2030) chip[DL]: CHIPoBLE advertising stoppede[0m
e[0;32mI (2040) NimBLE: GAP procedure initiated: stop advertising.
e[0m
e[0;32mI (4050) chip[DL]: BLE deinit successful and memory reclaimede[0m
e[0;32mI (4300) chip[DL]: WIFI_EVENT_STA_DISCONNECTEDe[0m
e[0;32mI (4300) chip[DL]: WiFi station state change: Connecting -> Connecting_Failede[0m

  1. You mentioned that the device was setup for Wi-Fi before the video. Is this step necessary from the Matter guide?
matter esp wifi connect <ssid> <password>

I wonder if it would be better to start by first getting the C++ version of the smart light demo from ESP Matter to work (esp-matter/examples/light at main · espressif/esp-matter · GitHub). That way you can rule out any problems coming from the Swift specific setup.

That said, my understanding of your logs is that the device boots up fine, and the problematic step is getting the device to connect to your WiFi network. In my experience, there is a whole range of possible reasons for that and I'm not aware of a good way of distinguishing between them (they all just end up causing the device to fail to connect to WiFi):

  • WiFi is 5 GHz only (I think ESP32-C6 only supports 2.4 GHz WiFi)
  • WiFi has no password (it must have a password to be eligible for Matter)
  • WiFi has some atypical configuration making is not a "home network" (enterprise authentication, being a mobile hotspot, using captive logins, ...)
  • WiFi signal is too weak
  • ...I'm sure there's more possible problems...
1 Like

I figured it out! Thanks @kubamracek !
Since I set it up once before and removed it, the entry or ESP32 device was in a limbo state.

I removed it from the Home app and started over by resetting my ESP32-C6.

  1. Tap on the cog to remove the accessory from Home if it exists

  2. You can reset your device using this command.

     idf.py erase-flash
    
  3. Then retry and build and run

     idf.py build flash monitor
    
  4. The other thing I have to do is customize the GPIO pin for my LED to be able to control it in LED.swift

    init() {
      var config = led_driver_get_config()
      print("config.gpio: \(config.gpio)")
      config.gpio = 23 // default may be 8?
    
      let handle = led_driver_init(&config)
      led_driver_set_power(handle, true)
      self.handle = handle!
    }
    
1 Like

I seem to have a working installation now. :grinning: There's still a submodule that can't be built but it does not affect the example projets.

The esp_rfc2217_server tool could not put the card in bootloader mode. I don't have the error log left, but will give it another try later on.

The esp_rfc2217_server tool could not put the card in bootloader mode

I'd start by checking whether the device shows up on USB (ioreg -p IOUSB on macOS) and that a serial device is available for it in /dev. Does that seem to work fine?

This is what ioreg says:

+-o Root  <class IORegistryEntry, id 0x100000100, retain 37>
  +-o AppleT6000USBXHCI@00000000  <class AppleT6000USBXHCI, id 0x1000005cb, registered, matched, active, busy 0 (755 ms), retain 74>
  +-o AppleT6000USBXHCI@02000000  <class AppleT6000USBXHCI, id 0x1000005ba, registered, matched, active, busy 0 (14 ms), retain 39>
  +-o AppleT6000USBXHCI@01000000  <class AppleT6000USBXHCI, id 0x10000067f, registered, matched, active, busy 0 (927 ms), retain 73>
    +-o USB JTAG/serial debug unit@01100000  <class IOUSBHostDevice, id 0x10000982f, registered, matched, active, busy 0 (219 ms), ret$

And when I start flashing I get this output from client:

root@cda52c637ee9:/code/led-blink# idf.py -p rfc2217://host.docker.internal:4000?ign_set_control flash
Executing action: flash
Running ninja in directory /code/led-blink/build
Executing "ninja flash"...
[1/1] cd /code/led-blink/build/bootloa...-blink/build/bootloader/bootloader.bin
Bootloader binary size 0x54d0 bytes. 0x6b30 bytes (56%) free.
[4/10] Performing build step for 'chip_gn'
ninja: no work to do.
[7/8] cd /code/led-blink/build/esp-idf...le.bin /code/led-blink/build/light.bin
light.bin binary size 0x28f80 bytes. Smallest app partition is 0x1e0000 bytes. 0x1b7080 bytes (91%) free.
[7/8] cd /root/esp/esp-idf/components/...nents/esptool_py/run_serial_tool.cmake
esptool.py --chip esp32c6 -p rfc2217://host.docker.internal:4000?ign_set_control -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 80m --flash_size 4MB 0x0 bootloader/bootloader.bin 0x20000 light.bin 0xc000 partition_table/partition-table.bin 0x1d000 ota_data_initial.bin
esptool.py v4.7.0
Serial port rfc2217://host.docker.internal:4000?ign_set_control
Connecting...
Device PID identification is only supported on COM and /dev/ serial ports.
..............................
A serial exception error occurred: timeout while waiting for option 'purge'
Note: This error originates from pySerial. It is likely not a problem with esptool, but with the hardware connection or drivers.
For troubleshooting steps visit: https://docs.espressif.com/projects/esptool/en/latest/troubleshooting.html
CMake Error at run_serial_tool.cmake:66 (message):
/root/.espressif/python_env/idf5.2_py3.10_env/bin/python;;/root/esp/esp-idf/components/esptool_py/esptool/esptool.py;--chip;esp32c6
failed.
FAILED: CMakeFiles/flash /code/led-blink/build/CMakeFiles/flash
cd /root/esp/esp-idf/components/esptool_py && /usr/local/lib/python3.10/dist-packages/cmake/data/bin/cmake -D IDF_PATH=/root/esp/esp-idf -D "SERIAL_TOOL=/root/.espressif/python_env/idf5.2_py3.10_env/bin/python;;/root/esp/esp-idf/components/esptool_py/esptool/esptool.py;--chip;esp32c6" -D "SERIAL_TOOL_ARGS=--before=default_reset;--after=hard_reset;write_flash;@flash_args" -D WORKING_DIRECTORY=/code/led-blink/build -P /root/esp/esp-idf/components/esptool_py/run_serial_tool.cmake
ninja: build stopped: subcommand failed.
ninja failed with exit code 1, output of the command is in the /code/led-blink/build/log/idf_py_stderr_output_8022 and /code/led-blink/build/log/idf_py_stdout_output_8022

And this is the output from the server:

ejeklint@Pers-MacBook-Pro-2021 swift-matter-examples % esp_rfc2217_server.py -v -p 4000 /dev/tty.usbmodem1101
INFO:root:RFC 2217 TCP/IP to Serial redirector - type Ctrl-C / BREAK to quit
INFO:root:Serving serial port: /dev/tty.usbmodem1101
INFO:root:TCP/IP port: 4000
INFO:root:Connected by 127.0.0.1:61042
INFO:rfc2217.server:client accepts RFC 2217
INFO:rfc2217.server:NOTIFY_MODEMSTATE: False
INFO:rfc2217.server:client accepts RFC 2217
INFO:rfc2217.server:NOTIFY_MODEMSTATE: False
INFO:rfc2217.server:set baud rate: 9600
INFO:rfc2217.server:set data size: 8
INFO:rfc2217.server:set parity: N
INFO:rfc2217.server:set stop bits: 1
INFO:rfc2217.server:changed flow control to None
INFO:rfc2217.server:Activating reset in thread
INFO:rfc2217.server:purge in
INFO:rfc2217.server:purge out
INFO:rfc2217.server:set baud rate: 115200
INFO:rfc2217.server:set data size: 8
INFO:rfc2217.server:set parity: N
INFO:rfc2217.server:set stop bits: 1
INFO:rfc2217.server:changed flow control to None
INFO:rfc2217.server:set baud rate: 115200
INFO:rfc2217.server:set data size: 8
INFO:rfc2217.server:set parity: N
INFO:rfc2217.server:set stop bits: 1
INFO:rfc2217.server:changed flow control to None
INFO:rfc2217.server:purge in
INFO:rfc2217.server:Activating reset in thread
INFO:rfc2217.server:purge in
INFO:rfc2217.server:purge out
INFO:rfc2217.server:set baud rate: 115200
INFO:rfc2217.server:set data size: 8
INFO:rfc2217.server:set parity: N
INFO:rfc2217.server:set stop bits: 1
INFO:rfc2217.server:changed flow control to None
INFO:rfc2217.server:set baud rate: 115200

And so on until it times out.

@ejeklint I don't know if you made any more progress.

I posted two videos and a blog post that document my install and setup process.

Did you install the any serial drivers? This may or may not help: How to Troubleshoot Embedded Swift and the Matter Smart Light

After installing that driver, I'm able to see the serial device at /dev/cu.usbmodem2101

To look for the serial devices attached to your Mac, use the commands:

ls /dev/cu.*
ls /dev/*

You may need to reboot after installing those drivers.

@PaulSolt Thank's for the head-up! I had to put this aside for a while, but am back at it now. Building the led-blink swift example has worked just fine, no issues flashing and running. But I've been with no luck building the matter/light example. It doesn't compile chip_gn module and I can't figure out what's missing. I'm going to try again after a complete cleanup of my environment. Also: have cleaned my entire python setup, it was a royal mess...

1 Like

Whew. After two hours of cleaning my macbook, uninstalling/reinstalling python and the like... IT WORKS! :wine_glass:

2 Likes