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.
master
Dustin 2022-05-16 20:34:23 -05:00
parent e628508ff5
commit 6c1757b43d
2 changed files with 15 additions and 5 deletions

View File

@ -9,4 +9,5 @@
#define TOPIC_ERRORS "garden/errors" #define TOPIC_ERRORS "garden/errors"
#define TOPIC_STATE "garden/state" #define TOPIC_STATE "garden/state"
#define SLEEP_MILLIS (1000 * 60) #define SLEEP_MILLIS_EARLY (1000 * 60)
#define SLEEP_MILLIS (1000 * 60 * 60)

View File

@ -11,6 +11,8 @@
INCTXT(RootCA, "isrgrootx1.pem"); INCTXT(RootCA, "isrgrootx1.pem");
RTC_DATA_ATTR uint32_t boot_count = 0;
WiFiClientSecure sock; WiFiClientSecure sock;
PubSubClient mqtt(sock); PubSubClient mqtt(sock);
@ -18,12 +20,17 @@ Values values;
Adafruit_seesaw ss; Adafruit_seesaw ss;
void setup() { void setup() {
delay(1000); // VSCode is slow to open the serial console after upload
Serial.begin(115200); Serial.begin(115200);
pinMode(13, OUTPUT); pinMode(13, OUTPUT);
digitalWrite(13, 1); 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()) { if (!wifi_connect()) {
Serial.printf("Failed to connect to WiFi, status %s\n", WiFi.status()); Serial.printf("Failed to connect to WiFi, status %s\n", WiFi.status());
reboot(); reboot();
@ -65,9 +72,11 @@ void setup() {
mqtt.disconnect(); mqtt.disconnect();
delay(1000); 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(); Serial.flush();
esp_sleep_enable_timer_wakeup((SLEEP_MILLIS - millis()) * 1000); esp_sleep_enable_timer_wakeup(timer * 1000);
esp_deep_sleep_start(); esp_deep_sleep_start();
} }