Skip to content

Commit a4acfdf

Browse files
P-R-O-C-H-Yvortigont
authored andcommitted
feat(zigbee): Add vibration sensor endpoint (#10944)
1 parent dfa1db0 commit a4acfdf

File tree

7 files changed

+320
-0
lines changed

7 files changed

+320
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
293293
libraries/Zigbee/src/ep/ZigbeeContactSwitch.cpp
294294
libraries/Zigbee/src/ep/ZigbeeDoorWindowHandle.cpp
295295
libraries/Zigbee/src/ep/ZigbeeWindowCovering.cpp
296+
libraries/Zigbee/src/ep/ZigbeeVibrationSensor.cpp
296297
)
297298

298299
set(ARDUINO_LIBRARY_BLE_SRCS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Arduino-ESP32 Zigbee Vibration Sensor Example
2+
3+
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) vibration sensor (IAS Zone),
4+
that can be used for example as a security device which is sensing a vibrations.
5+
6+
# Supported Targets
7+
8+
Currently, this example supports the following targets.
9+
10+
| Supported Targets | ESP32-C6 | ESP32-H2 |
11+
| ----------------- | -------- | -------- |
12+
13+
## Hardware Required
14+
15+
* A USB cable for power supply and programming
16+
17+
### Configure the Project
18+
19+
Set the Button GPIO by changing the `button` variable. By default, it's the pin `BOOT_PIN` (BOOT button on ESP32-C6 and ESP32-H2).
20+
Set the Sensor GPIO by changing the `sensor_pin` variable.
21+
22+
#### Using Arduino IDE
23+
24+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
25+
26+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
27+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
28+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
29+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
30+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
31+
32+
## Troubleshooting
33+
34+
If the End device flashed with this example is not connecting to the coordinator, erase the flash of the End device before flashing the example to the board.
35+
36+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
37+
38+
* **LED not blinking:** Check the wiring connection and the IO selection.
39+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
40+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
41+
42+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
43+
44+
## Contribute
45+
46+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
47+
48+
If you have any **feedback** or **issue** to report on this example/library, please open an issue or fix it by creating a new PR. Contributions are more than welcome!
49+
50+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
51+
52+
## Resources
53+
54+
* Official ESP32 Forum: [Link](https://esp32.com)
55+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
56+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
57+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
58+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @brief This example demonstrates Zigbee vibration sensor (IAS Zone).
17+
*
18+
* The example demonstrates how to use Zigbee library to create a end device vibration sensor.
19+
* The vibration sensor is a Zigbee end device, which is reporting data to the Zigbee network.
20+
*
21+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
22+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
23+
*
24+
* Please check the README.md for instructions and more detailed description.
25+
*
26+
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
27+
*/
28+
29+
#ifndef ZIGBEE_MODE_ED
30+
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
31+
#endif
32+
33+
#include "Zigbee.h"
34+
35+
/* Zigbee vibration sensor configuration */
36+
#define VIBRATION_SENSOR_ENDPOINT_NUMBER 10
37+
uint8_t button = BOOT_PIN;
38+
uint8_t sensor_pin = 4;
39+
40+
ZigbeeVibrationSensor zbVibrationSensor = ZigbeeVibrationSensor(VIBRATION_SENSOR_ENDPOINT_NUMBER);
41+
42+
void setup() {
43+
Serial.begin(115200);
44+
45+
// Init button + sensor
46+
pinMode(button, INPUT_PULLUP);
47+
pinMode(sensor_pin, INPUT);
48+
49+
// Optional: set Zigbee device name and model
50+
zbVibrationSensor.setManufacturerAndModel("Espressif", "ZigbeeVibrationSensor");
51+
52+
// Add endpoint to Zigbee Core
53+
Zigbee.addEndpoint(&zbVibrationSensor);
54+
55+
Serial.println("Starting Zigbee...");
56+
// When all EPs are registered, start Zigbee in End Device mode
57+
if (!Zigbee.begin()) {
58+
Serial.println("Zigbee failed to start!");
59+
Serial.println("Rebooting...");
60+
ESP.restart();
61+
} else {
62+
Serial.println("Zigbee started successfully!");
63+
}
64+
Serial.println("Connecting to network");
65+
while (!Zigbee.connected()) {
66+
Serial.print(".");
67+
delay(100);
68+
}
69+
Serial.println();
70+
}
71+
72+
void loop() {
73+
// Checking pin for contact change
74+
static bool sensed = false;
75+
if (digitalRead(sensor_pin) == HIGH && !sensed) {
76+
// Update contact sensor value
77+
zbVibrationSensor.setVibration(true);
78+
sensed = true;
79+
//if sensed, wait 2 seconds before next sensing
80+
delay(2000);
81+
} else if (digitalRead(sensor_pin) == LOW && sensed) {
82+
zbVibrationSensor.setVibration(false);
83+
sensed = false;
84+
//if not sensed, wait 0,5 seconds before next sensing
85+
delay(500);
86+
}
87+
88+
// Checking button for factory reset
89+
if (digitalRead(button) == LOW) { // Push button pressed
90+
// Key debounce handling
91+
delay(100);
92+
int startTime = millis();
93+
while (digitalRead(button) == LOW) {
94+
delay(50);
95+
if ((millis() - startTime) > 3000) {
96+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
97+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
98+
delay(1000);
99+
Zigbee.factoryReset();
100+
}
101+
}
102+
}
103+
delay(100);
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
3+
"requires": [
4+
"CONFIG_SOC_IEEE802154_SUPPORTED=y"
5+
]
6+
}

libraries/Zigbee/src/Zigbee.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
#include "ep/ZigbeeContactSwitch.h"
2222
#include "ep/ZigbeeDoorWindowHandle.h"
2323
#include "ep/ZigbeeWindowCovering.h"
24+
#include "ep/ZigbeeVibrationSensor.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include "ZigbeeVibrationSensor.h"
2+
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
3+
4+
esp_zb_cluster_list_t *zigbee_vibration_sensor_clusters_create(zigbee_vibration_sensor_cfg_t *vibration_sensor) {
5+
esp_zb_basic_cluster_cfg_t *basic_cfg = vibration_sensor ? &(vibration_sensor->basic_cfg) : NULL;
6+
esp_zb_identify_cluster_cfg_t *identify_cfg = vibration_sensor ? &(vibration_sensor->identify_cfg) : NULL;
7+
esp_zb_ias_zone_cluster_cfg_t *ias_zone_cfg = vibration_sensor ? &(vibration_sensor->ias_zone_cfg) : NULL;
8+
esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create();
9+
esp_zb_cluster_list_add_basic_cluster(cluster_list, esp_zb_basic_cluster_create(basic_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
10+
esp_zb_cluster_list_add_identify_cluster(cluster_list, esp_zb_identify_cluster_create(identify_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
11+
esp_zb_cluster_list_add_ias_zone_cluster(cluster_list, esp_zb_ias_zone_cluster_create(ias_zone_cfg), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
12+
return cluster_list;
13+
}
14+
15+
ZigbeeVibrationSensor::ZigbeeVibrationSensor(uint8_t endpoint) : ZigbeeEP(endpoint) {
16+
_device_id = ESP_ZB_HA_IAS_ZONE_ID;
17+
_zone_status = 0;
18+
_zone_id = 0xff;
19+
_ias_cie_endpoint = 1;
20+
21+
//Create custom vibration sensor configuration
22+
zigbee_vibration_sensor_cfg_t vibration_sensor_cfg = ZIGBEE_DEFAULT_VIBRATION_SENSOR_CONFIG();
23+
_cluster_list = zigbee_vibration_sensor_clusters_create(&vibration_sensor_cfg);
24+
25+
_ep_config = {.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_IAS_ZONE_ID, .app_device_version = 0};
26+
}
27+
28+
void ZigbeeVibrationSensor::setIASClientEndpoint(uint8_t ep_number) {
29+
_ias_cie_endpoint = ep_number;
30+
}
31+
32+
void ZigbeeVibrationSensor::setVibration(bool sensed) {
33+
log_v("Setting Vibration sensor to %s", sensed ? "sensed" : "not sensed");
34+
uint8_t vibration = (uint8_t)sensed;
35+
esp_zb_lock_acquire(portMAX_DELAY);
36+
esp_zb_zcl_set_attribute_val(
37+
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_IAS_ZONE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_IAS_ZONE_ZONESTATUS_ID, &vibration, false
38+
);
39+
esp_zb_lock_release();
40+
_zone_status = vibration;
41+
report();
42+
}
43+
44+
void ZigbeeVibrationSensor::report() {
45+
/* Send IAS Zone status changed notification command */
46+
47+
esp_zb_zcl_ias_zone_status_change_notif_cmd_t status_change_notif_cmd;
48+
status_change_notif_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_64_ENDP_PRESENT;
49+
status_change_notif_cmd.zcl_basic_cmd.src_endpoint = _endpoint;
50+
status_change_notif_cmd.zcl_basic_cmd.dst_endpoint = _ias_cie_endpoint; //default is 1
51+
memcpy(status_change_notif_cmd.zcl_basic_cmd.dst_addr_u.addr_long, _ias_cie_addr, sizeof(esp_zb_ieee_addr_t));
52+
53+
status_change_notif_cmd.zone_status = _zone_status;
54+
status_change_notif_cmd.extend_status = 0;
55+
status_change_notif_cmd.zone_id = _zone_id;
56+
status_change_notif_cmd.delay = 0;
57+
58+
esp_zb_lock_acquire(portMAX_DELAY);
59+
esp_zb_zcl_ias_zone_status_change_notif_cmd_req(&status_change_notif_cmd);
60+
esp_zb_lock_release();
61+
log_v("IAS Zone status changed notification sent");
62+
}
63+
64+
void ZigbeeVibrationSensor::zbIASZoneEnrollResponse(const esp_zb_zcl_ias_zone_enroll_response_message_t *message) {
65+
if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_IAS_ZONE) {
66+
log_v("IAS Zone Enroll Response: zone id(%d), status(%d)", message->zone_id, message->response_code);
67+
if (message->response_code == ESP_ZB_ZCL_IAS_ZONE_ENROLL_RESPONSE_CODE_SUCCESS) {
68+
log_v("IAS Zone Enroll Response: success");
69+
esp_zb_lock_acquire(portMAX_DELAY);
70+
memcpy(
71+
_ias_cie_addr,
72+
(*(esp_zb_ieee_addr_t *)
73+
esp_zb_zcl_get_attribute(_endpoint, ESP_ZB_ZCL_CLUSTER_ID_IAS_ZONE, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_IAS_ZONE_IAS_CIE_ADDRESS_ID)
74+
->data_p),
75+
sizeof(esp_zb_ieee_addr_t)
76+
);
77+
esp_zb_lock_release();
78+
_zone_id = message->zone_id;
79+
}
80+
81+
} else {
82+
log_w("Received message ignored. Cluster ID: %d not supported for On/Off Light", message->info.cluster);
83+
}
84+
}
85+
86+
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Class of Zigbee contact switch (IAS Zone) endpoint inherited from common EP class */
2+
3+
#pragma once
4+
5+
#include "soc/soc_caps.h"
6+
#include "sdkconfig.h"
7+
#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
8+
9+
#include "ZigbeeEP.h"
10+
#include "ha/esp_zigbee_ha_standard.h"
11+
12+
// clang-format off
13+
#define ZIGBEE_DEFAULT_VIBRATION_SENSOR_CONFIG() \
14+
{ \
15+
.basic_cfg = \
16+
{ \
17+
.zcl_version = ESP_ZB_ZCL_BASIC_ZCL_VERSION_DEFAULT_VALUE, \
18+
.power_source = ESP_ZB_ZCL_BASIC_POWER_SOURCE_DEFAULT_VALUE, \
19+
}, \
20+
.identify_cfg = \
21+
{ \
22+
.identify_time = ESP_ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE, \
23+
}, \
24+
.ias_zone_cfg = \
25+
{ \
26+
.zone_state = ESP_ZB_ZCL_IAS_ZONE_ZONESTATE_NOT_ENROLLED, \
27+
.zone_type = ESP_ZB_ZCL_IAS_ZONE_ZONETYPE_VIBRATION_MOVEMENT, \
28+
.zone_status = 0, \
29+
.ias_cie_addr = ESP_ZB_ZCL_ZONE_IAS_CIE_ADDR_DEFAULT, \
30+
.zone_id = 0xff, \
31+
.zone_ctx = {0, 0, 0, 0}, \
32+
}, \
33+
}
34+
// clang-format on
35+
36+
typedef struct zigbee_vibration_sensor_cfg_s {
37+
esp_zb_basic_cluster_cfg_t basic_cfg;
38+
esp_zb_identify_cluster_cfg_t identify_cfg;
39+
esp_zb_ias_zone_cluster_cfg_t ias_zone_cfg;
40+
} zigbee_vibration_sensor_cfg_t;
41+
42+
class ZigbeeVibrationSensor : public ZigbeeEP {
43+
public:
44+
ZigbeeVibrationSensor(uint8_t endpoint);
45+
~ZigbeeVibrationSensor() {}
46+
47+
// Set the IAS Client endpoint number (default is 1)
48+
void setIASClientEndpoint(uint8_t ep_number);
49+
50+
// Set the vibration sensor value (true = sensed, false = not sensed)
51+
void setVibration(bool sensed);
52+
53+
// Report the vibration sensor value, done automatically after setting the sensed value
54+
void report();
55+
56+
private:
57+
void zbIASZoneEnrollResponse(const esp_zb_zcl_ias_zone_enroll_response_message_t *message) override;
58+
uint8_t _zone_status;
59+
uint8_t _zone_id;
60+
esp_zb_ieee_addr_t _ias_cie_addr;
61+
uint8_t _ias_cie_endpoint;
62+
};
63+
64+
#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED

0 commit comments

Comments
 (0)