Nimbin[12]?Embedded / esp_garden / commits / ccc6b2a

esp_garden git · master

Distributed ESP sensor and actuator system (garden)

esp32 esp-idf iot sensors c++ sql · first commit 2025-10-15 · last commit 2026-01-20 (8 months ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/mic/esp_garden

C++ 70.7% Markdown 18% C 10.7%
git clone https://git.christianimmanuel.de/embedded/esp_garden.gitwget https://git.christianimmanuel.de/embedded/esp_garden/archive/esp_garden.tar.gz

Add setup time to wakeup

Christian Immanuel · 2025-10-23 21:30 · ccc6b2afbf8cc4790fc168b8a7783919e1cb9743

 client_sensor/ClientSensor.cpp  | 21 ++++++++++++++-------
 client_sensor/ClientSensor.h    |  2 ++
 client_sensor/client_sensor.ino |  9 +++++++--
 globals.cpp                     |  3 ++-
 globals.h                       | 10 +++++++++-
 server/server.ino               | 11 +++--------
 6 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/client_sensor/ClientSensor.cpp b/client_sensor/ClientSensor.cpp
index 877007e..4e1d088 100644
--- a/client_sensor/ClientSensor.cpp
+++ b/client_sensor/ClientSensor.cpp
@@ -6,7 +6,6 @@ volatile bool ack_received = false;
 volatile bool send_process_complete = false;
 volatile bool send_ack_process_complete = false;
 
-
 // ----------------------------------------------------------------------------
 // Helper
 // ----------------------------------------------------------------------------
@@ -395,19 +394,27 @@ void sleep_UntilNextSend() {
 
     if (min_delay_ms == UINT32_MAX) return;
 
+
+    // i just need the next sensor read from here, the sleep part should get handled somewhere else as other esp need the next sensor read, sleep is just for that one.
+
     // Prevent sleeping if next send is within 1 minute
-    if (min_delay_ms < min_sleep_time) {
-        DBG_PRINTF("Delaying for %u ms\n", min_delay_ms);
+    if (min_delay_ms < 60 * 1000) {
+        DBG_PRINTF("Delaying for %u ms (no deep sleep)\n", min_delay_ms);
         DBG_PRINTF("######################\n\n");
         delay(min_delay_ms);
         return;
     }
 
-    DBG_PRINTF("Sleeping for %u ms\n", min_delay_ms);
-    esp_sleep_enable_timer_wakeup((uint64_t)min_delay_ms * 1000ULL);
-    // set server_time for the case we dont get a new connection.
-    // idk why its 1000 to big,..
+    uint32_t prep_time_ms = g_setup_duration + g_wakeup_earlier;
+    uint32_t sleep_time_ms = (min_delay_ms > prep_time_ms)
+                           ? (min_delay_ms - prep_time_ms)
+                           : 0;
+
+    DBG_PRINTF("Sleeping for %u ms (prep margin: %u ms)\n",
+               sleep_time_ms, prep_time_ms);
     DBG_PRINTF("######################\n\n");
+
+    esp_sleep_enable_timer_wakeup((uint64_t)sleep_time_ms * 1000ULL);
     esp_deep_sleep_start();
 }
 
diff --git a/client_sensor/ClientSensor.h b/client_sensor/ClientSensor.h
index 766ca8e..5d8aa45 100644
--- a/client_sensor/ClientSensor.h
+++ b/client_sensor/ClientSensor.h
@@ -11,6 +11,8 @@
 #include <sys/time.h>
 
 
+
+
 // ----------------------------------------------------------------------------
 // WiFi handling
 // ----------------------------------------------------------------------------
diff --git a/client_sensor/client_sensor.ino b/client_sensor/client_sensor.ino
index 5562f38..6e5194d 100644
--- a/client_sensor/client_sensor.ino
+++ b/client_sensor/client_sensor.ino
@@ -9,6 +9,8 @@ RTC_DATA_ATTR uint32_t fake_value_to_send = 0;
 // Setup
 // ----------------------------------------------------------------------------
 void setup() {
+    uint32_t setup_start = millis();
+
     DBG_BEGIN();
     setup_Timezone();
     init_WiFi();
@@ -27,6 +29,11 @@ void setup() {
         get_AllSensorConfigs();
 
     DBG_PRINTF("----------------------\n");
+
+    ensure_Peers();
+
+    g_setup_duration = millis() - setup_start;
+    DBG_PRINTF("Setup duration: %u ms\n", g_setup_duration);
 }
 
 // ----------------------------------------------------------------------------
@@ -34,8 +41,6 @@ void setup() {
 // ----------------------------------------------------------------------------
 
 void loop() {
-    ensure_Peers();
-
     DBG_PRINTF("## Unix time: %lld\n", (long long)get_CurrentUnixMs() / 1000);
     for (size_t config_id = 0; config_id < SENSOR_CONFIG_COUNT; config_id++) {
         if ( is_TimeToReadSensor(config_id) ) {
diff --git a/globals.cpp b/globals.cpp
index 8e94976..11c7af4 100644
--- a/globals.cpp
+++ b/globals.cpp
@@ -1,7 +1,8 @@
 #include "globals.h"
 
+uint32_t g_setup_duration = 0;
+uint32_t g_wakeup_earlier = 2000;
 
-int64_t g_time_now = 0;
 SensorEntry g_buffer[BUFFER_SIZE]; 
 uint8_t g_buffer_count = 0;
 char g_pending_sensor_configs[4][32] = {};
diff --git a/globals.h b/globals.h
index 352858e..d0b4985 100644
--- a/globals.h
+++ b/globals.h
@@ -1,4 +1,5 @@
-#pragma once
+#ifndef GLOBALS_H
+#define GLOBALS_H
 
 #include <Arduino.h>
 
@@ -33,6 +34,10 @@
 
 #define SENSOR_CONFIG_COUNT 2
 
+
+extern uint32_t g_setup_duration; // getting set automaticall in setup so we know how long it takes
+extern uint32_t g_wakeup_earlier; // some safety space if setup takes longere (maybe 20sec safety?)
+
 struct SensorConfigServer_s {
     char sensor_name[32];
     bool success;
@@ -87,3 +92,6 @@ extern const uint8_t PEER_SERVER[];
 extern const uint8_t PEER_PUMP[];
 extern RTC_DATA_ATTR SensorConfig_s sensor_configs[];
 extern uint8_t config_request_attempts_max;
+
+
+#endif
diff --git a/server/server.ino b/server/server.ino
index 14d1474..c7a26c9 100644
--- a/server/server.ino
+++ b/server/server.ino
@@ -101,7 +101,7 @@ void add_Peer(const uint8_t* addr) {
 }
 
 // ----------------------------------------------------------------------------
-// ESP-NOW receive callback (reply with time)
+// ESP-NOW receive callback
 // ----------------------------------------------------------------------------
 
 void on_DataRecv(const esp_now_recv_info *info, const uint8_t *data, int len) {
@@ -140,7 +140,6 @@ void on_DataRecv(const esp_now_recv_info *info, const uint8_t *data, int len) {
     DBG_PRINTF("  %s\n", msg);
     g_received_ok_from_server = false;
 
-    // expected format: <sensor_name>:<value>:<timestamp>
     char *first = strchr(msg, ':');
     char *second = first ? strchr(first + 1, ':') : NULL;
 
@@ -157,13 +156,9 @@ void on_DataRecv(const esp_now_recv_info *info, const uint8_t *data, int len) {
         DBG_PRINTLN("Invalid message format (expected <name>:<value>:<time>)");
     }
 
-    // --- Default reply with time ---
     /*
-    char time_msg[32];
-    snprintf(time_msg, sizeof(time_msg), "time:%lu", now());
-    DBG_PRINTLN("Send Answer");
-    esp_now_send(received_from_mac_addr, (uint8_t*)time_msg, strlen(time_msg));
-    */
+     * Replying to Sensor happens in main loop
+     */
 }