From a0e5d84e2d777b7baaf9e7dfc671f1e846e17cc7 Mon Sep 17 00:00:00 2001
From: Moritz Perschke <moritz.perschke@uibk.ac.at>
Date: Wed, 3 Aug 2022 13:31:42 +0200
Subject: [PATCH] esp32 host and esp32 client now communicate as they should

---
 client/client/lib/espnow/src/espnow.cpp | 73 +++++++++++++------------
 client/client/lib/espnow/src/espnow.hpp |  5 +-
 client/client/src/main.cpp              | 14 ++++-
 host/esp32/lib/espnow/src/espnow.cpp    | 27 +++++----
 host/esp32/lib/espnow/src/espnow.hpp    |  1 +
 host/esp32/platformio.ini               |  1 +
 host/esp32/src/main.cpp                 | 15 +++--
 7 files changed, 79 insertions(+), 57 deletions(-)

diff --git a/client/client/lib/espnow/src/espnow.cpp b/client/client/lib/espnow/src/espnow.cpp
index 197ed71..25e45c3 100644
--- a/client/client/lib/espnow/src/espnow.cpp
+++ b/client/client/lib/espnow/src/espnow.cpp
@@ -4,7 +4,7 @@
 #include "WiFi.h"
 #include "espnow.hpp"
 
-uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // leaving this in as possible backup
+uint8_t A_MAC[] = {0x58, 0xCF, 0x79, 0x04, 0x37, 0x38};
 bool host_defined = false;
 esp_now_peer_info_t hostInfo;
 Preferences preferences;
@@ -15,13 +15,15 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status){
 }
 
 void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len){
+    Serial.println("message recieved");
     config new_config;
     memcpy(&new_config, incomingData, sizeof(new_config)); // TODO: check for valid mac
 
     // put the host address in flash mem
     preferences.begin("config", false);
     if(!preferences.isKey("host")){
-        preferences.putBytes("host", new_config.host, sizeof(uint8_t) * 6);
+        preferences.putBytes("host", new_config.host, sizeof(new_config.host));
+        Serial.println("host mac saved to flash");
     }// host change shouldn't be an issue
     preferences.end();
     // sync time
@@ -38,24 +40,20 @@ esp_err_t espnow_setup(){
         //initialization failed
         return result; // not sure about this
     }
-
-    preferences.begin("config", true);
+    
+    preferences.begin("config", false);
     if(preferences.isKey("host")){
-        // add host from flash mem as peer
-        preferences.getBytes("host", hostInfo.peer_addr, sizeof(uint8_t) * 6);
+        preferences.getBytes("host", hostInfo.peer_addr, sizeof(hostInfo.peer_addr));
+        Serial.println("Mac used from flash");
     }
     else{
-        // add Broadcast Mac as default
-        memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC));
+        memcpy(hostInfo.peer_addr, A_MAC, sizeof(hostInfo.peer_addr));
     }
     preferences.end();
+
     hostInfo.channel = 0;
-    hostInfo.encrypt = true;
-    result = esp_now_add_peer(&hostInfo);
-    if(result != ESP_OK){
-        //peer couldn't be added
-        return result;
-    }
+    hostInfo.encrypt = 0;
+    esp_now_add_peer(&hostInfo);
 
     esp_now_register_recv_cb(on_data_recv);
     esp_now_register_send_cb(on_data_sent);
@@ -64,39 +62,44 @@ esp_err_t espnow_setup(){
 }
 
 void Message::add_data(float value, int identifier){
-            if(amountData < NUM_SENSORS){
-                data->values[amountData] = value;
-                data->identifiers[amountData] = identifier;
-                amountData++;
-            }
-
-            if(data->timestamp == 0){
-                //add timestamp
-                data->timestamp = rtc.getMillis();
-            }
-        }
+    if(data->amountData < NUM_SENSORS){
+        data->values[data->amountData] = value;
+        data->identifiers[data->amountData] = identifier;
+        data->amountData++;
+    }
+}
 
 esp_err_t Message::send(){
+    Serial.println("sending Message");
     esp_err_t success;
-    success = esp_now_send(recipient, (uint8_t *) &data, sizeof(data)); 
+    success = esp_now_send(recipient, (uint8_t *) &data, sizeof(data));
+
+    for(int i=0; i<data->amountData; i++){
+        Serial.println(data->values[i]);
+    }
     // TODO: cache data before resetting
-    free((void*) data);
+    Serial.println((String) "time sent: " + data->timestamp);
+    Serial.println((String) "Send status: " + success);
+    Serial.println();
+    Serial.flush();
 
+    free((void*) data);
     return success;
 }
 
-// maybe do this in constructor
-void Message::set_timestamp(long millis){
-    data->timestamp = millis;
-}
-
 Message :: Message(){
     data = (data_struct*) malloc(sizeof(data_struct));
     
     preferences.begin("config", true);
-    preferences.getBytes("host", recipient, sizeof(uint8_t) * 6);
+    if(preferences.isKey("host")){
+        preferences.getBytes("host", recipient, sizeof(uint8_t) * 6);
+    }
+    else{
+        memcpy(recipient, A_MAC, sizeof(A_MAC));
+        Serial.println("backup mac used");
+    }
     preferences.end();
 
-    amountData = 0;
-    data->timestamp = 0; // I am assuming we are not sending data from Unix Epoch
+    data->amountData = 0;
+    data->timestamp = rtc.getMillis(); // I am assuming we are not sending data from Unix Epoch
 }
\ No newline at end of file
diff --git a/client/client/lib/espnow/src/espnow.hpp b/client/client/lib/espnow/src/espnow.hpp
index 0cfd977..7fa8ff0 100644
--- a/client/client/lib/espnow/src/espnow.hpp
+++ b/client/client/lib/espnow/src/espnow.hpp
@@ -8,6 +8,7 @@
 typedef struct data_struct{
     int identifiers[NUM_SENSORS];
     float values[NUM_SENSORS];
+    int amountData;
     long timestamp; //maybe make this array
 }data_struct;
 
@@ -22,15 +23,15 @@ class Message{
     public:
         Message();
         void add_data(float value, int identifier);
-        void set_timestamp(long millis);
         esp_err_t send();
 
     private:
         data_struct *data;
-        int amountData;
         uint8_t recipient[6];
 
 };
 
 esp_err_t espnow_setup();
+bool is_host_defined();
+
 #endif
\ No newline at end of file
diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp
index 0ce3c63..2554b91 100644
--- a/client/client/src/main.cpp
+++ b/client/client/src/main.cpp
@@ -6,13 +6,21 @@ void setup() {
   // put your setup code here, to run once:
   Serial.begin(115200);
   while(!Serial);
+  Serial.flush();
 
   esp_err_t result = espnow_setup();
+
 }
 
+int counter = 0;
+
 void loop() {
   // put your main code here, to run repeatedly:
-  // Message* new_data = new Message();
-  // new_data->send();
-  // delete new_data;
+  Message* new_data = new Message();
+  new_data->add_data(++counter * 1.1, 0);
+  new_data->add_data(counter * 1.2, 1);
+  new_data->add_data(counter * 1.3, 2);
+  new_data->send();
+  delete new_data;
+  delay(5000);
 }
\ No newline at end of file
diff --git a/host/esp32/lib/espnow/src/espnow.cpp b/host/esp32/lib/espnow/src/espnow.cpp
index d031dcc..71f8c96 100644
--- a/host/esp32/lib/espnow/src/espnow.cpp
+++ b/host/esp32/lib/espnow/src/espnow.cpp
@@ -6,19 +6,25 @@
 
 uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // leaving this in as possible backup
 esp_now_peer_info_t hostInfo;
-data_struct lastData;
 ESP32Time rtc;
 
 void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status){
+    Serial.println((String) "Message sent: " + status);
 }
 
 
 void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len){
+    data_struct lastData;
     memcpy(&lastData, incomingData, sizeof(lastData));
-    for(int i=0; i<NUM_SENSORS; i++){
-        Serial.println(lastData.identifiers[i] + lastData.values[i]);
-    }
-    Serial.println("at " + lastData.timestamp);
+    Serial.println("message copied");
+    // for(int i=0; i<lastData.amountData; i++){
+    //     Serial.print((String) lastData.identifiers[i] + ": ");
+    //     Serial.println((String) lastData.values[i]);
+    // }
+    Serial.println((String) lastData.values[0]);
+    Serial.println((String) "sent time: " + lastData.timestamp);
+    Serial.println((String) "RTC time: " + rtc.getMillis());
+    Serial.flush();
 }
 
 esp_err_t espnow_setup(){
@@ -30,13 +36,10 @@ esp_err_t espnow_setup(){
         return result; // not sure about this
     }
 
+    memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC));
     hostInfo.channel = 0;
-    hostInfo.encrypt = true;
-    result = esp_now_add_peer(&hostInfo);
-    if(result != ESP_OK){
-        //peer couldn't be added
-        return result;
-    }
+    hostInfo.encrypt = 0;
+    esp_now_add_peer(&hostInfo);
 
     esp_now_register_recv_cb(on_data_recv);
     esp_now_register_send_cb(on_data_sent);
@@ -45,7 +48,7 @@ esp_err_t espnow_setup(){
 }
 
 Message :: Message(){
-    memcpy(BROADCAST_MAC, recipient, sizeof(BROADCAST_MAC));
+    memcpy(recipient, BROADCAST_MAC, sizeof(BROADCAST_MAC));
     config = (config_struct*) malloc(sizeof(config_struct));
     esp_wifi_get_mac(WIFI_IF_STA, config->host);
     config->time_millis = rtc.getMillis();
diff --git a/host/esp32/lib/espnow/src/espnow.hpp b/host/esp32/lib/espnow/src/espnow.hpp
index 0f58892..ba7313a 100644
--- a/host/esp32/lib/espnow/src/espnow.hpp
+++ b/host/esp32/lib/espnow/src/espnow.hpp
@@ -6,6 +6,7 @@
 typedef struct data_struct{
     int identifiers[NUM_SENSORS];
     float values[NUM_SENSORS];
+    int amountData;
     long timestamp; //maybe make this array
 }data_struct;
 
diff --git a/host/esp32/platformio.ini b/host/esp32/platformio.ini
index e19f975..62f6cf2 100644
--- a/host/esp32/platformio.ini
+++ b/host/esp32/platformio.ini
@@ -12,4 +12,5 @@
 platform = espressif32
 board = esp32-c3-devkitm-1
 framework = arduino
+monitor_speed=115200
 lib_deps = fbiego/ESP32Time@^2.0.0
diff --git a/host/esp32/src/main.cpp b/host/esp32/src/main.cpp
index 48f83e3..4232420 100644
--- a/host/esp32/src/main.cpp
+++ b/host/esp32/src/main.cpp
@@ -7,14 +7,19 @@ void setup() {
   // put your setup code here, to run once:
   Serial.begin(115200);
   while(!Serial);
-  WiFi.mode(WIFI_STA);
+  Serial.flush();
   espnow_setup();
-
-  Message* msg = new Message;
-  msg->send();
 }
 
+int counter = 0;
 void loop() {
   // put your main code here, to run repeatedly:
+  if(counter++ % 10 == 0){
+    Message* msg = new Message;
+    msg->send();
+    delete msg;
+  }
+
   Serial.println("No message...");
-}
\ No newline at end of file
+  delay(1000);
+} 
\ No newline at end of file
-- 
GitLab