16
16
* @brief This example demonstrates Zigbee analog input / output device.
17
17
*
18
18
* The example demonstrates how to use Zigbee library to create a end device analog device.
19
- *
19
+ *
20
20
* Proper Zigbee mode must be selected in Tools->Zigbee mode
21
21
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
22
22
*
26
26
* Modified by Pat Clay
27
27
*/
28
28
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
- /* Zigbee analog device configuration */
36
- #define ANALOG_DEVICE_ENDPOINT_NUMBER 1
37
-
38
- uint8_t analogPin = A0;
39
- uint8_t button = BOOT_PIN;
40
-
41
- ZigbeeAnalog zbAnalogDevice = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER);
42
-
43
- void onAnalogOutputChange (float analog_output) {
44
- Serial.printf (" Received analog output change: %.1f\r\n " , analog_output);
45
- }
46
-
47
- void setup () {
48
- Serial.begin (115200 );
49
- Serial.println (" Starting..." );
50
-
51
- // Init button switch
52
- pinMode (button, INPUT_PULLUP);
53
-
54
- // Set analog resolution to 10 bits
55
- analogReadResolution (10 );
56
-
57
- // Optional: set Zigbee device name and model
58
- zbAnalogDevice.setManufacturerAndModel (" Espressif" , " ZigbeeAnalogDevice" );
59
-
60
- // Add analog sensor cluster to Zigbee Analog accoding your needs
61
- zbAnalogDevice.addAnalogInput ();
62
- zbAnalogDevice.addAnalogOutput ();
63
-
64
- // If analog output cluster is added, set callback function for analog output change
65
- zbAnalogDevice.onAnalogOutputChange (onAnalogOutputChange);
66
-
67
- // Add endpoints to Zigbee Core
68
- Zigbee.addEndpoint (&zbAnalogDevice);
69
-
70
- Serial.println (" Starting Zigbee..." );
71
- // When all EPs are registered, start Zigbee in End Device mode
72
- if (!Zigbee.begin ()) {
73
- Serial.println (" Zigbee failed to start!" );
74
- Serial.println (" Rebooting..." );
75
- ESP.restart ();
76
- } else {
77
- Serial.println (" Zigbee started successfully!" );
78
- }
79
- Serial.println (" Connecting to network" );
80
- while (!Zigbee.connected ()) {
81
- Serial.print (" ." );
82
- delay (100 );
83
- }
84
- Serial.println (" Connected" );
85
-
86
- // Optional: Add reporting for analog input
87
- zbAnalogDevice.setAnalogInputReporting (0 , 30 , 10 ); // report every 30 seconds if value changes by 10
88
- }
89
-
90
- void loop () {
91
- static uint32_t timeCounter = 0 ;
92
-
93
- // Read ADC value and update the analog value every 2s
94
- if (!(timeCounter++ % 20 )) { // delaying for 100ms x 20 = 2s
95
- float analog = (float )analogRead (analogPin);
96
- Serial.printf (" Updating analog input to %.1f\r\n " , analog);
97
- zbAnalogDevice.setAnalogInput (analog);
98
-
99
- // Analog input supports reporting
100
- zbAnalogDevice.reportAnalogInput ();
101
- }
102
-
103
- // Checking button for factory reset and reporting
104
- if (digitalRead (button) == LOW) { // Push button pressed
105
- // Key debounce handling
106
- delay (100 );
107
- int startTime = millis ();
108
- while (digitalRead (button) == LOW) {
109
- delay (50 );
110
- if ((millis () - startTime) > 3000 ) {
111
- // If key pressed for more than 3secs, factory reset Zigbee and reboot
112
- Serial.println (" Resetting Zigbee to factory and rebooting in 1s." );
113
- delay (1000 );
114
- Zigbee.factoryReset ();
115
- }
116
- }
117
- }
118
- delay (100 );
119
- }
120
-
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
+ /* Zigbee analog device configuration */
36
+ #define ANALOG_DEVICE_ENDPOINT_NUMBER 1
37
+
38
+ uint8_t analogPin = A0;
39
+ uint8_t button = BOOT_PIN;
40
+
41
+ ZigbeeAnalog zbAnalogDevice = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER);
42
+
43
+ void onAnalogOutputChange (float analog_output) {
44
+ Serial.printf (" Received analog output change: %.1f\r\n " , analog_output);
45
+ }
46
+
47
+ void setup () {
48
+ Serial.begin (115200 );
49
+ Serial.println (" Starting..." );
50
+
51
+ // Init button switch
52
+ pinMode (button, INPUT_PULLUP);
53
+
54
+ // Set analog resolution to 10 bits
55
+ analogReadResolution (10 );
56
+
57
+ // Optional: set Zigbee device name and model
58
+ zbAnalogDevice.setManufacturerAndModel (" Espressif" , " ZigbeeAnalogDevice" );
59
+
60
+ // Add analog sensor cluster to Zigbee Analog accoding your needs
61
+ zbAnalogDevice.addAnalogInput ();
62
+ zbAnalogDevice.addAnalogOutput ();
63
+
64
+ // If analog output cluster is added, set callback function for analog output change
65
+ zbAnalogDevice.onAnalogOutputChange (onAnalogOutputChange);
66
+
67
+ // Add endpoints to Zigbee Core
68
+ Zigbee.addEndpoint (&zbAnalogDevice);
69
+
70
+ Serial.println (" Starting Zigbee..." );
71
+ // When all EPs are registered, start Zigbee in End Device mode
72
+ if (!Zigbee.begin ()) {
73
+ Serial.println (" Zigbee failed to start!" );
74
+ Serial.println (" Rebooting..." );
75
+ ESP.restart ();
76
+ } else {
77
+ Serial.println (" Zigbee started successfully!" );
78
+ }
79
+ Serial.println (" Connecting to network" );
80
+ while (!Zigbee.connected ()) {
81
+ Serial.print (" ." );
82
+ delay (100 );
83
+ }
84
+ Serial.println (" Connected" );
85
+
86
+ // Optional: Add reporting for analog input
87
+ zbAnalogDevice.setAnalogInputReporting (0 , 30 , 10 ); // report every 30 seconds if value changes by 10
88
+ }
89
+
90
+ void loop () {
91
+ static uint32_t timeCounter = 0 ;
92
+
93
+ // Read ADC value and update the analog value every 2s
94
+ if (!(timeCounter++ % 20 )) { // delaying for 100ms x 20 = 2s
95
+ float analog = (float )analogRead (analogPin);
96
+ Serial.printf (" Updating analog input to %.1f\r\n " , analog);
97
+ zbAnalogDevice.setAnalogInput (analog);
98
+
99
+ // Analog input supports reporting
100
+ zbAnalogDevice.reportAnalogInput ();
101
+ }
102
+
103
+ // Checking button for factory reset and reporting
104
+ if (digitalRead (button) == LOW) { // Push button pressed
105
+ // Key debounce handling
106
+ delay (100 );
107
+ int startTime = millis ();
108
+ while (digitalRead (button) == LOW) {
109
+ delay (50 );
110
+ if ((millis () - startTime) > 3000 ) {
111
+ // If key pressed for more than 3secs, factory reset Zigbee and reboot
112
+ Serial.println (" Resetting Zigbee to factory and rebooting in 1s." );
113
+ delay (1000 );
114
+ Zigbee.factoryReset ();
115
+ }
116
+ }
117
+ }
118
+ delay (100 );
119
+ }
0 commit comments