From 6c1757b43d855cd17f711d2905f94891e2d7813f Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Mon, 16 May 2022 20:34:23 -0500 Subject: [PATCH] main: Deep sleep for up to an hour It really isn't necessary to have minute-level granularity of soil moisture (especially since it's just been sitting at "max" for 2 weeks!). Updating frequently is helpful for diagnostics, though. To compromise, the sensor will now publish data every minute for the first few minutes after it starts up, then reduce its update frequency to once every hour. --- constants.h | 3 ++- gardensensor.ino | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) 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(); }