Skip to content

Commit c17ddf6

Browse files
ci(pre-commit): Apply automatic fixes
1 parent 261cb06 commit c17ddf6

File tree

3 files changed

+134
-135
lines changed

3 files changed

+134
-135
lines changed

Diff for: libraries/Zigbee/examples/Zigbee_Illuminance_Sensor/Zigbee_Illuminance_Sensor.ino

+113-112
Original file line numberDiff line numberDiff line change
@@ -26,115 +26,116 @@
2626
* Created by MikaFromTheRoof (https://github.com/MikaFromTheRoof)
2727
*/
2828

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-
#define ZIGBEE_ILLUMINANCE_SENSOR_ENDPOINT 9
36-
uint8_t button = BOOT_PIN;
37-
uint8_t illuminance_sensor_pin = 6; // Insert the analog pin to which the sensor (e.g. photoresistor) is connected
38-
39-
ZigbeeIlluminanceSensor zbIlluminanceSensor = ZigbeeIlluminanceSensor(ZIGBEE_ILLUMINANCE_SENSOR_ENDPOINT);
40-
41-
/********************* Illuminance sensor **************************/
42-
static void illuminance_sensor_value_update(void *arg) {
43-
for (;;) {
44-
// read the raw analog value from the sensor
45-
int lsens_analog_raw = analogRead(illuminance_sensor_pin);
46-
Serial.printf("[Illuminance Sensor] raw analog value: %d\r\n", lsens_analog_raw);
47-
48-
// conversion into zigbee raw illuminance value (typically between 0 in darkness and 50000 in direct sunlight)
49-
// depends on the value range of the raw analog sensor values and will need calibration for correct lux values
50-
int lsens_illuminance_raw = map(lsens_analog_raw, 0, 4095, 0, 50000); // to demonstate map the 12-bit ADC value (0-4095) to Zigbee illuminance range (0-50000)
51-
Serial.printf("[Illuminance Sensor] raw illuminance value: %d\r\n", lsens_illuminance_raw);
52-
53-
// according to zigbee documentation the formular 10^(lsens_illuminance_raw/10000)-1 can be used to calculate lux value from raw illuminance value
54-
// Note: Zigbee2MQTT seems to be using the formular 10^(lsens_illuminance_raw/10000) instead (without -1)
55-
int lsens_illuminance_lux = round(pow(10,(lsens_illuminance_raw / 10000.0))-1);
56-
Serial.printf("[Illuminance Sensor] lux value: %d lux\r\n", lsens_illuminance_lux);
57-
58-
// Update illuminance in illuminance sensor EP
59-
zbIlluminanceSensor.setIlluminance(lsens_illuminance_raw); // use raw illuminance here!
60-
61-
delay(1000); // reduce delay (in ms), if you want your device to react more quickly to changes in illuminance
62-
}
63-
}
64-
65-
/********************* Arduino functions **************************/
66-
void setup() {
67-
Serial.begin(115200);
68-
69-
// Optional: configure analog input
70-
analogSetAttenuation(ADC_11db); // set analog to digital converter (ADC) attenuation to 11 dB (up to ~3.3V input)
71-
analogReadResolution(12); // set analog read resolution to 12 bits (value range from 0 to 4095), 12 is default
72-
73-
// Init button for factory reset
74-
pinMode(button, INPUT_PULLUP);
75-
76-
// Optional: Set Zigbee device name and model
77-
zbIlluminanceSensor.setManufacturerAndModel("Espressif", "ZigbeeIlluminanceSensor");
78-
79-
// Optional: Set power source (choose between ZB_POWER_SOURCE_MAINS and ZB_POWER_SOURCE_BATTERY), defaults to unknown
80-
zbIlluminanceSensor.setPowerSource(ZB_POWER_SOURCE_MAINS);
81-
82-
// Set minimum and maximum for raw illuminance value (0 min and 50000 max equals to 0 lux - 100,000 lux)
83-
zbIlluminanceSensor.setMinMaxValue(0, 50000);
84-
85-
// Optional: Set tolerance for raw illuminance value
86-
zbIlluminanceSensor.setTolerance(1);
87-
88-
// Add endpoint to Zigbee Core
89-
Serial.println("Adding Zigbee illuminance sensor endpoint to Zigbee Core");
90-
Zigbee.addEndpoint(&zbIlluminanceSensor);
91-
92-
Serial.println("Starting Zigbee...");
93-
// When all EPs are registered, start Zigbee in End Device mode
94-
if (!Zigbee.begin()) {
95-
Serial.println("Zigbee failed to start!");
96-
Serial.println("Rebooting...");
97-
ESP.restart();
98-
} else {
99-
Serial.println("Zigbee started successfully!");
100-
}
101-
Serial.println("Connecting to network");
102-
while (!Zigbee.connected()) {
103-
Serial.print(".");
104-
delay(100);
105-
}
106-
Serial.println();
107-
108-
// Start illuminance sensor reading task
109-
xTaskCreate(illuminance_sensor_value_update, "illuminance_sensor_update", 2048, NULL, 10, NULL);
110-
111-
// Set reporting schedule for illuminance value measurement in seconds, must be called after Zigbee.begin()
112-
// min_interval and max_interval in seconds, delta
113-
// if min = 1 and max = 0, delta = 1000, reporting is sent when raw illuminance value changes by 1000, but at most once per second
114-
// if min = 0 and max = 10, delta = 1000, reporting is sent every 10 seconds or if raw illuminance value changes by 1000
115-
// if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of illuminance change
116-
// Note: On pairing with Zigbee Home Automation or Zigbee2MQTT the reporting schedule will most likely be overwritten with their default settings
117-
zbIlluminanceSensor.setReporting(1, 0, 1000);
118-
}
119-
120-
/********************* Main loop **************************/
121-
void loop() {
122-
// Checking button for factory reset
123-
if (digitalRead(button) == LOW) { // Push button pressed
124-
// Key debounce handling
125-
delay(100);
126-
int startTime = millis();
127-
while (digitalRead(button) == LOW) {
128-
delay(50);
129-
if ((millis() - startTime) > 3000) {
130-
// If key pressed for more than 3 secs, factory reset Zigbee and reboot
131-
Serial.println("Resetting Zigbee to factory and rebooting in 1s");
132-
delay(1000);
133-
Zigbee.factoryReset();
134-
}
135-
}
136-
// force report of illuminance when button is pressed
137-
zbIlluminanceSensor.report();
138-
}
139-
delay(100);
140-
}
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+
#define ZIGBEE_ILLUMINANCE_SENSOR_ENDPOINT 9
36+
uint8_t button = BOOT_PIN;
37+
uint8_t illuminance_sensor_pin = 6; // Insert the analog pin to which the sensor (e.g. photoresistor) is connected
38+
39+
ZigbeeIlluminanceSensor zbIlluminanceSensor = ZigbeeIlluminanceSensor(ZIGBEE_ILLUMINANCE_SENSOR_ENDPOINT);
40+
41+
/********************* Illuminance sensor **************************/
42+
static void illuminance_sensor_value_update(void *arg) {
43+
for (;;) {
44+
// read the raw analog value from the sensor
45+
int lsens_analog_raw = analogRead(illuminance_sensor_pin);
46+
Serial.printf("[Illuminance Sensor] raw analog value: %d\r\n", lsens_analog_raw);
47+
48+
// conversion into zigbee raw illuminance value (typically between 0 in darkness and 50000 in direct sunlight)
49+
// depends on the value range of the raw analog sensor values and will need calibration for correct lux values
50+
int lsens_illuminance_raw =
51+
map(lsens_analog_raw, 0, 4095, 0, 50000); // to demonstate map the 12-bit ADC value (0-4095) to Zigbee illuminance range (0-50000)
52+
Serial.printf("[Illuminance Sensor] raw illuminance value: %d\r\n", lsens_illuminance_raw);
53+
54+
// according to zigbee documentation the formular 10^(lsens_illuminance_raw/10000)-1 can be used to calculate lux value from raw illuminance value
55+
// Note: Zigbee2MQTT seems to be using the formular 10^(lsens_illuminance_raw/10000) instead (without -1)
56+
int lsens_illuminance_lux = round(pow(10, (lsens_illuminance_raw / 10000.0)) - 1);
57+
Serial.printf("[Illuminance Sensor] lux value: %d lux\r\n", lsens_illuminance_lux);
58+
59+
// Update illuminance in illuminance sensor EP
60+
zbIlluminanceSensor.setIlluminance(lsens_illuminance_raw); // use raw illuminance here!
61+
62+
delay(1000); // reduce delay (in ms), if you want your device to react more quickly to changes in illuminance
63+
}
64+
}
65+
66+
/********************* Arduino functions **************************/
67+
void setup() {
68+
Serial.begin(115200);
69+
70+
// Optional: configure analog input
71+
analogSetAttenuation(ADC_11db); // set analog to digital converter (ADC) attenuation to 11 dB (up to ~3.3V input)
72+
analogReadResolution(12); // set analog read resolution to 12 bits (value range from 0 to 4095), 12 is default
73+
74+
// Init button for factory reset
75+
pinMode(button, INPUT_PULLUP);
76+
77+
// Optional: Set Zigbee device name and model
78+
zbIlluminanceSensor.setManufacturerAndModel("Espressif", "ZigbeeIlluminanceSensor");
79+
80+
// Optional: Set power source (choose between ZB_POWER_SOURCE_MAINS and ZB_POWER_SOURCE_BATTERY), defaults to unknown
81+
zbIlluminanceSensor.setPowerSource(ZB_POWER_SOURCE_MAINS);
82+
83+
// Set minimum and maximum for raw illuminance value (0 min and 50000 max equals to 0 lux - 100,000 lux)
84+
zbIlluminanceSensor.setMinMaxValue(0, 50000);
85+
86+
// Optional: Set tolerance for raw illuminance value
87+
zbIlluminanceSensor.setTolerance(1);
88+
89+
// Add endpoint to Zigbee Core
90+
Serial.println("Adding Zigbee illuminance sensor endpoint to Zigbee Core");
91+
Zigbee.addEndpoint(&zbIlluminanceSensor);
92+
93+
Serial.println("Starting Zigbee...");
94+
// When all EPs are registered, start Zigbee in End Device mode
95+
if (!Zigbee.begin()) {
96+
Serial.println("Zigbee failed to start!");
97+
Serial.println("Rebooting...");
98+
ESP.restart();
99+
} else {
100+
Serial.println("Zigbee started successfully!");
101+
}
102+
Serial.println("Connecting to network");
103+
while (!Zigbee.connected()) {
104+
Serial.print(".");
105+
delay(100);
106+
}
107+
Serial.println();
108+
109+
// Start illuminance sensor reading task
110+
xTaskCreate(illuminance_sensor_value_update, "illuminance_sensor_update", 2048, NULL, 10, NULL);
111+
112+
// Set reporting schedule for illuminance value measurement in seconds, must be called after Zigbee.begin()
113+
// min_interval and max_interval in seconds, delta
114+
// if min = 1 and max = 0, delta = 1000, reporting is sent when raw illuminance value changes by 1000, but at most once per second
115+
// if min = 0 and max = 10, delta = 1000, reporting is sent every 10 seconds or if raw illuminance value changes by 1000
116+
// if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of illuminance change
117+
// Note: On pairing with Zigbee Home Automation or Zigbee2MQTT the reporting schedule will most likely be overwritten with their default settings
118+
zbIlluminanceSensor.setReporting(1, 0, 1000);
119+
}
120+
121+
/********************* Main loop **************************/
122+
void loop() {
123+
// Checking button for factory reset
124+
if (digitalRead(button) == LOW) { // Push button pressed
125+
// Key debounce handling
126+
delay(100);
127+
int startTime = millis();
128+
while (digitalRead(button) == LOW) {
129+
delay(50);
130+
if ((millis() - startTime) > 3000) {
131+
// If key pressed for more than 3 secs, factory reset Zigbee and reboot
132+
Serial.println("Resetting Zigbee to factory and rebooting in 1s");
133+
delay(1000);
134+
Zigbee.factoryReset();
135+
}
136+
}
137+
// force report of illuminance when button is pressed
138+
zbIlluminanceSensor.report();
139+
}
140+
delay(100);
141+
}

Diff for: libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ ZigbeeIlluminanceSensor::ZigbeeIlluminanceSensor(uint8_t endpoint) : ZigbeeEP(en
77
esp_zb_light_sensor_cfg_t light_sensor_cfg = ZIGBEE_DEFAULT_ILLUMINANCE_SENSOR_CONFIG();
88
_cluster_list = esp_zb_light_sensor_clusters_create(&light_sensor_cfg);
99

10-
_ep_config = {
11-
.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_LIGHT_SENSOR_DEVICE_ID, .app_device_version = 0
12-
};
10+
_ep_config = {.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_LIGHT_SENSOR_DEVICE_ID, .app_device_version = 0};
1311
}
1412

1513
void ZigbeeIlluminanceSensor::setMinMaxValue(uint16_t min, uint16_t max) {
@@ -22,7 +20,7 @@ void ZigbeeIlluminanceSensor::setMinMaxValue(uint16_t min, uint16_t max) {
2220
void ZigbeeIlluminanceSensor::setTolerance(uint16_t tolerance) {
2321
esp_zb_attribute_list_t *light_measure_cluster =
2422
esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
25-
esp_zb_illuminance_meas_cluster_add_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_TOLERANCE_ID, (void *)&tolerance);
23+
esp_zb_illuminance_meas_cluster_add_attr(light_measure_cluster, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_TOLERANCE_ID, (void *)&tolerance);
2624
}
2725

2826
void ZigbeeIlluminanceSensor::setReporting(uint16_t min_interval, uint16_t max_interval, uint16_t delta) {
@@ -51,7 +49,8 @@ void ZigbeeIlluminanceSensor::setIlluminance(uint16_t illuminanceValue) {
5149
log_d("Setting Illuminance to %d", illuminanceValue);
5250
esp_zb_lock_acquire(portMAX_DELAY);
5351
esp_zb_zcl_set_attribute_val(
54-
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MEASURED_VALUE_ID, &illuminanceValue, false
52+
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_ILLUMINANCE_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MEASURED_VALUE_ID,
53+
&illuminanceValue, false
5554
);
5655
esp_zb_lock_release();
5756
}

Diff for: libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.h

+17-18
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,23 @@
99
#include "ZigbeeEP.h"
1010
#include "ha/esp_zigbee_ha_standard.h"
1111

12-
#define ZIGBEE_DEFAULT_ILLUMINANCE_SENSOR_CONFIG() \
13-
{ \
14-
.basic_cfg = \
15-
{ \
16-
.zcl_version = ESP_ZB_ZCL_BASIC_ZCL_VERSION_DEFAULT_VALUE, \
17-
.power_source = ESP_ZB_ZCL_BASIC_POWER_SOURCE_DEFAULT_VALUE, \
18-
}, \
19-
.identify_cfg = \
20-
{ \
21-
.identify_time = ESP_ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE, \
22-
}, \
23-
.illuminance_cfg = \
24-
{ \
25-
.measured_value = ESP_ZB_ZCL_ILLUMINANCE_MEASUREMENT_LIGHT_SENSOR_TYPE_DEFAULT_VALUE, \
26-
.min_value = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MIN_MEASURED_VALUE_MIN_VALUE, \
27-
.max_value = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MAX_MEASURED_VALUE_MAX_VALUE, \
28-
}, \
29-
}
12+
#define ZIGBEE_DEFAULT_ILLUMINANCE_SENSOR_CONFIG() \
13+
{ \
14+
.basic_cfg = \
15+
{ \
16+
.zcl_version = ESP_ZB_ZCL_BASIC_ZCL_VERSION_DEFAULT_VALUE, \
17+
.power_source = ESP_ZB_ZCL_BASIC_POWER_SOURCE_DEFAULT_VALUE, \
18+
}, \
19+
.identify_cfg = \
20+
{ \
21+
.identify_time = ESP_ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE, \
22+
}, \
23+
.illuminance_cfg = { \
24+
.measured_value = ESP_ZB_ZCL_ILLUMINANCE_MEASUREMENT_LIGHT_SENSOR_TYPE_DEFAULT_VALUE, \
25+
.min_value = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MIN_MEASURED_VALUE_MIN_VALUE, \
26+
.max_value = ESP_ZB_ZCL_ATTR_ILLUMINANCE_MEASUREMENT_MAX_MEASURED_VALUE_MAX_VALUE, \
27+
}, \
28+
}
3029

3130
class ZigbeeIlluminanceSensor : public ZigbeeEP {
3231
public:

0 commit comments

Comments
 (0)