3737
3838#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
3939#define TIME_TO_SLEEP 55 /* Sleep for 55s will + 5s delay for establishing connection => data reported every 1 minute */
40+ #define REPORT_TIMEOUT 30000 /* Timeout for reporting data in ms */
4041
4142uint8_t button = BOOT_PIN;
4243
@@ -57,10 +58,14 @@ void meausureAndSleep() {
5758 zbTempSensor.setTemperature (temperature);
5859 zbTempSensor.setHumidity (humidity);
5960
61+
6062 // Report temperature and humidity values
6163 zbTempSensor.report (); // reports temperature and humidity values (if humidity sensor is not added, only temperature is reported)
6264 Serial.printf (" Reported temperature: %.2f°C, Humidity: %.2f%%\r\n " , temperature, humidity);
6365
66+ unsigned long startTime = millis ();
67+ const unsigned long timeout = REPORT_TIMEOUT;
68+
6469 // Wait until data was succesfully sent
6570 while (dataToSend != 0 ){
6671 if (resend){
@@ -69,15 +74,19 @@ void meausureAndSleep() {
6974 dataToSend = 2 ;
7075 zbTempSensor.report (); // report again
7176 }
77+ if (millis () - startTime >= timeout) {
78+ Serial.println (" Report timeout!" );
79+ break ;
80+ }
7281 }
7382
74- // Put device to deep sleep after data was sent successfully
83+ // Put device to deep sleep after data was sent successfully or timeout
7584 Serial.println (" Going to sleep now" );
7685 esp_deep_sleep_start ();
7786}
7887
7988void onResponse (zb_cmd_type_t command, esp_zb_zcl_status_t status){
80- Serial.printf (" Response status recieved %s" , zbTempSensor.esp_zb_zcl_status_to_name (status));
89+ Serial.printf (" Response status received %s\r\n " , zbTempSensor.esp_zb_zcl_status_to_name (status));
8190 if (command == ZB_CMD_REPORT_ATTRIBUTE){
8291 switch (status){
8392 case ESP_ZB_ZCL_STATUS_SUCCESS: dataToSend--; break ;
0 commit comments