Nimbin[12]?Embedded / esp_garden / commits / 23880a6

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 double posting buffer

Chris esp · 2025-11-02 19:20 · 23880a6efc894bab9dc6b1660d38662e2ef9c7a1

 client_pump/main/globals.h          | 14 --------------
 client_sensor/main/ClientSensor.cpp | 30 ++++++++++++++++++------------
 client_sensor/main/main.cpp         |  1 -
 post_server/main/WiFiManager.cpp    | 26 ++++++++++++++++----------
 post_server/main/WiFiManager.h      |  2 +-
 post_server/main/main.cpp           |  2 +-
 server/main/main.cpp                |  2 +-
 7 files changed, 37 insertions(+), 40 deletions(-)

diff --git a/client_pump/main/globals.h b/client_pump/main/globals.h
index 2572ddf..894d085 100644
--- a/client_pump/main/globals.h
+++ b/client_pump/main/globals.h
@@ -7,20 +7,6 @@
 #include <stdint.h>
 #include <stdbool.h>
 
-#define DEBUG 1
-
-#if DEBUG
-#define DBG_TAG "APP"
-#define DBG_BEGIN()     ESP_LOGI(DBG_TAG, "Debug logging started")
-#define DBG_PRINT(x)    ESP_LOGI(DBG_TAG, "%s", x)
-#define DBG_PRINTLN(x)  ESP_LOGI(DBG_TAG, "%s\n", x)
-#define DBG_PRINTF(...) ESP_LOGI(DBG_TAG, __VA_ARGS__)
-#else
-#define DBG_BEGIN()
-#define DBG_PRINT(x)
-#define DBG_PRINTLN(x)
-#define DBG_PRINTF(...)
-#endif
 
 // Network Time
 #define NTP_SERVER "pool.ntp.org"
diff --git a/client_sensor/main/ClientSensor.cpp b/client_sensor/main/ClientSensor.cpp
index 0d05b72..77df38f 100644
--- a/client_sensor/main/ClientSensor.cpp
+++ b/client_sensor/main/ClientSensor.cpp
@@ -25,7 +25,7 @@ uint8_t config_request_attempts = 0;
 // ----------------------------------------------------------------------------
 void printMac(const uint8_t* mac) {
     if (!mac) return;
-    ESP_LOGI("APP", "%02X:%02X:%02X:%02X:%02X:%02X",
+    ESP_LOGI("         ", "%02X:%02X:%02X:%02X:%02X:%02X",
              mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
 }
 
@@ -47,7 +47,7 @@ int32_t get_WiFiChannel() {
 
     int32_t channel = 0;
     for (int i = 0; i < ap_count; i++) {
-        if (strcmp((char*)ap_list[i].ssid, ssid) == 0) {
+        if (strcmp((char*)ap_list[i].ssid, WIFI_SSID) == 0) {
             channel = ap_list[i].primary;
             break;
         }
@@ -108,9 +108,16 @@ void print_SensorConfig(const SensorConfig_s& cfg) {
     ESP_LOGI(TAG, "  next_sensor_read: %lld", cfg.next_sensor_read);
 
     if (cfg.time_count > 0) {
-        ESP_LOGI(TAG, "  times: ");
-        for (uint8_t i = 0; i < cfg.time_count; i++)
-            ESP_LOGI(TAG, "%u", cfg.times[i]);
+        char buf[128];
+        int offset = 0;
+
+        offset += snprintf(buf + offset, sizeof(buf) - offset, "  times: ");
+        for (uint8_t i = 0; i < cfg.time_count; i++) {
+            offset += snprintf(buf + offset, sizeof(buf) - offset, "%u ", cfg.times[i]);
+            if (offset >= sizeof(buf)) break;
+        }
+
+        ESP_LOGI(TAG, "%s", buf);
     }
 }
 
@@ -192,9 +199,7 @@ void on_DataSent(const wifi_tx_info_t* info, esp_now_send_status_t status) {
     static const char *TAG = "on_DataSent";
 
     ESP_LOGI(TAG, "Packet to ");
-    for (int i = 0; i < 6; i++) {
-        ESP_LOGI(TAG, "  :%02X", info->des_addr[i]);
-    }
+    printMac(info->des_addr);
 
     if (status == ESP_NOW_SEND_SUCCESS)
         ESP_LOGI(TAG, " queued successfully");
@@ -407,9 +412,8 @@ void init_EspNow() {
 bool send_Message(const uint8_t* peer, const char* message) {
     static const char *TAG = "send_Message";
 
-    ESP_LOGI(TAG, "send_Message: ");
+    ESP_LOGI(TAG, "send_Message to");
     printMac(peer);
-    ESP_LOGI(TAG, "Message:");
     ESP_LOGI(TAG, "  %s", message);
     if (!esp_now_is_peer_exist(peer)) {
         ESP_LOGI(TAG, "ERROR: peer doese not exist!");
@@ -797,8 +801,10 @@ void get_AllSensorConfigs() {
 
 
     process_PendingSensorConfigs();
-    if (g_pending_sensor_configs_count == 0)
+    if (g_pending_sensor_configs_count == 0) {
+        ESP_LOGI("get_AllSensorConfigs", "All init configs received");
         all_init_configs_received = true;
+    }
 }
 
 // ----------------------------------------------------------------------------
@@ -810,7 +816,7 @@ void send_DataToPumpAndServer(size_t config_id, uint32_t value, bool shall_pump)
     static const char *TAG = "send_DataToPumpAndServer";
 
     SensorConfig_s& p = sensor_configs[config_id];
-    ESP_LOGI(TAG, "send_DataToPumpAndServer: %s", p.sensor_name);
+    ESP_LOGI(TAG, "%s", p.sensor_name);
 
     int pumping_duration = shall_pump ? p.pumping_duration : 0;
 
diff --git a/client_sensor/main/main.cpp b/client_sensor/main/main.cpp
index d4acbcf..8f9fd6e 100644
--- a/client_sensor/main/main.cpp
+++ b/client_sensor/main/main.cpp
@@ -40,7 +40,6 @@ void sensor_loop(void*) {
                 send_DataToPumpAndServer(config_id, value_to_send, shall_pump);
                 fake_value_to_send++;
             }
-            print_SensorConfig(peer);
         }
 
         if (g_pending_sensor_configs_count > 0)
diff --git a/post_server/main/WiFiManager.cpp b/post_server/main/WiFiManager.cpp
index c60ab4b..37c4adf 100644
--- a/post_server/main/WiFiManager.cpp
+++ b/post_server/main/WiFiManager.cpp
@@ -156,6 +156,10 @@ esp_err_t http_event_handler(esp_http_client_event_t *evt) {
 
 void post_ToServer( const char* path, char* json, size_t len) {
     static const char *TAG = "post_ToServer";
+
+    ESP_LOGI(TAG, "Posting json:");
+    ESP_LOGI(TAG, "%s", json);
+
     // prepare HTTP client
     char url[256];
     snprintf(url, sizeof(url), "https://%s%s", server_host, path);
@@ -196,9 +200,11 @@ void post_ToServer( const char* path, char* json, size_t len) {
 
 }
 
-void WiFi_PostDataToServer(size_t buffer_len, SensorEntry_s buffer[BUFFER_SIZE]) {
+void WiFi_PostDataToServer() {
     static const char *TAG = "WiFi_PostDataToServer";
 
+    ESP_LOGI(TAG, "Buffer len: %zu", g_buffer_count);
+
     // allocate JSON buffer
     const size_t json_cap = JSON_BUFFER_SIZE;
     char *json = (char*)malloc(json_cap);
@@ -218,7 +224,7 @@ void WiFi_PostDataToServer(size_t buffer_len, SensorEntry_s buffer[BUFFER_SIZE])
     }
     len += (size_t)ret;
 
-    for (size_t i = 0; i < buffer_len; ++i) {
+    for (size_t i = 0; i < g_buffer_count; ++i) {
         // separator
         if (i != 0) {
             ret = snprintf(json + len, (len < json_cap) ? (json_cap - len) : 0, ",");
@@ -230,16 +236,16 @@ void WiFi_PostDataToServer(size_t buffer_len, SensorEntry_s buffer[BUFFER_SIZE])
             len += (size_t)ret;
         }
 
-        const char *table = buffer[i].table;
-        const char *value = buffer[i].value;
+        const char *table = g_buffer[i].table;
+        const char *value = g_buffer[i].value;
 
         // format object
         ret = snprintf(json + len, (len < json_cap) ? (json_cap - len) : 0,
                 "{\"table\":\"%s\",\"value\":\"%s\",\"time\":%" PRId64 ",\"pump_reached\":%d}",
                 table,
                 value,
-                buffer[i].time,
-                buffer[i].pump_reached);
+                g_buffer[i].time,
+                g_buffer[i].pump_reached);
         if (ret < 0) {
             free(json);
             g_received_ok_from_server = true;
@@ -265,6 +271,7 @@ void WiFi_PostDataToServer(size_t buffer_len, SensorEntry_s buffer[BUFFER_SIZE])
 
     post_ToServer("/insert.php", json, len);
 
+    g_buffer_count = 0;
 
     // parse pending_sensors from response
     g_pending_sensor_configs_count = 0;
@@ -319,19 +326,18 @@ void WiFi_PostDataToServer(size_t buffer_len, SensorEntry_s buffer[BUFFER_SIZE])
                         g_pending_sensor_configs_count++;
                     }
                 }
-                DBG_PRINTF("COUNTED: %d\n", g_pending_sensor_configs_count);
+                ESP_LOGI(TAG, "COUNTED: %d", g_pending_sensor_configs_count);
             }
         }
     }
 
     if (g_pending_sensor_configs_count > 0) {
-        DBG_PRINTF("NEW CONFIGS RECEIVED AT WIFIManager:\n");
+        ESP_LOGI(TAG, "NEW CONFIGS RECEIVED AT WIFIManager");
         for (size_t i = 0; i < g_pending_sensor_configs_count; i++) {
-            DBG_PRINTF("%s\n", g_pending_sensor_configs[i]);
+            ESP_LOGI(TAG, "  %s", g_pending_sensor_configs[i]);
         }
     }
 
-    // final housekeeping
     g_received_ok_from_server = true;
     free(json);
 }
diff --git a/post_server/main/WiFiManager.h b/post_server/main/WiFiManager.h
index 48e2518..e726972 100644
--- a/post_server/main/WiFiManager.h
+++ b/post_server/main/WiFiManager.h
@@ -17,7 +17,7 @@ esp_err_t WiFi_connect();
 esp_err_t WiFi_disconnect();
 
 void WIFI_SyncTime();
-void WiFi_PostDataToServer(size_t buffer_len, SensorEntry_s buffer[BUFFER_SIZE]);
+void WiFi_PostDataToServer();
 
 bool WIFI_GetSensorConfigServer(const char* sensor_name, SensorConfigServer_s &cfg);
 
diff --git a/post_server/main/main.cpp b/post_server/main/main.cpp
index 9d7ff04..88824eb 100644
--- a/post_server/main/main.cpp
+++ b/post_server/main/main.cpp
@@ -36,7 +36,7 @@ extern "C" void app_main() {
             g_buffer_count = 2;
 
             while (true) {
-                WiFi_PostDataToServer(g_buffer_count, g_buffer);
+                WiFi_PostDataToServer();
                 // Check stack size 
                 UBaseType_t free_stack = uxTaskGetStackHighWaterMark(NULL);
                 ESP_LOGI("wifi_task", "Stack remaining: %u bytes", (unsigned)free_stack);
diff --git a/server/main/main.cpp b/server/main/main.cpp
index 2968ad7..6a69859 100644
--- a/server/main/main.cpp
+++ b/server/main/main.cpp
@@ -17,7 +17,7 @@ extern "C" void server_loop(void*) {
             get_PendingConfigFromServer();
 
         if (g_buffer_count > 0)
-            WiFi_PostDataToServer(g_buffer_count, g_buffer);
+            WiFi_PostDataToServer();
 
         if (g_received_ok_from_server)
             send_ConfigToEsp();