Skip to content

Commit 6d7b544

Browse files
Some last corrections
1 parent 8fc75db commit 6d7b544

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

libraries/Zigbee/examples/Zigbee_Light_Sensor/Zigbee_Light_Sensor.ino

+14-13
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,21 @@
4242
static void light_sensor_value_update(void *arg) {
4343
for (;;) {
4444
// read the raw analog value from the sensor
45-
int lsens_analog = analogRead(light_sensor_pin);
46-
Serial.printf("[Light Sensor] raw analog value: %d°C\r\n", lsens_analog);
45+
int lsens_analog_raw = analogRead(light_sensor_pin);
46+
Serial.printf("[Light Sensor] raw analog value: %d\r\n", lsens_analog_raw);
4747

48-
// conversion into zigbee illuminance number value (typically between 0 in darkness and 50000 in direct sunlight)
48+
// conversion into zigbee raw illuminance value (typically between 0 in darkness and 50000 in direct sunlight)
4949
// depends on the value range of the raw analog sensor values and will need calibration for correct lux values
50-
int lsens_value = lsens_analog * 10; // mult by 10 for the sake of this example
51-
Serial.printf("[Light Sensor] number value: %d°C\r\n", lsens_value);
50+
int lsens_illuminance_raw = lsens_analog_raw * 10; // multiply by 10 for the sake of this example
51+
Serial.printf("[Light Sensor] raw illuminance value: %d\r\n", lsens_illuminance_raw);
5252

53-
// zigbee uses the formular below to calculate lx (lux) values from the number values
54-
int lsens_value_lx = (int) 10^(lsens_value/10000)-1;
55-
Serial.printf("[Light Sensor] lux value: %d°C\r\n", lsens_value_lx);
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("[Light Sensor] lux value: %d lux\r\n", lsens_illuminance_lux);
5657

5758
// Update illuminance in light sensor EP
58-
zbLightSensor.setIlluminance(lsens_value); // use the number value here!
59+
zbLightSensor.setIlluminance(lsens_illuminance_raw); // use raw illuminance here!
5960

6061
delay(1000); // reduce delay (in ms), if you want your device to react more quickly to changes in illuminance
6162
}
@@ -78,10 +79,10 @@
7879
// Optional: Set power source (choose between ZB_POWER_SOURCE_MAINS and ZB_POWER_SOURCE_BATTERY), defaults to unknown
7980
zbLightSensor.setPowerSource(ZB_POWER_SOURCE_MAINS);
8081

81-
// Set minimum and maximum illuminance number value (0 min and 50000 max equals to 0 lx - 100,000 lx)
82+
// Set minimum and maximum for raw illuminance value (0 min and 50000 max equals to 0 lux - 100,000 lux)
8283
zbLightSensor.setMinMaxValue(0, 50000);
8384

84-
// Optional: Set tolerance for illuminance measurement
85+
// Optional: Set tolerance for raw illuminance value
8586
zbLightSensor.setTolerance(1);
8687

8788
// Add endpoint to Zigbee Core
@@ -109,8 +110,8 @@
109110

110111
// Set reporting schedule for illuminance value measurement in seconds, must be called after Zigbee.begin()
111112
// min_interval and max_interval in seconds, delta
112-
// if min = 1 and max = 0, delta = 1000, reporting is sent when illuminance value changes by 1000, but at most once per second
113-
// if min = 0 and max = 10, delta = 1000, reporting is sent every 10 seconds or if illuminance value changes by 1000
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
114115
// if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of illuminance change
115116
// Note: On pairing with Zigbee Home Automation or Zigbee2MQTT the reporting schedule will most likely be overwritten with their default settings
116117
zbLightSensor.setReporting(1, 0, 1000);

0 commit comments

Comments
 (0)