Skip to content
Snippets Groups Projects
Commit 8e05e442 authored by Moritz Perschke's avatar Moritz Perschke
Browse files

tested

parent bc6814c6
No related branches found
No related tags found
2 merge requests!39Merge Develop into Main,!26moved mac storage to singleton, tried to reduce response time
...@@ -68,7 +68,7 @@ void RtcMemory::get_host_mac(uint8_t* destination, bool open){ ...@@ -68,7 +68,7 @@ void RtcMemory::get_host_mac(uint8_t* destination, bool open){
void RtcMemory::store_mac_address(uint8_t* mac){ void RtcMemory::store_mac_address(uint8_t* mac){
preferences.begin("config", false); 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], 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]); mac[1],mac[2],mac[3],mac[4],mac[5]);
} }
......
...@@ -29,7 +29,6 @@ class RtcMemory { ...@@ -29,7 +29,6 @@ class RtcMemory {
private: private:
RtcMemory(){} RtcMemory(){}
// used for data storage
// used for MAC storage // used for MAC storage
Preferences preferences; Preferences preferences;
uint8_t BROADCAST_MAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; uint8_t BROADCAST_MAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
......
...@@ -15,18 +15,6 @@ bool was_msg_received(){ ...@@ -15,18 +15,6 @@ bool was_msg_received(){
return false; 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_err_t add_host_to_peers(config received){
esp_now_peer_info_t host; esp_now_peer_info_t host;
memset(&host, 0, sizeof(host)); memset(&host, 0, sizeof(host));
...@@ -56,6 +44,7 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) ...@@ -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/ // you can also set your own MAC https://randomnerdtutorials.com/get-change-esp32-esp8266-mac-address-arduino/
switch (received_msg.type){ switch (received_msg.type){
case hostChange:{ case hostChange:{
msg_recv = true;
ESP_LOGI(TAG, "hostChange received"); ESP_LOGI(TAG, "hostChange received");
Time::getInstance().setTime(received_msg.epoch_seconds); 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) ...@@ -71,22 +60,17 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len)
add_host_to_peers(received_msg); add_host_to_peers(received_msg);
} }
case dataAck:{ case dataAck:{
msg_recv = true;
ESP_LOGI(TAG, "dataAck received."); ESP_LOGI(TAG, "dataAck received.");
Time::getInstance().setTime( Time::getInstance().setTime(
received_msg.epoch_seconds); // see https://www.esp32.com/viewtopic.php?t=9965, maybe this needs an offset 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()); 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); // delay(50);
default:{ default:{
break; break;
} }
msg_recv = true;
} }
} }
...@@ -102,7 +86,7 @@ esp_err_t espnow_setup() ...@@ -102,7 +86,7 @@ esp_err_t espnow_setup()
return result; // not sure about this 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; hostInfo.channel = 0;
......
...@@ -30,11 +30,11 @@ std::string Message::getMessageAsMinifiedJsonString() const ...@@ -30,11 +30,11 @@ std::string Message::getMessageAsMinifiedJsonString() const
Message::Message(ClientDataPackage data) : clientDataPackage(std::move(data)) Message::Message(ClientDataPackage data) : clientDataPackage(std::move(data))
{ {
// check for existing host mac address, use broadcast otherwise // 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) Message::Message(MeasurementData const &data, const SensorInformation &information, unsigned long timestamp)
: clientDataPackage(data, information, timestamp) : clientDataPackage(data, information, timestamp)
{ {
// check for existing host mac address, use broadcast otherwise // check for existing host mac address, use broadcast otherwise
get_host_mac(recipient); RtcMemory::getInstance().get_host_mac(recipient);
} }
...@@ -132,19 +132,13 @@ void syncUTCTimeToRTC(); ...@@ -132,19 +132,13 @@ void syncUTCTimeToRTC();
void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len)
{ {
response response = {}; 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); esp_read_mac(response.mac, ESP_MAC_WIFI_STA);
response.time = rtc.getEpoch(); 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)){ if(!esp_now_is_peer_exist(mac)){
esp_now_peer_info_t client = {}; esp_now_peer_info_t client = {};
memcpy(client.peer_addr, mac, sizeof(uint8_t) * 6); 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) ...@@ -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); DynamicJsonDocument doc = parseReceivedJsonData(data);
String macAddress = getMacAddressAsString(mac); String macAddress = getMacAddressAsString(mac);
...@@ -179,13 +184,6 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) ...@@ -179,13 +184,6 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len)
queue.push(lineData); queue.push(lineData);
xSemaphoreGive(xMutex); 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) String documentToLineProtocolString(const DynamicJsonDocument &doc)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment