3232
3333#include " Zigbee.h"
3434
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+
3541#define USE_GLOBAL_ON_RESPONSE_CALLBACK 1 // Set to 0 to use local callback specified directly for the endpoint.
3642
3743/* Zigbee temperature + humidity sensor configuration */
@@ -45,6 +51,7 @@ uint8_t button = BOOT_PIN;
4551
4652ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER);
4753
54+ volatile bool otaInhibitSleep = false ;
4855uint8_t dataToSend = 2 ; // Temperature and humidity values are reported in same endpoint, so 2 values are reported
4956bool resend = false ;
5057
@@ -73,6 +80,15 @@ void onResponse(zb_cmd_type_t command, esp_zb_zcl_status_t status) {
7380}
7481#endif
7582
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+
7692/* *********************** Temp sensor *****************************/
7793static void meausureAndSleep (void *arg) {
7894 // Measure temperature sensor value
@@ -115,8 +131,10 @@ static void meausureAndSleep(void *arg) {
115131 }
116132
117133 // 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+ }
120138}
121139
122140/* ******************** Arduino functions **************************/
@@ -132,6 +150,12 @@ void setup() {
132150 // Optional: set Zigbee device name and model
133151 zbTempSensor.setManufacturerAndModel (" Espressif" , " SleepyZigbeeTempSensor" );
134152
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+
135159 // Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement)
136160 zbTempSensor.setMinMaxValue (10 , 50 );
137161
@@ -180,6 +204,9 @@ void setup() {
180204 Serial.println ();
181205 Serial.println (" Successfully connected to Zigbee network" );
182206
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+
183210 // Start Temperature sensor reading task
184211 xTaskCreate (meausureAndSleep, " temp_sensor_update" , 2048 , NULL , 10 , NULL );
185212}
@@ -193,6 +220,10 @@ void loop() {
193220 while (digitalRead (button) == LOW) {
194221 delay (50 );
195222 if ((millis () - startTime) > 10000 ) {
223+ if (otaInhibitSleep) {
224+ Serial.println (" OTA in progress, cannot reset now" );
225+ break ;
226+ }
196227 // If key pressed for more than 10secs, factory reset Zigbee and reboot
197228 Serial.println (" Resetting Zigbee to factory and rebooting in 1s." );
198229 delay (1000 );
0 commit comments