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

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 mac address stuff

Christian Immanuel · 2025-10-26 13:00 · e009777611b21ee50e88153a209ca17c05320c58

 client_sensor/ClientSensor.h    |  4 +-
 client_sensor/client_sensor.ino |  8 ++--
 globals.cpp                     | 15 ++-----
 globals.h                       | 11 +----
 post_server/WiFiManager.cpp     |  3 +-
 server/Server.cpp               | 96 +++++------------------------------------
 server/Server.h                 |  6 ---
 server/server.ino               |  8 +++-
 8 files changed, 29 insertions(+), 122 deletions(-)

diff --git a/client_sensor/ClientSensor.h b/client_sensor/ClientSensor.h
index 40a212e..a874281 100644
--- a/client_sensor/ClientSensor.h
+++ b/client_sensor/ClientSensor.h
@@ -65,8 +65,8 @@ void process_PendingSensorConfigs();
 // Send Data to peers
 // ----------------------------------------------------------------------------
 
-bool is_TimeToReadSensor(SensorConfig_s& p);
-void send_DataToPeer(SensorConfig_s& p, uint32_t value);
+bool is_TimeToReadSensor(size_t config_id);
+void send_DataToPeer(size_t config_id, uint32_t value);
 void send_NextSensorReadToPeer(const uint8_t* mac, int64_t unix_time);
 
 
diff --git a/client_sensor/client_sensor.ino b/client_sensor/client_sensor.ino
index 8c82f27..d2269d6 100644
--- a/client_sensor/client_sensor.ino
+++ b/client_sensor/client_sensor.ino
@@ -16,12 +16,12 @@ void setup() {
     init_WiFi();
     init_EspNow();
 
-    restore_SensorConfigs();
-
     DBG_PRINTLN("Requesting time sync...");
     if (request_TimeSync()) DBG_PRINTLN("done");
     else DBG_PRINTLN("failed (timeout)");
 
+    restore_SensorConfigs();
+
     DBG_PRINTF("----------------------\n");
 
     // Add all known configs to an array so we can request thoose from server
@@ -46,7 +46,7 @@ void loop() {
         SensorConfig_s &peer = sensor_configs[config_id];
         DBG_PRINTF("LOOP checking config: %s\n", peer.sensor_name);
         print_SensorConfig(peer);
-        if ( is_TimeToReadSensor(peer) ) {
+        if ( is_TimeToReadSensor(config_id) ) {
             DBG_PRINTLN("ITS TIME FOR THIS SENSOR!");
             /*
              * read sensor value
@@ -61,7 +61,7 @@ void loop() {
                     get_NextSensorReadFor(sensor_configs[config_id].sensor_name);
 
             // send to server and configured peer (e.g. pump)
-            send_DataToPeer(peer, value_to_send);
+            send_DataToPeer(config_id, value_to_send);
             fake_value_to_send++;
         }
         print_SensorConfig(peer);
diff --git a/globals.cpp b/globals.cpp
index 05b4328..8e1e5b6 100644
--- a/globals.cpp
+++ b/globals.cpp
@@ -19,19 +19,12 @@ bool g_received_ok_from_server = false;
 const uint32_t g_min_sleep_time = 60000;
 RTC_DATA_ATTR bool all_init_configs_received = false;
 
-//constexpr uint8_t PEER_SERVER[] = {0x58,0xcf,0x79,0x1e,0xed,0x18};
-constexpr
-uint8_t PEER_SERVER[]  = {0x54,0xca,0x29,0x1f,0x3d,0xa8};
-uint8_t PEER_PUMP[6]   = {0x51,0xc1,0x79,0x1e,0xed,0x18};
-uint8_t PEER_SENSOR[6] = {0xcc,0x8d,0xa2,0x69,0xf9,0x0c};
-
-bool init_sensor_mac_received = false;
-bool init_pump_mac_received   = false;
-
+constexpr uint8_t PEER_SERVER[6] = {0x55,0xde,0x67,0x2a,0x1e,0x29};
+constexpr uint8_t PEER_PUMP[6]   = {0x55,0xc1,0x79,0x1e,0xed,0x18};
 
 RTC_DATA_ATTR SensorConfig_s sensor_configs[] = {
-    {"moisture_1", true, 11, 10, "pump_1", 0, 0, {0},  90000, true,  {465, 466}, 2, 0, 0},
-    {"moisture_2", true, 11, 10, "pump_2", 0, 0, {0}, 120000, false, {0}, 0, 0, 0}
+    {"moisture_1", true, 11, 10, "pump_1", 0, 0, PEER_PUMP,  90000, true,  {465, 466}, 2, 0, 0},
+    {"moisture_2", true, 11, 10, "pump_2", 0, 0, PEER_PUMP, 120000, false, {0}, 0, 0, 0}
 };
 
 uint8_t config_request_attempts_max = 3;
diff --git a/globals.h b/globals.h
index a734ef8..f63fe13 100644
--- a/globals.h
+++ b/globals.h
@@ -48,7 +48,6 @@ struct SensorConfigServer_s {
     char notification_name[32];
     int  pumping_delay_sec;
     int  pumping_duration;
-    char pump_mac[18];       // "AA:BB:CC:DD:EE:FF"
     int  delay_ms;
     bool use_time;
     uint16_t times[8];
@@ -63,7 +62,7 @@ struct SensorConfig_s {
     char notification_name[32];
     int  pumping_delay_sec;
     int  pumping_duration;
-    uint8_t pump_mac[6];       // {0x51,0xc1,0x79,0x1e,0xed,0x18}
+    const uint8_t* pump_mac;       // {0x51,0xc1,0x79,0x1e,0xed,0x18}
     int  delay_ms;
     bool use_time;
     uint16_t times[8];
@@ -99,14 +98,8 @@ extern bool    g_received_ok_from_server;
 
 extern const uint32_t g_min_sleep_time;
 extern RTC_DATA_ATTR bool all_init_configs_received;
-
 extern const uint8_t PEER_SERVER[];
-extern uint8_t PEER_PUMP[];
-extern uint8_t PEER_SENSOR[];
-
-extern bool init_sensor_mac_received;
-extern bool init_pump_mac_received;
-
+extern const uint8_t PEER_PUMP[];
 extern RTC_DATA_ATTR SensorConfig_s sensor_configs[];
 extern uint8_t config_request_attempts_max;
 
diff --git a/post_server/WiFiManager.cpp b/post_server/WiFiManager.cpp
index 3d0489b..c29fd48 100644
--- a/post_server/WiFiManager.cpp
+++ b/post_server/WiFiManager.cpp
@@ -132,7 +132,6 @@ bool WIFI_GetSensorConfigServer(const char* sensor_name, SensorConfigServer_s &c
     cfg.is_active    = extractBool("is_active");
 
     extractString("notification_name", cfg.notification_name, sizeof(cfg.notification_name));
-    //extractString("pump_mac", cfg.pump_mac, sizeof(cfg.pump_mac));
     cfg.pumping_delay_sec = extractInt("pumping_delay_sec");
     cfg.pumping_duration  = extractInt("pumping_duration");
 
@@ -164,7 +163,7 @@ bool WIFI_GetSensorConfigServer(const char* sensor_name, SensorConfigServer_s &c
 
     DBG_PRINTF("Parsed: success=%d pin=%d min=%d delay=%d use_time=%d time_count=%u\n",
                cfg.success, cfg.pin, cfg.min_moisture, cfg.delay_ms, cfg.use_time, cfg.time_count);
-    DBG_PRINTF("sensor_name='%s' notification='%s' pumping_delay_sec='%d' pumping_duration='%d'\n",
+    DBG_PRINTF("        sensor_name='%s' notification='%s' pumping_delay_sec='%d' pumping_duration='%d'\n",
                cfg.sensor_name, cfg.notification_name, cfg.pumping_delay_sec, cfg.pumping_duration);
 
     return cfg.success;
diff --git a/server/Server.cpp b/server/Server.cpp
index 2dcd39e..30e5952 100644
--- a/server/Server.cpp
+++ b/server/Server.cpp
@@ -3,8 +3,6 @@
 #include "WiFiManager.h"
 
 #include <esp_now.h>
-#include "esp_wifi.h"
-
 
 
 uint8_t received_from_mac_addr[6];
@@ -20,7 +18,6 @@ void print_SensorConfigServer(const SensorConfigServer_s& cfg) {
     DBG_PRINTF("Pin: %d\n", cfg.pin);
     DBG_PRINTF("Use Time: %d\n", cfg.use_time);
     DBG_PRINTF("Delay: %d\n", cfg.delay_ms);
-    DBG_PRINTF("Notification MAC: %s\n", cfg.pump_mac);
     DBG_PRINTF("Notification Name: %s\n", cfg.notification_name);
     DBG_PRINTF("Min Moisture: %d\n", cfg.min_moisture);
     DBG_PRINTF("Active: %d\n", cfg.is_active);
@@ -36,54 +33,9 @@ void print_SensorConfigServer(const SensorConfigServer_s& cfg) {
     DBG_PRINTLN("----------------------------");
 }
 
-void copy_Mac(const uint8_t* src, uint8_t* dest) {
-    memcpy(dest, src, 6);
-}
-
-// ----------------------------------------------------------------------------
-// Save/Restore to flash NVS
-// ----------------------------------------------------------------------------
-
-#include <Preferences.h>
-
-Preferences prefs;
-
-void save_ToFlash() {
-    DBG_PRINTLN("Saving configs to NVS...");
-
-    prefs.begin("peers", false);
-    prefs.putBytes("pump_mac", PEER_PUMP, 6);
-    prefs.putBytes("sensor_mac", PEER_SENSOR, 6);
-    prefs.putBool("sensor_received", init_sensor_mac_received);
-    prefs.putBool("pump_received", init_pump_mac_received);
-    prefs.end();
-
-    DBG_PRINTLN("Save complete.");
-}
-
-void restore_FromFlash() {
-    DBG_PRINTLN("Restoring configs from NVS...");
-
-    prefs.begin("peers", true);
-
-    size_t pump_len   = prefs.getBytes("pump_mac", PEER_PUMP, 6);
-    size_t sensor_len = prefs.getBytes("sensor_mac", PEER_SENSOR, 6);
-
-    init_sensor_mac_received = prefs.getBool("sensor_received", false);
-    init_pump_mac_received   = prefs.getBool("pump_received", false);
-
-    prefs.end();
-
-    if (pump_len == 6 && sensor_len == 6) {
-        DBG_PRINTLN("Peer MACs restored from flash.");
-    } else {
-        DBG_PRINTLN("No stored peer MACs found, using defaults.");
-    }
-}
-
 // ----------------------------------------------------------------------------
 // Server handling (g_buffer)
-// ----------------------------------------------------------------------------
+ // ----------------------------------------------------------------------------
 
 void add_ReadingCallback(const char* table, const char* value, const int64_t time) {
     if (g_buffer_count >= BUFFER_SIZE) {
@@ -105,9 +57,10 @@ void init_Server() {
 // WiFi handling
 // ----------------------------------------------------------------------------
 
+#include "esp_wifi.h"
+
 void init_WiFi() {
     WiFi.mode(WIFI_MODE_APSTA);
-    esp_wifi_set_mac(WIFI_IF_STA, PEER_SERVER);
 
     WiFi.begin(ssid, password);
 
@@ -183,51 +136,22 @@ void on_DataRecv(const esp_now_recv_info *info, const uint8_t *data, int len) {
                received_from_mac_addr[0], received_from_mac_addr[1], received_from_mac_addr[2],
                received_from_mac_addr[3], received_from_mac_addr[4], received_from_mac_addr[5], msg);
 
-    if (strncmp(msg, "time?", 5) == 0) {
+    if (strcmp(msg, "time?") == 0) {
         char time_msg[32];
         snprintf(time_msg, sizeof(time_msg), "time:%lld", now());
-
-        // Set senders MAC address if not already set
-        if (!init_sensor_mac_received && strcmp(msg, "time?sensor") == 0) {
-            copy_Mac(received_from_mac_addr, PEER_SENSOR);
-            init_sensor_mac_received = true;
-            save_ToFlash();
-        } else if (!init_pump_mac_received && strcmp(msg, "time?pump") == 0) {
-            copy_Mac(received_from_mac_addr, PEER_PUMP);
-            init_pump_mac_received = true;
-            save_ToFlash();
-        }
-
-        esp_now_send(received_from_mac_addr, (uint8_t*)time_msg, strlen(time_msg));
-        return;
-    }
-    if (strcmp(msg, "time_sensor?") == 0) {
-        char time_msg[32];
-        snprintf(time_msg, sizeof(time_msg), "time:%lld", now());
-        copy_Mac(received_from_mac_addr, PEER_SENSOR);
         esp_now_send(received_from_mac_addr, (uint8_t*)time_msg, strlen(time_msg));
         return;
     }
 
+    // Queue config request instead of calling HTTP directly
     if (strncmp(msg, "next_read:", 10) == 0) {
-        const char* value_str = msg + 10;
-        char* sep = strchr(value_str, ':');
-        if (sep != nullptr && strncmp(sep, ":reached_pump:", 14) == 0) {
-            *sep = '\0';
-            next_connection_unix = atoll(value_str);
-
-            const char* reached_pump_str = sep + 14;
-            int reached_pump = atoi(reached_pump_str);
 
-            init_pump_mac_received = (reached_pump != 0);
-
-            if (!init_pump_mac_received)
-                save_ToFlash();
+        const char* value_str = msg + 10;
+        next_connection_unix = atoll(value_str);
+        go_to_sleep = true;
+        DBG_PRINTF("Received next_read: %lld\n", next_connection_unix);
+        return;
 
-            go_to_sleep = true;
-            DBG_PRINTF("Received next_read: %lld, reached_pump: %d\n", next_connection_unix, reached_pump);
-            return;
-        }
     } else if (strncmp(msg, "get_config:", 11) == 0 && !pending_config.active) {
 
         pending_config.active = true;
diff --git a/server/Server.h b/server/Server.h
index 02483dd..a17c1be 100644
--- a/server/Server.h
+++ b/server/Server.h
@@ -23,12 +23,6 @@ extern PendingConfigRequest_s pending_config;
 
 void print_SensorConfigServer(const SensorConfigServer_s& cfg);
 
-// ----------------------------------------------------------------------------
-// Save/Restore to flash NVS
-// ----------------------------------------------------------------------------
-
-void restore_FromFlash();
-
 // ----------------------------------------------------------------------------
 // Server handling (g_buffer)
 // ----------------------------------------------------------------------------
diff --git a/server/server.ino b/server/server.ino
index 39c53e3..c2880ad 100644
--- a/server/server.ino
+++ b/server/server.ino
@@ -1,6 +1,10 @@
 #include "Server.h"
 #include "WiFiManager.h"
 
+#include "esp_system.h"
+extern "C" esp_err_t esp_base_mac_addr_set(const uint8_t mac[6]);
+
+
 int64_t next_connection_unix = 0;
 bool go_to_sleep = false;
 
@@ -12,7 +16,7 @@ void setup() {
 
     DBG_BEGIN();
 
-    restore_FromFlash();
+    esp_base_mac_addr_set(PEER_SERVER);
 
     init_WiFi();
     WIFI_SyncTime();
@@ -43,7 +47,7 @@ void loop() {
     if (g_received_ok_from_server)
         send_ConfigToEsp();
 
-    if (go_to_sleep && init_sensor_mac_received)
+    if (go_to_sleep)
         sleep_UntilNextReceive();
 }