Add illuminance cluster & reporting

This commit is contained in:
rbaron 2022-12-10 10:47:16 +01:00
parent 3c2094db2d
commit 8eb54106d5
4 changed files with 57 additions and 16 deletions

View file

@ -1,4 +1,7 @@
// This is a zigbee2mqtt zigbee2mqtt.io converter for this sample.
// See https://www.zigbee2mqtt.io/advanced/support-new-devices/01_support_new_devices.html
// on how to use it.
const fz = require('zigbee-herdsman-converters/converters/fromZigbee');
const tz = require('zigbee-herdsman-converters/converters/toZigbee');
const exposes = require('zigbee-herdsman-converters/lib/exposes');
@ -12,18 +15,32 @@ const definition = {
model: 'b-parasite',
vendor: 'b-parasite',
description: 'IoT development board - Zigbee sample',
fromZigbee: [fz.temperature, fz.humidity, fz.battery],
fromZigbee: [fz.temperature, fz.humidity, fz.battery, fz.soil_moisture, fz.illuminance],
toZigbee: [],
exposes: [e.temperature(), e.humidity(), e.battery()],
exposes: [
e.temperature(),
e.humidity(),
e.battery(),
e.soil_moisture(),
e.illuminance_lux()],
configure: async (device, coordinatorEndpoint, logger) => {
const endpoint = device.getEndpoint(10);
await reporting.bind(
endpoint,
coordinatorEndpoint,
['genPowerCfg', 'msTemperatureMeasurement', 'msRelativeHumidity']);
coordinatorEndpoint, [
'genPowerCfg',
'msTemperatureMeasurement',
'msRelativeHumidity',
'msSoilMoisture',
'msIlluminanceMeasurement',
]);
await reporting.batteryPercentageRemaining(endpoint);
// Not reportable :(
// await reporting.batteryVoltage(endpoint);
await reporting.temperature(endpoint);
await reporting.humidity(endpoint);
await reporting.soil_moisture(endpoint);
await reporting.illuminance(endpoint);
}
};

View file

@ -1,4 +1,5 @@
#include <dk_buttons_and_leds.h>
#include <math.h>
#include <prstlib/adc.h>
#include <prstlib/button.h>
#include <prstlib/led.h>
@ -81,6 +82,12 @@ PRST_ZB_ZCL_DECLARE_SOIL_MOISTURE_ATTRIB_LIST(
soil_moisture_attr_list,
&dev_ctx.soil_moisture_attrs.percentage);
ZB_ZCL_DECLARE_ILLUMINANCE_MEASUREMENT_ATTRIB_LIST(
illuminance_attr_list,
/*value=*/&dev_ctx.illuminance_attrs.log_lux,
/*min_value=*/NULL,
/*max_value*/ NULL);
PRST_ZB_DECLARE_CLUSTER_LIST(
app_template_clusters,
basic_attr_list,
@ -88,7 +95,8 @@ PRST_ZB_DECLARE_CLUSTER_LIST(
temp_measurement_attr_list,
rel_humi_attr_list,
basic_attr_list,
soil_moisture_attr_list);
soil_moisture_attr_list,
illuminance_attr_list);
PRST_ZB_DECLARE_ENDPOINT(
app_template_ep,
@ -144,6 +152,12 @@ void update_sensors_cb(zb_uint8_t arg) {
PRST_ZB_ZCL_ATTR_SOIL_MOISTURE_VALUE_ID,
&soil_moisture);
// Illuminance in 10000 * log_10(lux) + 1.
zb_int16_t log_lux = 10000 * log10((float)sensors.photo.brightness) + 1;
prst_zb_set_attr_value(ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT,
ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MEASURED_VALUE_ID,
&log_lux);
ZB_SCHEDULE_APP_ALARM(update_sensors_cb,
/*param=*/0,
ZB_TIME_ONE_SECOND * CONFIG_PRST_ZB_SLEEP_DURATION_SEC);
@ -154,9 +168,6 @@ int main(void) {
RET_IF_ERR(prst_led_init());
RET_IF_ERR(prst_button_init());
// We do this to quickly put the shtc3 to sleep.
prst_sensors_read_all(&sensors);
register_factory_reset_button(FACTORY_RESET_BUTTON);
prst_zb_attrs_init(&dev_ctx);
@ -165,8 +176,6 @@ int main(void) {
update_sensors_cb(/*arg=*/0);
RET_IF_ERR(prst_led_flash(2));
zb_zdo_pim_set_long_poll_interval(
ZB_TIME_ONE_SECOND * CONFIG_PRST_ZB_PARENT_POLL_INTERVAL_SEC);
power_down_unused_ram();
@ -174,5 +183,7 @@ int main(void) {
zigbee_enable();
zigbee_configure_sleepy_behavior(/*enable=*/true);
RET_IF_ERR(prst_led_flash(2));
return 0;
}

View file

@ -13,7 +13,7 @@ typedef struct {
zb_uint16_t max_val;
} prst_rel_humidity_attrs_t;
// Power configuration cluster - section 3.3.2.2.3
// Power configuration cluster - section 3.3.2.2.3.
typedef struct {
// Units of 100 mV. 0x00 - 0xff (optional, not reportable :()).
zb_uint8_t voltage;
@ -27,15 +27,20 @@ typedef struct {
zb_uint16_t percentage;
} prst_soil_moisture_attrs_t;
// Illuminance cluster - section 4.2.2.2.
typedef struct {
// 10000 * log_10(lux) + 1.
zb_uint16_t log_lux;
} prst_illuminancce_attrs_t;
struct zb_device_ctx {
zb_zcl_basic_attrs_ext_t basic_attr;
zb_zcl_identify_attrs_t identify_attr;
// In units of 0.01 C.
zb_zcl_temp_measurement_attrs_t temp_measure_attrs;
prst_rel_humidity_attrs_t rel_humidity_attrs;
// In units of 100 mV.
prst_batt_attrs_t batt_attrs;
prst_soil_moisture_attrs_t soil_moisture_attrs;
prst_illuminancce_attrs_t illuminance_attrs;
};
void prst_zb_attrs_init(struct zb_device_ctx *dev_ctx);

View file

@ -9,10 +9,10 @@
#define PRST_ZB_DEVICE_ID 0x0008
#define PRST_ZB_DEVICE_VERSION 0
#define PRST_ZB_IN_CLUSTER_NUM 6
#define PRST_ZB_IN_CLUSTER_NUM 7
#define PRST_ZB_OUT_CLUSTER_NUM 0
#define PRST_ZB_CLUSTER_NUM (PRST_ZB_IN_CLUSTER_NUM + PRST_ZB_OUT_CLUSTER_NUM)
#define PRST_ZB_ATTR_REPORTING_COUNT 4
#define PRST_ZB_ATTR_REPORTING_COUNT 5
#define PRST_ZB_DECLARE_CLUSTER_LIST( \
cluster_list_name, \
@ -21,7 +21,8 @@
temp_measurement_attr_list, \
rel_humidity_attr_list, \
batt_att_list, \
soil_moisture_attr_list) \
soil_moisture_attr_list, \
illuminance_attr_list) \
zb_zcl_cluster_desc_t cluster_list_name[] = \
{ \
ZB_ZCL_CLUSTER_DESC( \
@ -54,6 +55,12 @@
(soil_moisture_attr_list), \
ZB_ZCL_CLUSTER_SERVER_ROLE, \
ZB_ZCL_MANUF_CODE_INVALID), \
ZB_ZCL_CLUSTER_DESC( \
ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, \
ZB_ZCL_ARRAY_SIZE(illuminance_attr_list, zb_zcl_attr_t), \
(illuminance_attr_list), \
ZB_ZCL_CLUSTER_SERVER_ROLE, \
ZB_ZCL_MANUF_CODE_INVALID), \
ZB_ZCL_CLUSTER_DESC( \
ZB_ZCL_CLUSTER_ID_POWER_CONFIG, \
ZB_ZCL_ARRAY_SIZE(batt_attr_list, zb_zcl_attr_t), \
@ -79,6 +86,7 @@
ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT, \
ZB_ZCL_CLUSTER_ID_REL_HUMIDITY_MEASUREMENT, \
PRST_ZB_ZCL_ATTR_SOIL_MOISTURE_CLUSTER_ID, \
ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, \
ZB_ZCL_CLUSTER_ID_POWER_CONFIG, \
}}