Nimbin[12]?Embedded / esp_garden / commits / 0d834c5

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

Cleanup and MAC fix

Christian Immanuel · 2025-10-23 16:44 · 0d834c5a018e3f8ccc0d221edac3b1c10af1d600

 client_sensor/ClientSensor.cpp | 87 ++++++++++++++++++++++++++++++++++++------
 globals.h                      |  8 ++--
 2 files changed, 79 insertions(+), 16 deletions(-)

diff --git a/client_sensor/ClientSensor.cpp b/client_sensor/ClientSensor.cpp
index a24acaf..cb98842 100644
--- a/client_sensor/ClientSensor.cpp
+++ b/client_sensor/ClientSensor.cpp
@@ -215,18 +215,63 @@ void on_DataRecvAck(const esp_now_recv_info *info, const uint8_t *data, int len)
     send_ack_process_complete = true;
 }
 
-void add_Peer(const uint8_t* addr) {
+void print_AllPeers() {
+    esp_now_peer_num_t peerCount;
+    esp_err_t res = esp_now_get_peer_num(&peerCount);
+    if (res != ESP_OK) {
+        DBG_PRINTLN("Failed to get peer count");
+        return;
+    }
+
+    DBG_PRINTF("Total peers: %d\n", peerCount.total_num);
+
+    for (int i = 0; i < peerCount.total_num; i++) {
+        esp_now_peer_info_t peerInfo;
+        res = esp_now_fetch_peer(i, &peerInfo);
+        if (res == ESP_OK) {
+            DBG_PRINTF("Peer %d: ", i);
+            printMac(peerInfo.peer_addr);
+        } else {
+            DBG_PRINTF("Failed to fetch peer %d\n", i);
+        }
+    }
+}
+
+void add_Peer(const uint8_t* addr, int32_t channel) {
+    DBG_PRINT("add_Peer: ");
+    printMac(addr);
+    if (esp_now_is_peer_exist(addr)) {
+        DBG_PRINTLN("Peer already exists, skipping:");
+        printMac(addr);
+        return;
+    }
+
     esp_now_peer_info_t peer{};
     memcpy(peer.peer_addr, addr, 6);
     peer.ifidx = WIFI_IF_STA;
-    peer.channel = 0;
+    peer.channel = channel;
     peer.encrypt = false;
-    if (!esp_now_is_peer_exist(addr)) esp_now_add_peer(&peer);
+
+    esp_err_t res = esp_now_add_peer(&peer);
+    if (res == ESP_OK) {
+        DBG_PRINT("Added peer: ");
+        printMac(addr);
+    } else {
+        DBG_PRINT("Failed to add peer: ");
+        printMac(addr);
+        DBG_PRINTF(" (ESP_ERR: %d)\n", res);
+    }
 }
 
 void ensure_Peers() {
-    for (size_t i=0; i<SENSOR_CONFIG_COUNT; i++) add_Peer(sensor_configs[i].pump_mac);
-    add_Peer(PEER_SERVER);
+    int32_t channel = get_WiFiChannel();
+
+    for (size_t i=0; i<SENSOR_CONFIG_COUNT; i++)
+        add_Peer(sensor_configs[i].pump_mac, channel);
+
+    add_Peer(PEER_SERVER, channel);
+
+    print_AllPeers();
 }
 
 void init_EspNow() {
@@ -239,6 +284,8 @@ void init_EspNow() {
 }
 
 bool send_Message(const uint8_t* peer, const char* message) {
+    DBG_PRINTLN("send_Message: ");
+    printMac(peer);
     DBG_PRINTLN("Message:");
     DBG_PRINTF("  %s\n", message);
     if (!esp_now_is_peer_exist(peer)) {
@@ -246,10 +293,10 @@ bool send_Message(const uint8_t* peer, const char* message) {
         return false;
     }
     esp_err_t result = esp_now_send(peer, (uint8_t*)message, strlen(message));
-    return result == ESP_OK;
+    return result == ESP_NOW_SEND_SUCCESS;
 }
 
-bool send_And_wait_Ack(const uint8_t* peer, const char* message, uint32_t timeout_ms = 5000) {
+bool send_AndWaitAck(const uint8_t* peer, const char* message, uint32_t timeout_ms = 5000) {
     send_ack_process_complete = false;
     ack_received = false;
 
@@ -386,7 +433,6 @@ void on_DataRecvSensorConfig(const esp_now_recv_info* info, const uint8_t* data,
 
 bool request_SensorConfig(const char* sensor_name) {
     const uint8_t* server_mac = PEER_SERVER;
-    if (!esp_now_is_peer_exist(server_mac)) add_Peer(server_mac);
 
     sensor_config_received = false;
     memset(sensor_config_message, 0, sizeof(sensor_config_message));
@@ -465,8 +511,9 @@ bool request_SensorConfig(const char* sensor_name) {
     cfg->notification_delay_ms = atoi(tokens[4]);
     strncpy(cfg->notification_name, tokens[5], sizeof(cfg->notification_name)-1);
     cfg->notification_name[sizeof(cfg->notification_name)-1] = '\0';
-    strncpy((char*)cfg->pump_mac, tokens[6], sizeof(cfg->pump_mac)-1);
-    cfg->pump_mac[sizeof(cfg->pump_mac)-1] = '\0';
+    uint8_t new_mac[6];
+    sscanf(tokens[6], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
+                &new_mac[0], &new_mac[1], &new_mac[2], &new_mac[3], &new_mac[4], &new_mac[5]);
     cfg->delay_ms = atoi(tokens[7]);
     cfg->use_time = atoi(tokens[8]) != 0;
 
@@ -481,6 +528,22 @@ bool request_SensorConfig(const char* sensor_name) {
         }
     }
 
+    for (int i = 0; i < 6; i++) {
+         if (new_mac[i] != cfg->pump_mac[i]) {
+             DBG_PRINTLN("New mac address!");
+             printMac(new_mac);
+             printMac(cfg->pump_mac);
+             if (esp_now_is_peer_exist(cfg->pump_mac)) {
+                 if (esp_now_del_peer(cfg->pump_mac) != ESP_OK)
+                     DBG_PRINTLN("Failed to delete old peer!");
+             }
+             memcpy(cfg->pump_mac, new_mac, 6);
+             ensure_Peers();
+
+             break;
+         }
+    }
+
     config_request_attempts = 0;
     return true;
 }
@@ -592,7 +655,7 @@ void send_DataToPeer(size_t config_id, uint32_t value) {
     DBG_PRINT("Sending to peer: ");
     printMac(p.pump_mac);
     send_ack_process_complete = false;
-    if (!send_And_wait_Ack(p.pump_mac, message)) {
+    if (!send_AndWaitAck(p.pump_mac, message)) {
         DBG_PRINTLN("Delivery failed or peer offline!");
         send_ack_process_complete = true;
     } else {
@@ -604,7 +667,7 @@ void send_DataToPeer(size_t config_id, uint32_t value) {
     DBG_PRINT("Sending to peer: ");
     printMac(PEER_SERVER);
     send_ack_process_complete = false;
-    if (!send_And_wait_Ack(PEER_SERVER, message)) {
+    if (!send_AndWaitAck(PEER_SERVER, message)) {
         DBG_PRINTLN("Failed to get ack from server!");
         send_ack_process_complete = true;
     }
diff --git a/globals.h b/globals.h
index 9ecb143..0301c7c 100644
--- a/globals.h
+++ b/globals.h
@@ -56,7 +56,7 @@ struct SensorConfig_s {
     int  min_moisture;
     int  notification_delay_ms;
     char notification_name[64];
-    uint8_t pump_mac[18];       // {0x51,0xc1,0x79,0x1e,0xed,0x18}
+    uint8_t pump_mac[6];       // {0x51,0xc1,0x79,0x1e,0xed,0x18}
     int  delay_ms;
     bool use_time;
     uint16_t times[8];
@@ -79,11 +79,11 @@ typedef struct struct_message_client {
 } struct_message_client;
 
 extern SensorEntry g_buffer[BUFFER_SIZE];
-extern uint8_t  g_buffer_count;
+extern uint8_t g_buffer_count;
 extern int64_t g_time_now;
-extern char g_pending_sensor_configs[4][32];
+extern char    g_pending_sensor_configs[4][32];
 extern uint8_t g_pending_sensor_configs_count;
-extern bool g_received_ok_from_server;
+extern bool    g_received_ok_from_server;