Skip to content

Commit 5c21be6

Browse files
committed
examples/matter: defer rainmaker start until ble deinit
Starting the rainmaker agent alongside the matter may cause matter commissioning to fail due to low heap memory. This happens because matter CASE and rainmaker’s MQTT connection occur simultaneously, and both operations are memory-intensive, potentially leading to failures.
1 parent 892535a commit 5c21be6

9 files changed

Lines changed: 49 additions & 10 deletions

File tree

examples/matter/matter_light/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@
1414
> Please refer to the [README in the parent folder](../README.md) for instructions.
1515
1616
> Please refer to the [THREAD_INSTRUCTIONS](./THREAD_INSTRUCTIONS.md) for instructions of Thread devices(ESP32-H2 or ESP32-C6).
17+
18+
#### Optimization
19+
20+
TO optimize the DRAM usage, this example uses the following optimizations:
21+
22+
- Disables the chip shell, `CONFIG_ENABLE_CHIP_SHELL=n`, it adds approx 10KB or RAM.
23+
- As this example has two endpoints (Endpoint 0: Root endpoint, Endpoint 1: Extended Color Light), the default dynamic endpoint count is set to 2. This is done by setting `CONFIG_ESP_MATTER_MAX_ENDPOINT_COUNT=2` in the menuconfig.
24+
25+
For more optimizations please refer [esp-matter’s RAM Flash optimization guide](https://docs.espressif.com/projects/esp-matter/en/latest/esp32/optimizations.html)

examples/matter/matter_light/main/app_main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,11 @@ extern "C" void app_main()
144144

145145
/* Pre start */
146146
ESP_ERROR_CHECK(app_matter_rmaker_start());
147-
/* Start the ESP RainMaker Agent */
148-
esp_rmaker_start();
147+
149148
rmaker_init_done = true;
150149

151150
app_matter_enable_matter_console();
151+
152+
// RainMaker start is deferred after Matter commissioning is complete
153+
// and BLE memory is reclaimed, so that MQTT connect doesnt fail.
152154
}

examples/matter/matter_light/main/app_matter_light.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ void app_event_cb(const ChipDeviceEvent *event, intptr_t arg)
212212
case chip::DeviceLayer::DeviceEventType::kFabricCommitted:
213213
ESP_LOGI(TAG, "Fabric is committed");
214214
break;
215+
216+
case chip::DeviceLayer::DeviceEventType::kBLEDeinitialized:
217+
ESP_LOGI(TAG, "BLE deinitialized and memory reclaimed");
218+
// Starting RainMaker after Matter commissioning is complete
219+
// and BLE memory is reclaimed, so that MQTT connect doesn't fail.
220+
esp_rmaker_start();
221+
break;
222+
215223
default:
216224
break;
217225
}

examples/matter/matter_light/sdkconfig.defaults

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,7 @@ CONFIG_ESP_INSIGHTS_TRANSPORT_MQTT=y
8181
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
8282
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
8383
CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT=y
84+
85+
# DRAM optimization
86+
CONFIG_ENABLE_CHIP_SHELL=n
87+
CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT=2

examples/matter/matter_switch/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@
1010
- To test remote control, change the network connection of the mobile.
1111

1212
> Please refer to the [README in the parent folder](../README.md) for instructions.
13+
14+
#### Optimization
15+
16+
TO optimize the DRAM usage, this example uses the following optimizations:
17+
18+
- Disables the chip shell, `CONFIG_ENABLE_CHIP_SHELL=n`, it adds approx 10KB or RAM.
19+
- As this example has two endpoints (Endpoint 0: Root endpoint, Endpoint 1: Extended Color Light), the default dynamic endpoint count is set to 2. This is done by setting `CONFIG_ESP_MATTER_MAX_ENDPOINT_COUNT=2` in the menuconfig.
20+
21+
For more optimizations please refer [esp-matter’s RAM Flash optimization guide](https://docs.espressif.com/projects/esp-matter/en/latest/esp32/optimizations.html)

examples/matter/matter_switch/main/app_main.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ static const char *TAG = "app_main";
2626

2727
static app_driver_handle_t switch_handle;
2828

29-
bool rmaker_init_done = false; // used with extern in `app_matter.c`
30-
3129
/* Callback to handle commands received from the RainMaker cloud */
3230
static esp_err_t write_cb(const esp_rmaker_device_t *device, const esp_rmaker_param_t *param,
3331
const esp_rmaker_param_val_t val, void *priv_data, esp_rmaker_write_ctx_t *ctx)
@@ -86,7 +84,6 @@ extern "C" void app_main()
8684
esp_rmaker_device_t *switch_device = esp_rmaker_switch_device_create(SWITCH_DEVICE_NAME, NULL, DEFAULT_POWER);
8785
esp_rmaker_device_add_cb(switch_device, write_cb, NULL);
8886

89-
9087
esp_rmaker_node_add_device(node, switch_device);
9188

9289
/* Enable OTA */
@@ -114,10 +111,9 @@ extern "C" void app_main()
114111
/* Pre start */
115112
ESP_ERROR_CHECK(app_matter_rmaker_start());
116113

117-
/* Start the ESP RainMaker Agent */
118-
esp_rmaker_start();
119-
rmaker_init_done = true;
120-
121114
/* Enable Matter diagnostics console*/
122115
app_matter_enable_matter_console();
116+
117+
// RainMaker start is deferred after Matter commissioning is complete
118+
// and BLE memory is reclaimed, so that MQTT connect doesnt fail.
123119
}

examples/matter/matter_switch/main/app_matter_switch.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ void app_event_cb(const ChipDeviceEvent *event, intptr_t arg)
7070
ESP_LOGI(TAG, "Commissioning complete");
7171
break;
7272

73+
case chip::DeviceLayer::DeviceEventType::PublicEventTypes::kBLEDeinitialized:
74+
ESP_LOGI(TAG, "BLE deinitialized and memory reclaimed");
75+
// Starting RainMaker after Matter commissioning is complete
76+
// and BLE memory is reclaimed, so that MQTT connect doesn't fail.
77+
esp_rmaker_start();
78+
break;
79+
7380
default:
7481
break;
7582
}

examples/matter/matter_switch/main/app_priv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ esp_err_t app_attribute_update_cb(esp_matter::attribute::callback_type_t type, u
5454
esp_err_t app_identification_cb(esp_matter::identification::callback_type_t type, uint16_t endpoint_id, uint8_t effect_id,
5555
uint8_t effect_variant, void *priv_data);
5656

57-
void app_event_cb(const ChipDeviceEvent *event, intptr_t arg);
57+
void app_event_cb(const ChipDeviceEvent *event, intptr_t arg);

examples/matter/matter_switch/sdkconfig.defaults

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,7 @@ CONFIG_ESP_INSIGHTS_TRANSPORT_MQTT=y
8181
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
8282
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
8383
CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT=y
84+
85+
# DRAM optimization
86+
CONFIG_ENABLE_CHIP_SHELL=n
87+
CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT=2

0 commit comments

Comments
 (0)