Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Zigbee): Adding Zigbee Wind speed sensor example #10455

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
libraries/Zigbee/src/ep/ZigbeeSwitch.cpp
libraries/Zigbee/src/ep/ZigbeeTempSensor.cpp
libraries/Zigbee/src/ep/ZigbeeThermostat.cpp
libraries/Zigbee/src/ep/ZigbeeWindSpeedSensor.cpp
)

set(ARDUINO_LIBRARY_BLE_SRCS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @brief This example demonstrates Zigbee windspeed sensor.
*
* The example demonstrates how to use Zigbee library to create a end device wind speed sensor.
* The wind speed sensor is a Zigbee end device, which is controlled by a Zigbee coordinator.
*
* Proper Zigbee mode must be selected in Tools->Zigbee mode
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
*
* Please check the README.md for instructions and more detailed description.
*
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
*/

#ifndef ZIGBEE_MODE_ED
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode"
#endif

#include "ZigbeeCore.h"
#include "ep/ZigbeeWindSpeedSensor.h"

#define BUTTON_PIN 9 //Boot button for C6/H2
#define WIND_SPEED_SENSOR_ENDPOINT_NUMBER 10

ZigbeeWindSpeedSensor zbWindSpeedSensor = ZigbeeWindSpeedSensor(WIND_SPEED_SENSOR_ENDPOINT_NUMBER);

/************************ WindSpeed sensor *****************************/
static void windspeed_sensor_value_update(void *arg) {
for (;;) {
// Read wind speed sensor value
// float wssens_value = windspeedRead();
float wssens_value = 5.2;
log_v("Wind speed sensor value: %.2fm/s", wssens_value);
// Update windspeed value in Windspeed sensor EP
zbWindSpeedSensor.setWindSpeed(wssens_value);
delay(1000);
}
}

/********************* Arduino functions **************************/
void setup() {

Serial.begin(115200);
while (!Serial) {
delay(10);
}

// Init button switch
pinMode(BUTTON_PIN, INPUT);

// Optional: set Zigbee device name and model
zbWindSpeedSensor.setManufacturerAndModel("Espressif", "ZigbeeWindSpeedSensor");

// Set minimum and maximum windspeed measurement value (10-50°C is default range for chip windspeed measurement)
zbWindSpeedSensor.setMinMaxValue(10, 50);

// Set tolerance for windspeed measurement in °C (lowest possible value is 0.01°C)
zbWindSpeedSensor.setTolerance(1);

// Add endpoint to Zigbee Core
Zigbee.addEndpoint(&zbWindSpeedSensor);

// When all EPs are registered, start Zigbee in End Device mode
Zigbee.begin();

// Start Wind speed sensor reading task
xTaskCreate(windspeed_sensor_value_update, "wind_speed_sensor_update", 2048, NULL, 10, NULL);

// Set reporting interval for windspeed measurement in seconds, must be called after Zigbee.begin()
// min_interval and max_interval in seconds, delta (WindSpeed change in °C)
// if min = 1 and max = 0, reporting is sent only when windspeed changes by delta
// if min = 0 and max = 10, reporting is sent every 10 seconds or windspeed changes by delta
// if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of windspeed change
zbWindSpeedSensor.setReporting(1, 0, 1);
}

void loop() {
// Checking button for factory reset
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
// Key debounce handling
delay(100);
int startTime = millis();
while (digitalRead(BUTTON_PIN) == LOW) {
delay(50);
if ((millis() - startTime) > 3000) {
// If key pressed for more than 3secs, factory reset Zigbee and reboot
Serial.printf("Resetting Zigbee to factory settings, reboot.\n");
Zigbee.factoryReset();
}
}
zbWindSpeedSensor.reportWindSpeed();
}
delay(100);
}
6 changes: 6 additions & 0 deletions libraries/Zigbee/examples/Zigbee_Wind_Speed_Sensor/ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
"requires": [
"CONFIG_SOC_IEEE802154_SUPPORTED=y"
]
}
103 changes: 103 additions & 0 deletions libraries/Zigbee/src/ep/ZigbeeWindSpeedSensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "ZigbeeWindSpeedSensor.h"
#if SOC_IEEE802154_SUPPORTED

// There is no device_id for wind speed sensor, we use a generic one
ZigbeeWindSpeedSensor::ZigbeeWindSpeedSensor(uint8_t endpoint) : ZigbeeEP(endpoint) {
_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID;

esp_zb_windspeed_sensor_cfg_t windspeed_sensor_cfg = ESP_ZB_DEFAULT_TEMPERATURE_SENSOR_CONFIG();
_cluster_list = esp_zb_windspeed_sensor_clusters_create(&windspeed_sensor_cfg);

_ep_config = {
.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_SIMPLE_SENSOR_DEVICE_ID, .app_device_version = 0
};
}

static int16_t zb_windspeed_to_s16(float windspeed) {
return (int16_t)(windspeed * 100);
}

/** @brief Wind_Speed_Measurement cluster server attribute identifiers
typedef enum {
ESP_ZB_ZCL_ATTR_WIND_SPEED_MEASUREMENT_MEASURED_VALUE_ID = 0x0000, < MeasuredValue Attribute
ESP_ZB_ZCL_ATTR_WIND_SPEED_MEASUREMENT_MIN_MEASURED_VALUE_ID = 0x0001, < MinMeasuredValue Attribute
ESP_ZB_ZCL_ATTR_WIND_SPEED_MEASUREMENT_MAX_MEASURED_VALUE_ID = 0x0002, < MaxMeasuredValue Attribute
ESP_ZB_ZCL_ATTR_WIND_SPEED_MEASUREMENT_TOLERANCE_ID = 0x0003, < Tolerance Attribute
} esp_zb_zcl_wind_speed_measurement_srv_attr_t;
*/
void ZigbeeWindSpeedSensor::setMinMaxValue(float min, float max) {
int16_t zb_min = zb_windspeed_to_s16(min);
int16_t zb_max = zb_windspeed_to_s16(max);
esp_zb_attribute_list_t *windspeed_measure_cluster =
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_WIND_SPEED_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
//
esp_zb_cluster_update_attr(windspeed_measure_cluster, ESP_ZB_ZCL_ATTR_WIND_SPEED_MEASUREMENT_MIN_MEASURED_VALUE_ID, (void *)&zb_min);
esp_zb_cluster_update_attr(windspeed_measure_cluster, ESP_ZB_ZCL_ATTR_WIND_SPEED_MEASUREMENT_MAX_MEASURED_VALUE_ID, (void *)&zb_max);
}

void ZigbeeWindSpeedSensor::setTolerance(float tolerance) {
// Convert tolerance to ZCL uint16_t
uint16_t zb_tolerance = (uint16_t)(tolerance * 100);
esp_zb_attribute_list_t *windspeed_measure_cluster =
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_WIND_SPEED_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_zb_windspeed_meas_cluster_add_attr(windspeed_measure_cluster, ESP_ZB_ZCL_ATTR_WIND_SPEED_MEASUREMENT_TOLERANCE_ID, (void *)&zb_tolerance);
}

void ZigbeeWindSpeedSensor::setReporting(uint16_t min_interval, uint16_t max_interval, float delta) {
esp_zb_zcl_reporting_info_t reporting_info = {
.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_SRV,
.ep = _endpoint,
.cluster_id = ESP_ZB_ZCL_CLUSTER_ID_WIND_SPEED_MEASUREMENT,
.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
.attr_id = ESP_ZB_ZCL_ATTR_WIND_SPEED_MEASUREMENT_MEASURED_VALUE_ID,
.u =
{
.send_info =
{
.min_interval = min_interval,
.max_interval = max_interval,
.delta =
{
.u16 = (uint16_t)(delta * 100), // Convert delta to ZCL uint16_t
},
.def_min_interval = min_interval,
.def_max_interval = max_interval,
},
},
.dst =
{
.profile_id = ESP_ZB_AF_HA_PROFILE_ID,
},
.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC,
};
esp_zb_zcl_update_reporting_info(&reporting_info);
}

void ZigbeeWindSpeedSensor::setWindSpeed(float windspeed) {
int16_t zb_windspeed = zb_windspeed_to_s16(windspeed);
log_v("Updating windspeed sensor value...");
/* Update windspeed sensor measured value */
log_d("Setting windspeed to %d", zb_windspeed);
esp_zb_lock_acquire(portMAX_DELAY);
esp_zb_zcl_set_attribute_val(
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_WIND_SPEED_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_WIND_SPEED_MEASUREMENT_MEASURED_VALUE_ID, &zb_windspeed, false
);
esp_zb_lock_release();
}

void ZigbeeWindSpeedSensor::reportWindSpeed() {
/* Send report attributes command */
esp_zb_zcl_report_attr_cmd_t report_attr_cmd;
report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT;
report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_WIND_SPEED_MEASUREMENT_MEASURED_VALUE_ID;
report_attr_cmd.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE;
report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_WIND_SPEED_MEASUREMENT;
report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint;

esp_zb_lock_acquire(portMAX_DELAY);
esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd);
esp_zb_lock_release();
log_v("Temperature report sent");
}

#endif //SOC_IEEE802154_SUPPORTED
30 changes: 30 additions & 0 deletions libraries/Zigbee/src/ep/ZigbeeWindSpeedSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Class of Zigbee WindSpeed sensor endpoint inherited from common EP class */

#pragma once

#include "soc/soc_caps.h"
#if SOC_IEEE802154_SUPPORTED

#include "ZigbeeEP.h"
#include "ha/esp_zigbee_ha_standard.h"

class ZigbeeWindSpeedSensor : public ZigbeeEP {
public:
ZigbeeWindSpeedSensor(uint8_t endpoint);
~ZigbeeWindSpeedSensor();

// Set the WindSpeed value in 0,01°C
void setWindSpeed(float value);

// Set the min and max value for the WindSpeed sensor
void setMinMaxValue(float min, float max);

// Set the tolerance value for the WindSpeed sensor
void setTolerance(float tolerance);

// Set the reporting interval for WindSpeed measurement in seconds and delta
void setReporting(uint16_t min_interval, uint16_t max_interval, float delta);
void reportWindSpeed();
};

#endif //SOC_IEEE802154_SUPPORTED
Loading