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

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

Cleanin time to int64, lastSent into sensor config

Christian Immanuel · 2025-10-23 10:47 · b36b3c6e4a5fc7996008fb709cf3034462ef013d

 client_sensor/ClientSensor.cpp  | 82 +++++++++++++++++++++--------------------
 client_sensor/ClientSensor.h    |  1 +
 client_sensor/client_sensor.ino | 14 +++++--
 globals.cpp                     |  6 +--
 globals.h                       |  7 ++--
 post_server/WiFiManager.cpp     |  6 +--
 post_server/WiFiManager.h       |  2 +-
 server/server.ino               |  2 +-
 8 files changed, 67 insertions(+), 53 deletions(-)

diff --git a/client_sensor/ClientSensor.cpp b/client_sensor/ClientSensor.cpp
index 99e59ac..5eedcea 100644
--- a/client_sensor/ClientSensor.cpp
+++ b/client_sensor/ClientSensor.cpp
@@ -59,6 +59,7 @@ void printSensorConfig(const SensorConfig_s& cfg) {
     DBG_PRINTF("  delay_ms: %d\n", cfg.delay_ms);
     DBG_PRINTF("  use_time: %s\n", cfg.use_time ? "true" : "false");
     DBG_PRINTF("  time_count: %u\n", cfg.time_count);
+    DBG_PRINTF("  last_sensor_read: %llu\n", cfg.last_sensor_read);
 
     if (cfg.time_count > 0) {
         DBG_PRINT("  times: ");
@@ -121,11 +122,6 @@ void restore_SensorConfigs() {
 }
 
 
-// ----------------------------------------------------------------------------
-// RTC variables
-// ----------------------------------------------------------------------------
-RTC_DATA_ATTR uint64_t last_sent_array[SENSOR_CONFIG_COUNT] = {0};
-
 // ----------------------------------------------------------------------------
 // ESP-NOW handling
 // ----------------------------------------------------------------------------
@@ -153,7 +149,7 @@ void on_DataRecv(const esp_now_recv_info *info, const uint8_t *data, int len) {
     DBG_PRINTF("Received from server: %s\n", msg);
 
     if (strncmp(msg, "time:", 5) == 0) {
-        time_t server_unix = strtoul(msg + 5, nullptr, 10);
+        int64_t server_unix = strtoll(msg + 5, nullptr, 10);
         struct timeval tv = { .tv_sec = server_unix, .tv_usec = 0 };
         settimeofday(&tv, nullptr);
         rtc_synced_with_server = true;
@@ -340,8 +336,9 @@ void sleep_UntilNextSend() {
                 if (delta_ms < min_delay_ms) min_delay_ms = delta_ms;
             }
         } else {
-            uint32_t delta_ms = (p.delay_ms > (ms_now - last_sent_array[i]))
-                ? (p.delay_ms - (ms_now - last_sent_array[i]))
+            uint64_t last_read = p.last_sensor_read * 1000ULL;
+            uint32_t delta_ms = (p.delay_ms > (ms_now - last_read))
+                ? (p.delay_ms - (ms_now - last_read))
                 : 0;
             if (delta_ms < min_delay_ms) min_delay_ms = delta_ms;
         }
@@ -550,60 +547,67 @@ void get_AllSensorConfigs() {
 // Send Data to peers
 // ----------------------------------------------------------------------------
 
-void send_DataToPeer(size_t config_id, uint32_t value) {
+bool is_TimeToReadSensor(size_t config_id) {
     uint64_t ms_now = get_CurrentUnixMs();
 
     SensorConfig_s& p = sensor_configs[config_id];
 
-    DBG_PRINTF("SENSOR: %s\n", p.sensor_name);
+    DBG_PRINTF("check is_TimeToReadSensor: %s\n", p.sensor_name);
     printSensorConfig(p);
+
     if (!p.is_active) { 
         DBG_PRINTLN("Sensor is inactive!");
-        return;
+        return false;
     }
 
-    bool should_send = false;
-
     if (p.use_time) {
         uint16_t min_current = get_CurrentMinutes();
         for (size_t j = 0; j < p.time_count; j++) {
             if (min_current == p.times[j] &&
-                    ms_now - last_sent_array[config_id] > 60000) {
-                should_send = true;
+                    ms_now - (p.last_sensor_read * 1000ULL) > 60000) {
+                return true;
                 break;
             }
         }
     } else {
-        if (ms_now - last_sent_array[config_id] >= p.delay_ms) should_send = true;
+        //if (ms_now - last_sent_array[config_id] >= p.delay_ms) is_time_to_read = true;
+        if (ms_now - (p.last_sensor_read * 1000ULL) >= p.delay_ms)
+            return true;
     }
+    return false;
+}
 
-    if (should_send) {
-        char message[32];
-        snprintf(message, sizeof(message), "%s:%u", p.sensor_name, value);
+void send_DataToPeer(size_t config_id, uint32_t value) {
+    char message[32];
 
-        DBG_PRINT("Sending to peer: ");
-        printMac(p.pump_mac);
-        send_ack_process_complete = false;
-        if (!send_And_wait_Ack(p.pump_mac, message)) {
-            DBG_PRINTLN("Delivery failed or peer offline!");
-            send_ack_process_complete = true;
-        } else {
-            last_sent_array[config_id] = get_CurrentUnixMs();
-            value++;
-        }
 
-        while (!send_ack_process_complete) delay(100);
+    SensorConfig_s& p = sensor_configs[config_id];
 
-        DBG_PRINT("Sending to peer: ");
-        printMac(PEER_SERVER);
-        send_ack_process_complete = false;
-        if (!send_And_wait_Ack(PEER_SERVER, message)) {
-            DBG_PRINTLN("Failed to get ack from server!");
-            send_ack_process_complete = true;
-        }
-        while (!send_ack_process_complete) delay(100);
+    DBG_PRINTF("send_DataToPeer: %s\n", p.sensor_name);
 
-        delay(5000);
+    snprintf(message, sizeof(message), "%s:%u", p.sensor_name, value);
+
+    DBG_PRINT("Sending to peer: ");
+    printMac(p.pump_mac);
+    send_ack_process_complete = false;
+    if (!send_And_wait_Ack(p.pump_mac, message)) {
+        DBG_PRINTLN("Delivery failed or peer offline!");
+        send_ack_process_complete = true;
+    } else {
+        value++; // we should remove this lateron!
     }
+
+    while (!send_ack_process_complete) delay(100);
+
+    DBG_PRINT("Sending to peer: ");
+    printMac(PEER_SERVER);
+    send_ack_process_complete = false;
+    if (!send_And_wait_Ack(PEER_SERVER, message)) {
+        DBG_PRINTLN("Failed to get ack from server!");
+        send_ack_process_complete = true;
+    }
+    while (!send_ack_process_complete) delay(100);
+
+    delay(5000);
 }
 
diff --git a/client_sensor/ClientSensor.h b/client_sensor/ClientSensor.h
index 7130a98..766ca8e 100644
--- a/client_sensor/ClientSensor.h
+++ b/client_sensor/ClientSensor.h
@@ -59,6 +59,7 @@ void process_PendingSensorConfigs();
 // Send Data to peers
 // ----------------------------------------------------------------------------
 
+bool is_TimeToReadSensor(size_t config_id);
 void send_DataToPeer(size_t config_id, uint32_t value);
 
 
diff --git a/client_sensor/client_sensor.ino b/client_sensor/client_sensor.ino
index 4155e53..697a77b 100644
--- a/client_sensor/client_sensor.ino
+++ b/client_sensor/client_sensor.ino
@@ -3,7 +3,7 @@
 
 // this is going to be a sensor read value!
 // RTC_DATA_ATTR is not required than!
-RTC_DATA_ATTR uint32_t value_to_send = 0;
+RTC_DATA_ATTR uint32_t fake_value_to_send = 0;
 
 // ----------------------------------------------------------------------------
 // Setup
@@ -37,9 +37,17 @@ void setup() {
 void loop() {
     ensure_Peers();
 
-    DBG_PRINTF("## Unix time: %llu\n", (unsigned long long)get_CurrentUnixMs() / 1000);
+    DBG_PRINTF("## Unix time: %lldl\n", (long long)get_CurrentUnixMs() / 1000);
     for (size_t config_id = 0; config_id < SENSOR_CONFIG_COUNT; config_id++) {
-        send_DataToPeer(config_id, value_to_send);
+        if ( is_TimeToReadSensor(config_id) ) {
+            // read sensor value
+            // default sensor configurations can be found in globals.cpp sensor_configs
+            // the struct SensorConfig_s is defined in globals.h
+            uint32_t value_to_send = fake_value_to_send;
+
+            sensor_configs[config_id].last_sensor_read = (long long)get_CurrentUnixMs() / 1000;
+            send_DataToPeer(config_id, value_to_send);
+        }
     }
 
     if (g_pending_sensor_configs_count > 0)
diff --git a/globals.cpp b/globals.cpp
index 034bf82..8e94976 100644
--- a/globals.cpp
+++ b/globals.cpp
@@ -1,7 +1,7 @@
 #include "globals.h"
 
 
-uint32_t g_time_now = 0;
+int64_t g_time_now = 0;
 SensorEntry g_buffer[BUFFER_SIZE]; 
 uint8_t g_buffer_count = 0;
 char g_pending_sensor_configs[4][32] = {};
@@ -21,8 +21,8 @@ constexpr uint8_t PEER_SERVER[] = {0x58,0xcf,0x79,0x1e,0xed,0x18};
 constexpr uint8_t PEER_PUMP[]   = {0x51,0xc1,0x79,0x1e,0xed,0x18};
 
 RTC_DATA_ATTR SensorConfig_s sensor_configs[] = {
-    {"moisture_1", true, 11, 10, 0, "pump_1", {0},  90000, true,  {465, 466}, 2},
-    {"moisture_2", true, 11, 10, 0, "pump_2", {0}, 120000, false, {0}, 0}
+    {"moisture_1", true, 11, 10, 0, "pump_1", {0},  90000, true,  {465, 466}, 2, 0},
+    {"moisture_2", true, 11, 10, 0, "pump_2", {0}, 120000, false, {0}, 0, 0}
 };
 
 uint8_t config_request_attempts_max = 3;
diff --git a/globals.h b/globals.h
index 2c13207..9ecb143 100644
--- a/globals.h
+++ b/globals.h
@@ -61,16 +61,17 @@ struct SensorConfig_s {
     bool use_time;
     uint16_t times[8];
     uint8_t  time_count;
+    int64_t last_sensor_read; // Unix time
 };
 
 struct SensorEntry {
     char table[TABLE_SIZE];
     char value[VALUE_SIZE];
-    uint64_t time;
+    int64_t time;
 };
 
 typedef struct struct_message_server {
-    uint64_t time;
+    int64_t time;
 } struct_message_server;
 
 typedef struct struct_message_client {
@@ -79,7 +80,7 @@ typedef struct struct_message_client {
 
 extern SensorEntry g_buffer[BUFFER_SIZE];
 extern uint8_t  g_buffer_count;
-extern uint32_t g_time_now;
+extern int64_t g_time_now;
 extern char g_pending_sensor_configs[4][32];
 extern uint8_t g_pending_sensor_configs_count;
 extern bool g_received_ok_from_server;
diff --git a/post_server/WiFiManager.cpp b/post_server/WiFiManager.cpp
index 4cb4b42..b99c052 100644
--- a/post_server/WiFiManager.cpp
+++ b/post_server/WiFiManager.cpp
@@ -169,8 +169,8 @@ bool WIFI_GetSensorConfigServer(const char* sensor_name, SensorConfigServer_s &c
     return cfg.success;
 }
 
-uint64_t now() {
-    return (uint64_t)time(nullptr);
+int64_t now() {
+    return (int64_t)time(nullptr);
 }
 
 void WIFI_Try(uint8_t max_attempts) {
@@ -198,7 +198,7 @@ void WIFI_Try(uint8_t max_attempts) {
 void WIFI_SyncTime() {
     DBG_PRINTLN("Syncing time...");
     configTime(GMT_OFFSET_SEC, DAYLIGHT_OFFSET_SEC, NTP_SERVER);
-    uint64_t t = now();
+    int64_t t = now();
     uint8_t tries = 0;
     while (t < 1600000000 && tries++ < 20) { delay(500); t = time(nullptr); }
     if (t < 1600000000) DBG_PRINTLN("NTP sync failed!");
diff --git a/post_server/WiFiManager.h b/post_server/WiFiManager.h
index 7ba92e5..4883f55 100644
--- a/post_server/WiFiManager.h
+++ b/post_server/WiFiManager.h
@@ -10,7 +10,7 @@ bool WIFI_GetSensorConfigServer(const char* sensor_name, SensorConfigServer_s &c
 using  WIFI_ReadingCallback = void(*)(const char* table, const char* value);
 extern WIFI_ReadingCallback WIFI_OnAddReading;
 
-uint64_t now();
+int64_t now();
 void WIFI_Try(uint8_t max_attempts = 20);
 void WIFI_SyncTime();
 bool WIFI_PostBuffer();
diff --git a/server/server.ino b/server/server.ino
index 9dc9d16..5df82a0 100644
--- a/server/server.ino
+++ b/server/server.ino
@@ -120,7 +120,7 @@ void on_DataRecv(const esp_now_recv_info *info, const uint8_t *data, int len) {
 
     if (strcmp(msg, "time?") == 0) {
         char time_msg[32];
-        snprintf(time_msg, sizeof(time_msg), "time:%lu", now());
+        snprintf(time_msg, sizeof(time_msg), "time:%lld", now());
         esp_now_send(received_from_mac_addr, (uint8_t*)time_msg, strlen(time_msg));
         return;
     }