diff --git a/client/client/lib/espnow/src/espnow.cpp b/client/client/lib/espnow/src/espnow.cpp index 11c111818ef16377b8f1d3b8b060acb80e7fa853..81fed6ec7016735a48ca5cb6ae1640891a93dbd0 100644 --- a/client/client/lib/espnow/src/espnow.cpp +++ b/client/client/lib/espnow/src/espnow.cpp @@ -41,15 +41,7 @@ esp_err_t espnow_setup(){ return result; // not sure about this } - preferences.begin("config", false); - if(preferences.isKey("host")){ - preferences.getBytes("host", hostInfo.peer_addr, sizeof(hostInfo.peer_addr)); - Serial.println("Mac used from flash"); - } - else{ - memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(hostInfo.peer_addr)); - } - preferences.end(); // check if there is a host saved in flash mem, broadcast otherwise + get_host_mac(hostInfo.peer_addr); // check if there is a host saved in flash mem, broadcast otherwise hostInfo.channel = 0; hostInfo.encrypt = 0; @@ -92,15 +84,8 @@ esp_err_t Message::send(){ Message :: Message(){ data = (data_struct*) malloc(sizeof(data_struct)); - preferences.begin("config", true); - if(preferences.isKey("host")){ - preferences.getBytes("host", recipient, sizeof(uint8_t) * 6); - } - else{ - memcpy(recipient, BROADCAST_MAC, sizeof(BROADCAST_MAC)); - Serial.println("backup mac used"); - } - preferences.end(); + // check for existing host mac address, use broadcast otherwise + get_host_mac(recipient); data->amountData = 0; data->timestamp = rtc.getMillis(); // I am assuming we are not sending data from Unix Epoch @@ -108,18 +93,21 @@ Message :: Message(){ Message :: Message(data_struct old_data){ memcpy(data, &old_data, sizeof(data)); + get_host_mac(recipient); +} + +Message :: ~Message(){ + free((void*) data); +} +void get_host_mac(uint8_t* destination){ preferences.begin("config", true); if(preferences.isKey("host")){ - preferences.getBytes("host", recipient, sizeof(uint8_t) * 6); + preferences.getBytes("host", destination, sizeof(uint8_t) * 6); } else{ - memcpy(recipient, BROADCAST_MAC, sizeof(BROADCAST_MAC)); + memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC)); Serial.println("backup mac used"); } preferences.end(); -} - -Message :: ~Message(){ - free((void*) data); } \ No newline at end of file