32
32
33
33
#include " Zigbee.h"
34
34
35
+ /* Zigbee OTA configuration */
36
+
37
+ #define OTA_UPGRADE_RUNNING_FILE_VERSION 0x01010100 // Increment this value when the running image is updated
38
+ #define OTA_UPGRADE_DOWNLOADED_FILE_VERSION 0x01010101 // Increment this value when the downloaded image is updated
39
+ #define OTA_UPGRADE_HW_VERSION 0x0101 // The hardware version, this can be used to differentiate between different hardware versions
40
+
35
41
#define USE_GLOBAL_ON_RESPONSE_CALLBACK 1 // Set to 0 to use local callback specified directly for the endpoint.
36
42
37
43
/* Zigbee temperature + humidity sensor configuration */
@@ -45,6 +51,7 @@ uint8_t button = BOOT_PIN;
45
51
46
52
ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER);
47
53
54
+ volatile bool otaInhibitSleep = false ;
48
55
uint8_t dataToSend = 2 ; // Temperature and humidity values are reported in same endpoint, so 2 values are reported
49
56
bool resend = false ;
50
57
@@ -73,6 +80,15 @@ void onResponse(zb_cmd_type_t command, esp_zb_zcl_status_t status) {
73
80
}
74
81
#endif
75
82
83
+ void otaSleepInhibitCallback (bool otaActive) {
84
+ otaInhibitSleep = otaActive;
85
+ if (otaActive) {
86
+ Serial.println (" OTA started: inhibiting sleep" );
87
+ } else {
88
+ Serial.println (" OTA finished: sleep allowed" );
89
+ }
90
+ }
91
+
76
92
/* *********************** Temp sensor *****************************/
77
93
static void meausureAndSleep (void *arg) {
78
94
// Measure temperature sensor value
@@ -115,8 +131,10 @@ static void meausureAndSleep(void *arg) {
115
131
}
116
132
117
133
// Put device to deep sleep after data was sent successfully or timeout
118
- Serial.println (" Going to sleep now" );
119
- esp_deep_sleep_start ();
134
+ if (!otaInhibitSleep) {
135
+ Serial.println (" Going to sleep now" );
136
+ esp_deep_sleep_start ();
137
+ }
120
138
}
121
139
122
140
/* ******************** Arduino functions **************************/
@@ -132,6 +150,12 @@ void setup() {
132
150
// Optional: set Zigbee device name and model
133
151
zbTempSensor.setManufacturerAndModel (" Espressif" , " SleepyZigbeeTempSensor" );
134
152
153
+ // Optional: set ota
154
+ zbTempSensor.addOTAClient (OTA_UPGRADE_RUNNING_FILE_VERSION, OTA_UPGRADE_DOWNLOADED_FILE_VERSION, OTA_UPGRADE_HW_VERSION);
155
+
156
+ // Optional: Register callback for OTA state change (inhibit sleep during OTA)
157
+ zbTempSensor.onOTAStateChange (otaSleepInhibitCallback);
158
+
135
159
// Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement)
136
160
zbTempSensor.setMinMaxValue (10 , 50 );
137
161
@@ -180,6 +204,9 @@ void setup() {
180
204
Serial.println ();
181
205
Serial.println (" Successfully connected to Zigbee network" );
182
206
207
+ // Start Zigbee OTA client query, first request is within a minute and the next requests are sent every hour automatically
208
+ zbLight.requestOTAUpdate ();
209
+
183
210
// Start Temperature sensor reading task
184
211
xTaskCreate (meausureAndSleep, " temp_sensor_update" , 2048 , NULL , 10 , NULL );
185
212
}
@@ -193,6 +220,10 @@ void loop() {
193
220
while (digitalRead (button) == LOW) {
194
221
delay (50 );
195
222
if ((millis () - startTime) > 10000 ) {
223
+ if (otaInhibitSleep) {
224
+ Serial.println (" OTA in progress, cannot reset now" );
225
+ break ;
226
+ }
196
227
// If key pressed for more than 10secs, factory reset Zigbee and reboot
197
228
Serial.println (" Resetting Zigbee to factory and rebooting in 1s." );
198
229
delay (1000 );
0 commit comments