|
42 | 42 | static void light_sensor_value_update(void *arg) {
|
43 | 43 | for (;;) {
|
44 | 44 | // 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); |
47 | 47 |
|
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) |
49 | 49 | // 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); |
52 | 52 |
|
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); |
56 | 57 |
|
57 | 58 | // 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! |
59 | 60 |
|
60 | 61 | delay(1000); // reduce delay (in ms), if you want your device to react more quickly to changes in illuminance
|
61 | 62 | }
|
|
78 | 79 | // Optional: Set power source (choose between ZB_POWER_SOURCE_MAINS and ZB_POWER_SOURCE_BATTERY), defaults to unknown
|
79 | 80 | zbLightSensor.setPowerSource(ZB_POWER_SOURCE_MAINS);
|
80 | 81 |
|
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) |
82 | 83 | zbLightSensor.setMinMaxValue(0, 50000);
|
83 | 84 |
|
84 |
| - // Optional: Set tolerance for illuminance measurement |
| 85 | + // Optional: Set tolerance for raw illuminance value |
85 | 86 | zbLightSensor.setTolerance(1);
|
86 | 87 |
|
87 | 88 | // Add endpoint to Zigbee Core
|
|
109 | 110 |
|
110 | 111 | // Set reporting schedule for illuminance value measurement in seconds, must be called after Zigbee.begin()
|
111 | 112 | // 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 |
114 | 115 | // if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of illuminance change
|
115 | 116 | // Note: On pairing with Zigbee Home Automation or Zigbee2MQTT the reporting schedule will most likely be overwritten with their default settings
|
116 | 117 | zbLightSensor.setReporting(1, 0, 1000);
|
|
0 commit comments