@@ -60,15 +60,12 @@ typedef enum {
6060 SWITCH_RELEASE_DETECTED,
6161} switch_state_t ;
6262
63- static switch_func_pair_t button_func_pair[] = {
64- {GPIO_INPUT_IO_TOGGLE_SWITCH, SWITCH_ONOFF_TOGGLE_CONTROL}
65- };
63+ static switch_func_pair_t button_func_pair[] = {{GPIO_INPUT_IO_TOGGLE_SWITCH, SWITCH_ONOFF_TOGGLE_CONTROL}};
6664
6765/* Default End Device config */
6866#define ESP_ZB_ZED_CONFIG () \
6967 { \
70- .esp_zb_role = ESP_ZB_DEVICE_TYPE_ED, \
71- .install_code_policy = INSTALLCODE_POLICY_ENABLE, \
68+ .esp_zb_role = ESP_ZB_DEVICE_TYPE_ED, .install_code_policy = INSTALLCODE_POLICY_ENABLE, \
7269 .nwk_cfg = { \
7370 .zed_cfg = \
7471 { \
@@ -85,58 +82,58 @@ static switch_func_pair_t button_func_pair[] = {
8582 { .host_connection_mode = ZB_HOST_CONNECTION_MODE_NONE, }
8683
8784/* Zigbee configuration */
88- #define INSTALLCODE_POLICY_ENABLE false /* enable the install code policy for security */
85+ #define INSTALLCODE_POLICY_ENABLE false /* enable the install code policy for security */
8986#define ED_AGING_TIMEOUT ESP_ZB_ED_AGING_TIMEOUT_64MIN
9087#define ED_KEEP_ALIVE 3000 /* 3000 millisecond */
9188#define HA_ESP_SENSOR_ENDPOINT 10 /* esp temperature sensor device endpoint, used for temperature measurement */
9289#define ESP_ZB_PRIMARY_CHANNEL_MASK ESP_ZB_TRANSCEIVER_ALL_CHANNELS_MASK /* Zigbee primary channel mask use in the example */
9390
9491/* Temperature sensor configuration */
95- #define ESP_TEMP_SENSOR_UPDATE_INTERVAL (1 ) /* Local sensor update interval (second) */
96- #define ESP_TEMP_SENSOR_MIN_VALUE (10 ) /* Local sensor min measured value (degree Celsius) */
97- #define ESP_TEMP_SENSOR_MAX_VALUE (50 ) /* Local sensor max measured value (degree Celsius) */
92+ #define ESP_TEMP_SENSOR_UPDATE_INTERVAL (1 ) /* Local sensor update interval (second) */
93+ #define ESP_TEMP_SENSOR_MIN_VALUE (10 ) /* Local sensor min measured value (degree Celsius) */
94+ #define ESP_TEMP_SENSOR_MAX_VALUE (50 ) /* Local sensor max measured value (degree Celsius) */
9895
9996/* Attribute values in ZCL string format
10097 * The string should be started with the length of its own.
10198 */
102- #define MANUFACTURER_NAME " \x0B " " ESPRESSIF"
103- #define MODEL_IDENTIFIER " \x09 " CONFIG_IDF_TARGET
99+ #define MANUFACTURER_NAME \
100+ " \x0B " \
101+ " ESPRESSIF"
102+ #define MODEL_IDENTIFIER " \x09 " CONFIG_IDF_TARGET
104103
105104/* ******************** Zigbee functions **************************/
106- static int16_t zb_temperature_to_s16 (float temp)
107- {
108- return (int16_t )(temp * 100 );
105+ static int16_t zb_temperature_to_s16 (float temp) {
106+ return (int16_t )(temp * 100 );
109107}
110108
111- static void esp_zb_buttons_handler (switch_func_pair_t *button_func_pair)
112- {
113- if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) {
114- /* Send report attributes command */
115- esp_zb_zcl_report_attr_cmd_t report_attr_cmd;
116- report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT;
117- report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID;
118- report_attr_cmd.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE;
119- report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT;
120- report_attr_cmd.zcl_basic_cmd .src_endpoint = HA_ESP_SENSOR_ENDPOINT;
121-
122- esp_zb_lock_acquire (portMAX_DELAY);
123- esp_zb_zcl_report_attr_cmd_req (&report_attr_cmd);
124- esp_zb_lock_release ();
125- log_i (" Send 'report attributes' command" );
126- }
127- }
109+ static void esp_zb_buttons_handler (switch_func_pair_t *button_func_pair) {
110+ if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) {
111+ /* Send report attributes command */
112+ esp_zb_zcl_report_attr_cmd_t report_attr_cmd;
113+ report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT;
114+ report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID;
115+ report_attr_cmd.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE;
116+ report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT;
117+ report_attr_cmd.zcl_basic_cmd .src_endpoint = HA_ESP_SENSOR_ENDPOINT;
128118
129- static void esp_app_temp_sensor_handler (float temperature)
130- {
131- int16_t measured_value = zb_temperature_to_s16 (temperature);
132- Serial.println (" Updating temperature sensor value..." );
133- Serial.println (measured_value);
134- /* Update temperature sensor measured value */
135119 esp_zb_lock_acquire (portMAX_DELAY);
136- esp_zb_zcl_set_attribute_val (HA_ESP_SENSOR_ENDPOINT,
137- ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
138- ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID, &measured_value, false );
120+ esp_zb_zcl_report_attr_cmd_req (&report_attr_cmd);
139121 esp_zb_lock_release ();
122+ log_i (" Send 'report attributes' command" );
123+ }
124+ }
125+
126+ static void esp_app_temp_sensor_handler (float temperature) {
127+ int16_t measured_value = zb_temperature_to_s16 (temperature);
128+ Serial.println (" Updating temperature sensor value..." );
129+ Serial.println (measured_value);
130+ /* Update temperature sensor measured value */
131+ esp_zb_lock_acquire (portMAX_DELAY);
132+ esp_zb_zcl_set_attribute_val (
133+ HA_ESP_SENSOR_ENDPOINT, ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID, &measured_value,
134+ false
135+ );
136+ esp_zb_lock_release ();
140137}
141138
142139static void bdb_start_top_level_commissioning_cb (uint8_t mode_mask) {
@@ -160,10 +157,10 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
160157 // Start Temperature sensor reading task
161158 xTaskCreate (temp_sensor_value_update, " temp_sensor_update" , 2048 , NULL , 10 , NULL );
162159 if (esp_zb_bdb_is_factory_new ()) {
163- log_i (" Start network steering" );
164- esp_zb_bdb_start_top_level_commissioning (ESP_ZB_BDB_MODE_NETWORK_STEERING);
160+ log_i (" Start network steering" );
161+ esp_zb_bdb_start_top_level_commissioning (ESP_ZB_BDB_MODE_NETWORK_STEERING);
165162 } else {
166- log_i (" Device rebooted" );
163+ log_i (" Device rebooted" );
167164 }
168165 } else {
169166 /* commissioning failed */
@@ -188,30 +185,31 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
188185 }
189186}
190187
191- static esp_zb_cluster_list_t *custom_temperature_sensor_clusters_create (esp_zb_temperature_sensor_cfg_t *temperature_sensor)
192- {
193- esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create ();
194- esp_zb_attribute_list_t *basic_cluster = esp_zb_basic_cluster_create (&(temperature_sensor->basic_cfg ));
195- ESP_ERROR_CHECK (esp_zb_basic_cluster_add_attr (basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, (void *)MANUFACTURER_NAME));
196- ESP_ERROR_CHECK (esp_zb_basic_cluster_add_attr (basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, (void *)MODEL_IDENTIFIER));
197- ESP_ERROR_CHECK (esp_zb_cluster_list_add_basic_cluster (cluster_list, basic_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
198- ESP_ERROR_CHECK (esp_zb_cluster_list_add_identify_cluster (cluster_list, esp_zb_identify_cluster_create (&(temperature_sensor->identify_cfg )), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
199- ESP_ERROR_CHECK (esp_zb_cluster_list_add_identify_cluster (cluster_list, esp_zb_zcl_attr_list_create (ESP_ZB_ZCL_CLUSTER_ID_IDENTIFY), ESP_ZB_ZCL_CLUSTER_CLIENT_ROLE));
200- ESP_ERROR_CHECK (esp_zb_cluster_list_add_temperature_meas_cluster (cluster_list, esp_zb_temperature_meas_cluster_create (&(temperature_sensor->temp_meas_cfg )), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
201- return cluster_list;
188+ static esp_zb_cluster_list_t *custom_temperature_sensor_clusters_create (esp_zb_temperature_sensor_cfg_t *temperature_sensor) {
189+ esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create ();
190+ esp_zb_attribute_list_t *basic_cluster = esp_zb_basic_cluster_create (&(temperature_sensor->basic_cfg ));
191+ ESP_ERROR_CHECK (esp_zb_basic_cluster_add_attr (basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, (void *)MANUFACTURER_NAME));
192+ ESP_ERROR_CHECK (esp_zb_basic_cluster_add_attr (basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, (void *)MODEL_IDENTIFIER));
193+ ESP_ERROR_CHECK (esp_zb_cluster_list_add_basic_cluster (cluster_list, basic_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
194+ ESP_ERROR_CHECK (
195+ esp_zb_cluster_list_add_identify_cluster (cluster_list, esp_zb_identify_cluster_create (&(temperature_sensor->identify_cfg )), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE)
196+ );
197+ ESP_ERROR_CHECK (
198+ esp_zb_cluster_list_add_identify_cluster (cluster_list, esp_zb_zcl_attr_list_create (ESP_ZB_ZCL_CLUSTER_ID_IDENTIFY), ESP_ZB_ZCL_CLUSTER_CLIENT_ROLE)
199+ );
200+ ESP_ERROR_CHECK (esp_zb_cluster_list_add_temperature_meas_cluster (
201+ cluster_list, esp_zb_temperature_meas_cluster_create (&(temperature_sensor->temp_meas_cfg )), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE
202+ ));
203+ return cluster_list;
202204}
203205
204- static esp_zb_ep_list_t *custom_temperature_sensor_ep_create (uint8_t endpoint_id, esp_zb_temperature_sensor_cfg_t *temperature_sensor)
205- {
206- esp_zb_ep_list_t *ep_list = esp_zb_ep_list_create ();
207- esp_zb_endpoint_config_t endpoint_config = {
208- .endpoint = endpoint_id,
209- .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID,
210- .app_device_id = ESP_ZB_HA_TEMPERATURE_SENSOR_DEVICE_ID,
211- .app_device_version = 0
212- };
213- esp_zb_ep_list_add_ep (ep_list, custom_temperature_sensor_clusters_create (temperature_sensor), endpoint_config);
214- return ep_list;
206+ static esp_zb_ep_list_t *custom_temperature_sensor_ep_create (uint8_t endpoint_id, esp_zb_temperature_sensor_cfg_t *temperature_sensor) {
207+ esp_zb_ep_list_t *ep_list = esp_zb_ep_list_create ();
208+ esp_zb_endpoint_config_t endpoint_config = {
209+ .endpoint = endpoint_id, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_TEMPERATURE_SENSOR_DEVICE_ID, .app_device_version = 0
210+ };
211+ esp_zb_ep_list_add_ep (ep_list, custom_temperature_sensor_clusters_create (temperature_sensor), endpoint_config);
212+ return ep_list;
215213}
216214
217215static void esp_zb_task (void *pvParameters) {
@@ -228,26 +226,30 @@ static void esp_zb_task(void *pvParameters) {
228226
229227 /* Config the reporting info */
230228 esp_zb_zcl_reporting_info_t reporting_info = {
231- .direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV,
232- .ep = HA_ESP_SENSOR_ENDPOINT,
233- .cluster_id = ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT,
234- .cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
235- .attr_id = ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID,
236- .u = {
237- .send_info = {
229+ .direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV,
230+ .ep = HA_ESP_SENSOR_ENDPOINT,
231+ .cluster_id = ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT,
232+ .cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
233+ .attr_id = ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID,
234+ .u =
235+ {
236+ .send_info =
237+ {
238238 .min_interval = 1 ,
239239 .max_interval = 0 ,
240- .delta = {
241- .u16 = 100 ,
242- },
240+ .delta =
241+ {
242+ .u16 = 100 ,
243+ },
243244 .def_min_interval = 1 ,
244245 .def_max_interval = 0 ,
245- },
246+ },
246247 },
247- .dst = {
248+ .dst =
249+ {
248250 .profile_id = ESP_ZB_AF_HA_PROFILE_ID,
249251 },
250- .manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC,
252+ .manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC,
251253 };
252254 esp_zb_zcl_update_reporting_info (&reporting_info);
253255 esp_zb_set_primary_network_channel_set (ESP_ZB_PRIMARY_CHANNEL_MASK);
@@ -277,13 +279,12 @@ static void switch_gpios_intr_enabled(bool enabled) {
277279}
278280
279281/* *********************** Temp sensor *****************************/
280- static void temp_sensor_value_update (void *arg)
281- {
282- for (;;) {
283- float tsens_value = temperatureRead ();
284- esp_app_temp_sensor_handler (tsens_value);
285- delay (1000 );
286- }
282+ static void temp_sensor_value_update (void *arg) {
283+ for (;;) {
284+ float tsens_value = temperatureRead ();
285+ esp_app_temp_sensor_handler (tsens_value);
286+ delay (1000 );
287+ }
287288}
288289
289290/* ******************** Arduino functions **************************/
0 commit comments