|
| 1 | +/* |
| 2 | + This example code is in the Public Domain (or CC0 licensed, at your option.) |
| 3 | +
|
| 4 | + Unless required by applicable law or agreed to in writing, this |
| 5 | + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 6 | + CONDITIONS OF ANY KIND, either express or implied. |
| 7 | +*/ |
| 8 | + |
| 9 | +#include <app/server/CommissioningWindowManager.h> |
| 10 | +#include <app/server/Server.h> |
| 11 | +#include <bsp/esp-bsp.h> |
| 12 | +#include <esp_err.h> |
| 13 | +#include <esp_log.h> |
| 14 | +#include <esp_matter.h> |
| 15 | +#include <esp_matter_ota.h> |
| 16 | +#include <nvs_flash.h> |
| 17 | + |
| 18 | +#include <app_openthread_config.h> |
| 19 | +#include <app_reset.h> |
| 20 | +#include <common_macros.h> |
| 21 | + |
| 22 | +// drivers implemented by this example |
| 23 | +#include <drivers/shtc3.h> |
| 24 | +#include <drivers/pir.h> |
| 25 | + |
| 26 | +static const char *TAG = "app_main"; |
| 27 | + |
| 28 | +using namespace esp_matter; |
| 29 | +using namespace esp_matter::attribute; |
| 30 | +using namespace esp_matter::endpoint; |
| 31 | +using namespace chip::app::Clusters; |
| 32 | + |
| 33 | +// Application cluster specification, 7.18.2.11. Temperature |
| 34 | +// represents a temperature on the Celsius scale with a resolution of 0.01°C. |
| 35 | +// temp = (temperature in °C) x 100 |
| 36 | +static void temp_sensor_notification(uint16_t endpoint_id, float temp, void *user_data) |
| 37 | +{ |
| 38 | + // schedule the attribute update so that we can report it from matter thread |
| 39 | + chip::DeviceLayer::SystemLayer().ScheduleLambda([endpoint_id, temp]() { |
| 40 | + attribute_t * attribute = attribute::get(endpoint_id, |
| 41 | + TemperatureMeasurement::Id, |
| 42 | + TemperatureMeasurement::Attributes::MeasuredValue::Id); |
| 43 | + |
| 44 | + esp_matter_attr_val_t val = esp_matter_invalid(NULL); |
| 45 | + attribute::get_val(attribute, &val); |
| 46 | + val.val.i16 = static_cast<int16_t>(temp * 100); |
| 47 | + |
| 48 | + attribute::update(endpoint_id, TemperatureMeasurement::Id, TemperatureMeasurement::Attributes::MeasuredValue::Id, &val); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +// Application cluster specification, 2.6.4.1. MeasuredValue Attribute |
| 53 | +// represents the humidity in percent. |
| 54 | +// humidity = (humidity in %) x 100 |
| 55 | +static void humidity_sensor_notification(uint16_t endpoint_id, float humidity, void *user_data) |
| 56 | +{ |
| 57 | + // schedule the attribute update so that we can report it from matter thread |
| 58 | + chip::DeviceLayer::SystemLayer().ScheduleLambda([endpoint_id, humidity]() { |
| 59 | + attribute_t * attribute = attribute::get(endpoint_id, |
| 60 | + RelativeHumidityMeasurement::Id, |
| 61 | + RelativeHumidityMeasurement::Attributes::MeasuredValue::Id); |
| 62 | + |
| 63 | + esp_matter_attr_val_t val = esp_matter_invalid(NULL); |
| 64 | + attribute::get_val(attribute, &val); |
| 65 | + val.val.u16 = static_cast<uint16_t>(humidity * 100); |
| 66 | + |
| 67 | + attribute::update(endpoint_id, RelativeHumidityMeasurement::Id, RelativeHumidityMeasurement::Attributes::MeasuredValue::Id, &val); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +static void occupancy_sensor_notification(uint16_t endpoint_id, bool occupancy, void *user_data) |
| 72 | +{ |
| 73 | + // schedule the attribute update so that we can report it from matter thread |
| 74 | + chip::DeviceLayer::SystemLayer().ScheduleLambda([endpoint_id, occupancy]() { |
| 75 | + attribute_t * attribute = attribute::get(endpoint_id, |
| 76 | + OccupancySensing::Id, |
| 77 | + OccupancySensing::Attributes::Occupancy::Id); |
| 78 | + |
| 79 | + esp_matter_attr_val_t val = esp_matter_invalid(NULL); |
| 80 | + attribute::get_val(attribute, &val); |
| 81 | + val.val.b = occupancy; |
| 82 | + |
| 83 | + attribute::update(endpoint_id, OccupancySensing::Id, OccupancySensing::Attributes::Occupancy::Id, &val); |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +static esp_err_t factory_reset_button_register() |
| 88 | +{ |
| 89 | + button_handle_t push_button; |
| 90 | + esp_err_t err = bsp_iot_button_create(&push_button, NULL, BSP_BUTTON_NUM); |
| 91 | + VerifyOrReturnError(err == ESP_OK, err); |
| 92 | + return app_reset_button_register(push_button); |
| 93 | +} |
| 94 | + |
| 95 | +static void open_commissioning_window_if_necessary() |
| 96 | +{ |
| 97 | + VerifyOrReturn(chip::Server::GetInstance().GetFabricTable().FabricCount() == 0); |
| 98 | + |
| 99 | + chip::CommissioningWindowManager & commissionMgr = chip::Server::GetInstance().GetCommissioningWindowManager(); |
| 100 | + VerifyOrReturn(commissionMgr.IsCommissioningWindowOpen() == false); |
| 101 | + |
| 102 | + // After removing last fabric, this example does not remove the Wi-Fi credentials |
| 103 | + // and still has IP connectivity so, only advertising on DNS-SD. |
| 104 | + CHIP_ERROR err = commissionMgr.OpenBasicCommissioningWindow(chip::System::Clock::Seconds16(300), |
| 105 | + chip::CommissioningWindowAdvertisement::kDnssdOnly); |
| 106 | + if (err != CHIP_NO_ERROR) |
| 107 | + { |
| 108 | + ESP_LOGE(TAG, "Failed to open commissioning window, err:%" CHIP_ERROR_FORMAT, err.Format()); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) |
| 113 | +{ |
| 114 | + switch (event->Type) { |
| 115 | + case chip::DeviceLayer::DeviceEventType::kCommissioningComplete: |
| 116 | + ESP_LOGI(TAG, "Commissioning complete"); |
| 117 | + break; |
| 118 | + |
| 119 | + case chip::DeviceLayer::DeviceEventType::kFailSafeTimerExpired: |
| 120 | + ESP_LOGI(TAG, "Commissioning failed, fail safe timer expired"); |
| 121 | + break; |
| 122 | + |
| 123 | + case chip::DeviceLayer::DeviceEventType::kFabricRemoved: |
| 124 | + ESP_LOGI(TAG, "Fabric removed successfully"); |
| 125 | + open_commissioning_window_if_necessary(); |
| 126 | + break; |
| 127 | + |
| 128 | + case chip::DeviceLayer::DeviceEventType::kBLEDeinitialized: |
| 129 | + ESP_LOGI(TAG, "BLE deinitialized and memory reclaimed"); |
| 130 | + break; |
| 131 | + |
| 132 | + default: |
| 133 | + break; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// This callback is invoked when clients interact with the Identify Cluster. |
| 138 | +// In the callback implementation, an endpoint can identify itself. (e.g., by flashing an LED or light). |
| 139 | +static esp_err_t app_identification_cb(identification::callback_type_t type, uint16_t endpoint_id, uint8_t effect_id, |
| 140 | + uint8_t effect_variant, void *priv_data) |
| 141 | +{ |
| 142 | + ESP_LOGI(TAG, "Identification callback: type: %u, effect: %u, variant: %u", type, effect_id, effect_variant); |
| 143 | + return ESP_OK; |
| 144 | +} |
| 145 | + |
| 146 | +// This callback is called for every attribute update. The callback implementation shall |
| 147 | +// handle the desired attributes and return an appropriate error code. If the attribute |
| 148 | +// is not of your interest, please do not return an error code and strictly return ESP_OK. |
| 149 | +static esp_err_t app_attribute_update_cb(attribute::callback_type_t type, uint16_t endpoint_id, uint32_t cluster_id, |
| 150 | + uint32_t attribute_id, esp_matter_attr_val_t *val, void *priv_data) |
| 151 | +{ |
| 152 | + // Since this is just a sensor and we don't expect any writes on our temperature sensor, |
| 153 | + // so, return success. |
| 154 | + return ESP_OK; |
| 155 | +} |
| 156 | + |
| 157 | +extern "C" void app_main() |
| 158 | +{ |
| 159 | + /* Initialize the ESP NVS layer */ |
| 160 | + nvs_flash_init(); |
| 161 | + |
| 162 | + /* Initialize push button on the dev-kit to reset the device */ |
| 163 | + esp_err_t err = factory_reset_button_register(); |
| 164 | + ABORT_APP_ON_FAILURE(ESP_OK == err, ESP_LOGE(TAG, "Failed to initialize reset button, err:%d", err)); |
| 165 | + |
| 166 | + /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ |
| 167 | + node::config_t node_config; |
| 168 | + node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); |
| 169 | + ABORT_APP_ON_FAILURE(node != nullptr, ESP_LOGE(TAG, "Failed to create Matter node")); |
| 170 | + |
| 171 | + // add temperature sensor device |
| 172 | + temperature_sensor::config_t temp_sensor_config; |
| 173 | + endpoint_t * temp_sensor_ep = temperature_sensor::create(node, &temp_sensor_config, ENDPOINT_FLAG_NONE, NULL); |
| 174 | + ABORT_APP_ON_FAILURE(temp_sensor_ep != nullptr, ESP_LOGE(TAG, "Failed to create temperature_sensor endpoint")); |
| 175 | + |
| 176 | + // add the humidity sensor device |
| 177 | + humidity_sensor::config_t humidity_sensor_config; |
| 178 | + endpoint_t * humidity_sensor_ep = humidity_sensor::create(node, &humidity_sensor_config, ENDPOINT_FLAG_NONE, NULL); |
| 179 | + ABORT_APP_ON_FAILURE(humidity_sensor_ep != nullptr, ESP_LOGE(TAG, "Failed to create humidity_sensor endpoint")); |
| 180 | + |
| 181 | + // initialize temperature and humidity sensor driver (shtc3) |
| 182 | + static shtc3_sensor_config_t shtc3_config = { |
| 183 | + .temperature = { |
| 184 | + .cb = temp_sensor_notification, |
| 185 | + .endpoint_id = endpoint::get_id(temp_sensor_ep), |
| 186 | + }, |
| 187 | + .humidity = { |
| 188 | + .cb = humidity_sensor_notification, |
| 189 | + .endpoint_id = endpoint::get_id(humidity_sensor_ep), |
| 190 | + }, |
| 191 | + }; |
| 192 | + err = shtc3_sensor_init(&shtc3_config); |
| 193 | + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to initialize temperature sensor driver")); |
| 194 | + |
| 195 | + // add the occupancy sensor |
| 196 | + occupancy_sensor::config_t occupancy_sensor_config; |
| 197 | + occupancy_sensor_config.occupancy_sensing.occupancy_sensor_type = |
| 198 | + chip::to_underlying(OccupancySensing::OccupancySensorTypeEnum::kPir); |
| 199 | + occupancy_sensor_config.occupancy_sensing.occupancy_sensor_type_bitmap = |
| 200 | + chip::to_underlying(OccupancySensing::OccupancySensorTypeBitmap::kPir); |
| 201 | + |
| 202 | + endpoint_t * occupancy_sensor_ep = occupancy_sensor::create(node, &occupancy_sensor_config, ENDPOINT_FLAG_NONE, NULL); |
| 203 | + ABORT_APP_ON_FAILURE(occupancy_sensor_ep != nullptr, ESP_LOGE(TAG, "Failed to create occupancy_sensor endpoint")); |
| 204 | + |
| 205 | + // initialize occupancy sensor driver (pir) |
| 206 | + static pir_sensor_config_t pir_config = { |
| 207 | + .cb = occupancy_sensor_notification, |
| 208 | + .endpoint_id = endpoint::get_id(occupancy_sensor_ep), |
| 209 | + }; |
| 210 | + err = pir_sensor_init(&pir_config); |
| 211 | + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to initialize occupancy sensor driver")); |
| 212 | + |
| 213 | +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD |
| 214 | + /* Set OpenThread platform config */ |
| 215 | + esp_openthread_platform_config_t config = { |
| 216 | + .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), |
| 217 | + .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), |
| 218 | + .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), |
| 219 | + }; |
| 220 | + set_openthread_platform_config(&config); |
| 221 | +#endif |
| 222 | + |
| 223 | + /* Matter start */ |
| 224 | + err = esp_matter::start(app_event_cb); |
| 225 | + ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err)); |
| 226 | +} |
0 commit comments