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+ }
0 commit comments