diff --git a/constants.h b/constants.h index 401acad..ee9ecd8 100644 --- a/constants.h +++ b/constants.h @@ -9,4 +9,5 @@ #define TOPIC_ERRORS "garden/errors" #define TOPIC_STATE "garden/state" -#define SLEEP_MILLIS (1000 * 60) \ No newline at end of file +#define SLEEP_MILLIS_EARLY (1000 * 60) +#define SLEEP_MILLIS (1000 * 60 * 60) \ No newline at end of file diff --git a/gardensensor.ino b/gardensensor.ino index 6c6590c..df18ccc 100644 --- a/gardensensor.ino +++ b/gardensensor.ino @@ -11,6 +11,8 @@ INCTXT(RootCA, "isrgrootx1.pem"); +RTC_DATA_ATTR uint32_t boot_count = 0; + WiFiClientSecure sock; PubSubClient mqtt(sock); @@ -18,12 +20,17 @@ Values values; Adafruit_seesaw ss; void setup() { - delay(1000); // VSCode is slow to open the serial console after upload - Serial.begin(115200); pinMode(13, OUTPUT); digitalWrite(13, 1); + // VSCode is slow to open the serial console after upload + if (boot_count == 0) { + delay(1000); + } + boot_count++; + Serial.printf("This is boot number %d\n", boot_count); + if (!wifi_connect()) { Serial.printf("Failed to connect to WiFi, status %s\n", WiFi.status()); reboot(); @@ -65,9 +72,11 @@ void setup() { mqtt.disconnect(); delay(1000); - Serial.println("Entering deep sleep ..."); + unsigned long timer = + ((boot_count <= 5 ? SLEEP_MILLIS_EARLY : SLEEP_MILLIS) - millis()); + Serial.printf("Entering deep sleep for %d milliseconds...\n", timer); Serial.flush(); - esp_sleep_enable_timer_wakeup((SLEEP_MILLIS - millis()) * 1000); + esp_sleep_enable_timer_wakeup(timer * 1000); esp_deep_sleep_start(); }