Nimbin[12]?Embedded / esp_garden / commits / 708ce7e

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

Fix timing stuff

Christian Immanuel · 2025-10-24 19:25 · 708ce7e3817b690c535a4f7520f0221c5aaae8e0

 client_sensor/ClientSensor.cpp  | 74 +++++++++++++++++++++++------------------
 client_sensor/ClientSensor.h    |  3 +-
 client_sensor/client_sensor.ino | 18 ++++++++--
 3 files changed, 58 insertions(+), 37 deletions(-)

diff --git a/client_sensor/ClientSensor.cpp b/client_sensor/ClientSensor.cpp
index 22870c8..5c1fc4f 100644
--- a/client_sensor/ClientSensor.cpp
+++ b/client_sensor/ClientSensor.cpp
@@ -6,6 +6,8 @@ volatile bool ack_received = false;
 volatile bool send_process_complete = false;
 volatile bool send_ack_process_complete = false;
 
+uint8_t config_request_attempts = 0;
+
 // ----------------------------------------------------------------------------
 // Helper
 // ----------------------------------------------------------------------------
@@ -45,8 +47,6 @@ void init_WiFi() {
 // Peers
 // ----------------------------------------------------------------------------
 
-uint8_t config_request_attempts = 0;
-
 void print_SensorConfig(const SensorConfig_s& cfg) {
     DBG_PRINTF("SensorConfig for '%s'\n", cfg.sensor_name);
     DBG_PRINTF("  is_active: %s\n", cfg.is_active ? "true" : "false");
@@ -367,16 +367,34 @@ uint16_t get_CurrentMinutes() {
 // ----------------------------------------------------------------------------
 // Deep sleep scheduler
 // ----------------------------------------------------------------------------
-int64_t getAndSet_NextSensorReadFor(const char* sensor_name) {
-    int64_t unix_now = get_CurrentUnix();
+
+
+bool is_TimeToReadSensor(size_t config_id) {
+    SensorConfig_s& p = sensor_configs[config_id];
+
+    DBG_PRINTF("check is_TimeToReadSensor: %s\n", p.sensor_name);
+
+    if (!p.is_active) {
+        DBG_PRINTLN("Sensor is inactive!");
+        return false;
+    }
+
+    return (p.next_sensor_read <= get_CurrentUnix());
+}
+
+int64_t get_NextSensorReadFor(const char* sensor_name) {
+    int64_t  unix_now    = get_CurrentUnix();
     uint16_t min_current = get_CurrentMinutes();
 
     for (size_t i = 0; i < SENSOR_CONFIG_COUNT; i++) {
         SensorConfig_s &p = sensor_configs[i];
+
         if (!p.is_active) continue;
         if (strcmp(p.sensor_name, sensor_name) != 0) continue;
-        if (p.next_sensor_read > unix_now)
-            return p.next_sensor_read;
+
+        int64_t last_read = p.last_sensor_read > 0 && p.last_sensor_read < unix_now
+                          ? p.last_sensor_read
+                          : unix_now;
 
         int64_t next_time = INT64_MAX;
 
@@ -388,13 +406,12 @@ int64_t getAndSet_NextSensorReadFor(const char* sensor_name) {
                 if ((uint32_t)delta < best_delta_min)
                     best_delta_min = delta;
             }
-            next_time = unix_now + (int64_t)best_delta_min * 60;
+            next_time = (unix_now - (unix_now % 60)) + (int64_t)best_delta_min * 60;
         } else if (p.delay_ms > 0) {
-            next_time = p.last_sensor_read + (p.delay_ms / 1000);
+            next_time = last_read + (p.delay_ms / 1000);
         }
 
         if (next_time != INT64_MAX) {
-            p.next_sensor_read = next_time;
             return next_time;
         }
     }
@@ -402,23 +419,18 @@ int64_t getAndSet_NextSensorReadFor(const char* sensor_name) {
     return -1;
 }
 
-int64_t getAndSet_NextSensorReadsUnixTime() {
+int64_t get_NextSensorReadsUnixTime() {
     int64_t unix_now = get_CurrentUnix();
     int64_t unix_next = INT64_MAX;
-    unsigned long start = millis();
 
     for (size_t i = 0; i < SENSOR_CONFIG_COUNT; i++) {
         SensorConfig_s &p = sensor_configs[i];
         if (!p.is_active) continue;
 
-        int64_t next_time = getAndSet_NextSensorReadFor(p.sensor_name);
-        if (next_time > 0) {
-            // optional: compensate for function execution time
-            next_time -= (int64_t)((millis() - start) / 1000);
+        int64_t next_time = get_NextSensorReadFor(p.sensor_name);
 
-            if (next_time < unix_next)
-                unix_next = next_time;
-        }
+        if (next_time > 0 && next_time < unix_next)
+            unix_next = next_time;
     }
 
     return (unix_next == INT64_MAX) ? -1 : unix_next;
@@ -452,6 +464,7 @@ void sleep_UntilNextSend(int64_t next_read) {
                sleep_time_ms, prep_time_ms);
     DBG_PRINTF("######################\n\n");
 
+    save_SensorConfigs();
     esp_sleep_enable_timer_wakeup((uint64_t)sleep_time_ms * 1000ULL);
     esp_deep_sleep_start();
 }
@@ -478,6 +491,7 @@ void on_DataRecvSensorConfig(const esp_now_recv_info* info, const uint8_t* data,
 }
 
 bool request_SensorConfig(const char* sensor_name) {
+    DBG_PRINTF("Getting sensor config for: %s\n", sensor_name);
     const uint8_t* server_mac = PEER_SERVER;
 
     sensor_config_received = false;
@@ -590,8 +604,12 @@ bool request_SensorConfig(const char* sensor_name) {
          }
     }
 
-    cfg->next_sensor_read = 0;
-    getAndSet_NextSensorReadFor(cfg->sensor_name);
+    if (cfg->last_sensor_read == 0) {
+        DBG_PRINTLN("RESETTING last_sensor_read");
+        cfg->last_sensor_read = get_CurrentUnix();
+    }
+
+    cfg->next_sensor_read = get_NextSensorReadFor(cfg->sensor_name);
     config_request_attempts = 0;
     return true;
 }
@@ -645,6 +663,9 @@ void get_AllSensorConfigs() {
             sizeof(g_pending_sensor_configs[0]) - 1
         );
 
+        // reset last_sensor_read
+        sensor_configs[i].last_sensor_read = get_CurrentUnix();
+
         g_pending_sensor_configs[g_pending_sensor_configs_count][sizeof(g_pending_sensor_configs[0]) - 1] = '\0';
 
         ++g_pending_sensor_configs_count;
@@ -660,19 +681,6 @@ void get_AllSensorConfigs() {
 // Send Data to peers
 // ----------------------------------------------------------------------------
 
-bool is_TimeToReadSensor(size_t config_id) {
-    SensorConfig_s& p = sensor_configs[config_id];
-
-    DBG_PRINTF("check is_TimeToReadSensor: %s\n", p.sensor_name);
-
-    if (!p.is_active) {
-        DBG_PRINTLN("Sensor is inactive!");
-        return false;
-    }
-
-    return (p.next_sensor_read <= get_CurrentUnix());
-}
-
 
 void send_DataToPeer(size_t config_id, uint32_t value) {
     char message[64];
diff --git a/client_sensor/ClientSensor.h b/client_sensor/ClientSensor.h
index b37918f..5592260 100644
--- a/client_sensor/ClientSensor.h
+++ b/client_sensor/ClientSensor.h
@@ -50,7 +50,8 @@ void setup_Timezone();
 // ----------------------------------------------------------------------------
 // Deep sleep scheduler
 // ----------------------------------------------------------------------------
-int64_t getAndSet_NextSensorReadsUnixTime();
+int64_t get_NextSensorReadFor(const char* sensor_name);
+int64_t get_NextSensorReadsUnixTime();
 void    sleep_UntilNextSend(int64_t next_read);
 
 // ----------------------------------------------------------------------------
diff --git a/client_sensor/client_sensor.ino b/client_sensor/client_sensor.ino
index e126ab2..2af5581 100644
--- a/client_sensor/client_sensor.ino
+++ b/client_sensor/client_sensor.ino
@@ -43,8 +43,17 @@ void setup() {
 void loop() {
     DBG_PRINTF("## Unix time: %lld\n", get_CurrentUnix());
     for (size_t config_id = 0; config_id < SENSOR_CONFIG_COUNT; config_id++) {
-        print_SensorConfig(sensor_configs[config_id]);
+        DBG_PRINTLN("START");
+        SensorConfig_s &peer = sensor_configs[config_id];
+        print_SensorConfig(peer);
+        DBG_PRINTLN("END");
+    }
+    for (size_t config_id = 0; config_id < SENSOR_CONFIG_COUNT; config_id++) {
+        SensorConfig_s &peer = sensor_configs[config_id];
+        DBG_PRINTF("LOOP checking config: %s\n", peer.sensor_name);
+        print_SensorConfig(peer);
         if ( is_TimeToReadSensor(config_id) ) {
+            DBG_PRINTLN("ITS TIME FOR THIS SENSOR!");
             /*
              * read sensor value
              * default sensor configurations can be found in globals.cpp sensor_configs
@@ -53,12 +62,15 @@ void loop() {
             uint32_t value_to_send = fake_value_to_send;
 
             // set last read time
-            sensor_configs[config_id].last_sensor_read = get_CurrentUnix();
+            peer.last_sensor_read = get_CurrentUnix();
+            peer.next_sensor_read = 
+                    get_NextSensorReadFor(sensor_configs[config_id].sensor_name);
 
             // send to server and configured peer (e.g. pump)
             send_DataToPeer(config_id, value_to_send);
             fake_value_to_send++;
         }
+        print_SensorConfig(peer);
     }
 
     if (g_pending_sensor_configs_count > 0)
@@ -71,7 +83,7 @@ void loop() {
      */
 
 
-    int64_t next_read = getAndSet_NextSensorReadsUnixTime();
+    int64_t next_read = get_NextSensorReadsUnixTime();
 
     sleep_UntilNextSend(next_read);
 }