From 8e05e442d71c63c100bb87ae882cfaccf067bb32 Mon Sep 17 00:00:00 2001 From: Moritz Perschke <moritz.perschke@uibk.ac.at> Date: Wed, 18 Jan 2023 12:56:35 +0100 Subject: [PATCH] tested --- client/libs/caching/src/ram_caching.cpp | 2 +- client/libs/caching/src/ram_caching.hpp | 1 - client/libs/espnow/src/ESPNow.cpp | 22 +++-------------- client/libs/espnow/src/Message.cpp | 4 ++-- host/host_central_mast/src/main.cpp | 32 ++++++++++++------------- 5 files changed, 21 insertions(+), 40 deletions(-) diff --git a/client/libs/caching/src/ram_caching.cpp b/client/libs/caching/src/ram_caching.cpp index a7c9a65..9571102 100644 --- a/client/libs/caching/src/ram_caching.cpp +++ b/client/libs/caching/src/ram_caching.cpp @@ -68,7 +68,7 @@ void RtcMemory::get_host_mac(uint8_t* destination, bool open){ void RtcMemory::store_mac_address(uint8_t* mac){ preferences.begin("config", false); - if(preferences.putBytes("host", mac, sizeof(mac)) > 0){ + if(preferences.putBytes("host", mac, sizeof(uint8_t) * 6) > 0){ ESP_LOGI(TAG, "Host MAC address saved to flash %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1],mac[2],mac[3],mac[4],mac[5]); } diff --git a/client/libs/caching/src/ram_caching.hpp b/client/libs/caching/src/ram_caching.hpp index aea4108..316fa17 100644 --- a/client/libs/caching/src/ram_caching.hpp +++ b/client/libs/caching/src/ram_caching.hpp @@ -29,7 +29,6 @@ class RtcMemory { private: RtcMemory(){} - // used for data storage // used for MAC storage Preferences preferences; uint8_t BROADCAST_MAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; diff --git a/client/libs/espnow/src/ESPNow.cpp b/client/libs/espnow/src/ESPNow.cpp index 7c005fd..b562771 100644 --- a/client/libs/espnow/src/ESPNow.cpp +++ b/client/libs/espnow/src/ESPNow.cpp @@ -15,18 +15,6 @@ bool was_msg_received(){ return false; } -void get_host_mac(uint8_t *destination) -{ - preferences.begin("config", true); - if (preferences.isKey("host")) { - preferences.getBytes("host", destination, sizeof(uint8_t) * 6); - } else { - memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC)); - ESP_LOGI(TAG, "Backup MAC address used"); - } - preferences.end(); -} - esp_err_t add_host_to_peers(config received){ esp_now_peer_info_t host; memset(&host, 0, sizeof(host)); @@ -56,6 +44,7 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) // you can also set your own MAC https://randomnerdtutorials.com/get-change-esp32-esp8266-mac-address-arduino/ switch (received_msg.type){ case hostChange:{ + msg_recv = true; ESP_LOGI(TAG, "hostChange received"); Time::getInstance().setTime(received_msg.epoch_seconds); @@ -71,22 +60,17 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) add_host_to_peers(received_msg); } case dataAck:{ + msg_recv = true; ESP_LOGI(TAG, "dataAck received."); Time::getInstance().setTime( received_msg.epoch_seconds); // see https://www.esp32.com/viewtopic.php?t=9965, maybe this needs an offset ESP_LOGI(TAG, "Timestamp received: %ld", Time::getInstance().getEpochSeconds()); - if(RtcMemory::getInstance().does_host_exist()){ - RtcMemory::getInstance().store_mac_address(received_msg.host); - add_host_to_peers(received_msg); - } - // add host to peers } // delay(50); default:{ break; } - msg_recv = true; } } @@ -102,7 +86,7 @@ esp_err_t espnow_setup() return result; // not sure about this } - get_host_mac(hostInfo.peer_addr); // check if there is a host saved in flash mem, broadcast otherwise + RtcMemory::getInstance().get_host_mac(hostInfo.peer_addr); // check if there is a host saved in flash mem, broadcast otherwise hostInfo.channel = 0; diff --git a/client/libs/espnow/src/Message.cpp b/client/libs/espnow/src/Message.cpp index b24ecad..fb5960e 100644 --- a/client/libs/espnow/src/Message.cpp +++ b/client/libs/espnow/src/Message.cpp @@ -30,11 +30,11 @@ std::string Message::getMessageAsMinifiedJsonString() const Message::Message(ClientDataPackage data) : clientDataPackage(std::move(data)) { // check for existing host mac address, use broadcast otherwise - get_host_mac(recipient); + RtcMemory::getInstance().get_host_mac(recipient); } Message::Message(MeasurementData const &data, const SensorInformation &information, unsigned long timestamp) : clientDataPackage(data, information, timestamp) { // check for existing host mac address, use broadcast otherwise - get_host_mac(recipient); + RtcMemory::getInstance().get_host_mac(recipient); } diff --git a/host/host_central_mast/src/main.cpp b/host/host_central_mast/src/main.cpp index 9a56051..83adf6a 100644 --- a/host/host_central_mast/src/main.cpp +++ b/host/host_central_mast/src/main.cpp @@ -132,19 +132,13 @@ void syncUTCTimeToRTC(); void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) { response response = {}; - response.type = dataAck; + // send a host change msg if client is new, simplifies client + response.type = esp_now_is_peer_exist(mac) ? dataAck : hostChange; esp_read_mac(response.mac, ESP_MAC_WIFI_STA); response.time = rtc.getEpoch(); - esp_err_t success = esp_now_send(mac, (uint8_t*) &response, sizeof(response)); - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), - (success == ESP_OK) ? "Response sent\n" : "Failed to respond\n"); - - esp_log_write(ESP_LOG_INFO, TAG_ESPNOW.c_str(), "Message recieved\n"); - // copy received data to a char array - char data[len]; - memcpy(data, incomingData, len); - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Raw received Data: %s\n", data); + // this block needs to happen before we send the message, as it's not possible to + // send a message to an unregistered peer (i.e. new client) if(!esp_now_is_peer_exist(mac)){ esp_now_peer_info_t client = {}; memcpy(client.peer_addr, mac, sizeof(uint8_t) * 6); @@ -157,6 +151,17 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) } } + esp_err_t success = esp_now_send(mac, (uint8_t*) &response, sizeof(response)); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), + (success == ESP_OK) ? "Response sent\n" : "Failed to respond\n"); + + + esp_log_write(ESP_LOG_INFO, TAG_ESPNOW.c_str(), "Message recieved\n"); + // copy received data to a char array + char data[len]; + memcpy(data, incomingData, len); + esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Raw received Data: %s\n", data); + DynamicJsonDocument doc = parseReceivedJsonData(data); String macAddress = getMacAddressAsString(mac); @@ -179,13 +184,6 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) queue.push(lineData); xSemaphoreGive(xMutex); - response response = {}; - response.type = dataAck; - esp_read_mac(response.mac, ESP_MAC_WIFI_STA); - response.time = rtc.getEpoch(); - esp_err_t success = esp_now_send(mac, (uint8_t*) &response, sizeof(response)); - esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), - (success == ESP_OK) ? "Response sent\n" : "Failed to respond\n"); } String documentToLineProtocolString(const DynamicJsonDocument &doc) -- GitLab