Skip to content
Snippets Groups Projects
Commit 59445779 authored by Zoe Michaela Dietmar Pfister's avatar Zoe Michaela Dietmar Pfister :gay_pride_flag:
Browse files

Merge branch 'espnow_cleanup' into 'develop'

moved mac storage to singleton, tried to reduce response time

See merge request !26
parents e9bcf0e1 e4806432
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
...@@ -237,7 +237,7 @@ fabric.properties ...@@ -237,7 +237,7 @@ fabric.properties
# Android studio 3.1+ serialized cache file # Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser .idea/caches/build_file_checksums.ser
.vscode/* **/.vscode/**
# Local History for Visual Studio Code # Local History for Visual Studio Code
.history/ .history/
......
.pio .pio
.vscode/.browse.c_cpp.db* .vscode/**
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
/home/moritz/Documents/TEAM/sensor-system/client/libs/
\ No newline at end of file
...@@ -16,14 +16,14 @@ void send_msgs(const std::__cxx11::list<Message> msgs) { ...@@ -16,14 +16,14 @@ void send_msgs(const std::__cxx11::list<Message> msgs) {
for (const Message &msg: msgs) { for (const Message &msg: msgs) {
if (msg.send() != ESP_OK) { if (msg.send() != ESP_OK) {
RtcMemory::store(msg.getMessageAsMinifiedJsonString()); RtcMemory::getInstance().store_data(msg.getMessageAsMinifiedJsonString());
} }
unsigned long ts = millis(); unsigned long ts = millis();
// it takes ~110ms for receiving an acknowledgement by the host in perfect conditions // it takes ~110ms for receiving an acknowledgement by the host in perfect conditions
uint16_t message_timeout = 2000; uint16_t message_timeout = 2000;
while (!was_msg_received()) { while (!was_msg_received()) {
if ((millis() - ts) > message_timeout) { if ((millis() - ts) > message_timeout) {
RtcMemory::store(msg.getMessageAsMinifiedJsonString()); RtcMemory::getInstance().store_data(msg.getMessageAsMinifiedJsonString());
ESP_LOGE(TAG, "Timeout: Host not available\n"); ESP_LOGE(TAG, "Timeout: Host not available\n");
break; break;
} }
......
#include "ram_caching.hpp" #include "ram_caching.hpp"
namespace RtcMemory{ static const char* TAG = "CACHING";
// 2D array of 20 Strings with maxLength of 251 RTC_DATA_ATTR char storage[maxLength_][maxSize_];
RTC_DATA_ATTR char storage[maxLength_][maxSize_]; RTC_DATA_ATTR int storedElements;
RTC_DATA_ATTR int storedElements; RTC_DATA_ATTR int headElement = 0;
RTC_DATA_ATTR int headElement = 0; RTC_DATA_ATTR int tailElement = 0;
RTC_DATA_ATTR int tailElement = 0;
static const char* TAG = "CACHING"; void RtcMemory::store_data(std::string message){
// turn data into char array
const char* jsonString = message.c_str();
void store(std::string message){ // move head to new element
headElement = (headElement + 1) % maxSize_;
// turn data into char array // apparently I am not allowed to copy to rtc mem
const char* jsonString = message.c_str(); for(int i=0; i<maxLength_; i++){
storage[i][headElement] = jsonString[i];
// move head to new element if(jsonString[i] == '\0'){
headElement = (headElement + 1) % maxSize_; break;
// apparently I am not allowed to copy to rtc mem
for(int i=0; i<maxLength_; i++){
storage[i][headElement] = jsonString[i];
if(jsonString[i] == '\0'){
break;
}
} }
storedElements++;
ESP_LOGE(TAG, "Moved message to storage.");
} }
storedElements++;
ESP_LOGE(TAG, "Moved message to storage.");
}
String get_from_storage(){ String RtcMemory::get_data_from_storage(){
// remove element pointed at by tail // remove element pointed at by tail
String buf = ""; String buf = "";
char current = '\0'; char current = '\0';
for(int i = 0; i<maxLength_; i++){ for(int i = 0; i<maxLength_; i++){
current = storage[i][tailElement]; current = storage[i][tailElement];
buf += current; buf += current;
if(current == '\0'){ if(current == '\0'){
break; break;
}
} }
// move tail to next element
tailElement = (tailElement + 1) % maxSize_;
storedElements--;
ESP_LOGE(TAG, "Retrieved message from storage");
return buf;
} }
// move tail to next element
tailElement = (tailElement + 1) % maxSize_;
storedElements--;
ESP_LOGE(TAG, "Retrieved message from storage");
return buf;
}
bool is_full(){ bool RtcMemory::is_data_storage_full(){
return headElement == tailElement; return headElement == tailElement;
} }
int RtcMemory::amount_stored_data(){
return storedElements;
}
void RtcMemory::get_host_mac(uint8_t* destination, bool open){
if(!open){preferences.begin("config", true);}
if (preferences.isKey("host")) {
preferences.getBytes("host", destination, sizeof(uint8_t) * 6);
ESP_LOGI(TAG, "Host Mac retrieved from flash");
} else {
memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC));
ESP_LOGI(TAG, "Backup MAC address used");
}
if(!open){preferences.end();}
int stored_amount(){
return storedElements;
}
} }
void RtcMemory::store_mac_address(uint8_t* mac){
preferences.begin("config", false);
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]);
}
else{
ESP_LOGI(TAG, "Couldn't save Host Mac to flash");
}
preferences.end();
}
bool RtcMemory::does_host_exist(){
preferences.begin("config", true);
bool answer = preferences.isKey("host");
preferences.end();
return answer;
}
\ No newline at end of file
...@@ -2,15 +2,37 @@ ...@@ -2,15 +2,37 @@
#define _RAM_CACHE #define _RAM_CACHE
#include "esp_log.h" #include "esp_log.h"
#include <ESP32Time.h> #include <ESP32Time.h>
#include <Preferences.h>
#define maxSize_ 20 #define maxSize_ 20
#define maxLength_ 251 #define maxLength_ 251
namespace RtcMemory { // move to singleton according to https://stackoverflow.com/a/1008289
void store(std::string message); // used to save data/mac addresses without loosing them in deep sleep
String get_from_storage(); class RtcMemory {
bool is_full(); public:
int stored_amount(); static RtcMemory &getInstance(){
} static RtcMemory instance;
return instance;
}
RtcMemory(RtcMemory const&) = delete;
void operator=(RtcMemory const&) = delete;
void store_data(std::string message);
String get_data_from_storage();
bool is_data_storage_full();
int amount_stored_data();
void store_mac_address(uint8_t* mac);
void get_host_mac(uint8_t* destination, bool open=false);
bool does_host_exist();
private:
RtcMemory(){}
// used for MAC storage
Preferences preferences;
uint8_t BROADCAST_MAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
};
#endif #endif
\ No newline at end of file
...@@ -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,49 +44,30 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) ...@@ -56,49 +44,30 @@ 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);
// delete old host // delete old host
preferences.begin("config", false); uint8_t old[6];
if(preferences.isKey("host")){ RtcMemory::getInstance().get_host_mac(old);
if(memcmp(received_msg.host, old, 6) == 0){
ESP_LOGI(TAG, "removing old host"); ESP_LOGI(TAG, "removing old host");
uint8_t old[6];
preferences.end();
get_host_mac(old); // maybe problem here, re-opening preferences
esp_now_del_peer(old); esp_now_del_peer(old);
} }
// add new host // add new host
preferences.begin("config", false); RtcMemory::getInstance().store_mac_address(received_msg.host);
if(preferences.putBytes("host", received_msg.host, sizeof(received_msg.host)) > 0){
ESP_LOGI(TAG, "Host MAC address saved to flash %02X:%02X:%02X:%02X:%02X:%02X", received_msg.host[0],
received_msg.host[1],received_msg.host[2],received_msg.host[3],received_msg.host[4],received_msg.host[5]);
}
else{
ESP_LOGI(TAG, "Couldn't save Host Mac to flash");
}
preferences.end();
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());
preferences.begin("config", false);
if (!preferences.isKey("host")) {
if(preferences.putBytes("host", received_msg.host, sizeof(received_msg.host)) > 0){
ESP_LOGI(TAG, "host MAC address saved to flash %02X:%02X:%02X:%02X:%02X:%02X", received_msg.host[0],
received_msg.host[1],received_msg.host[2],received_msg.host[3],received_msg.host[4],received_msg.host[5]);
}
// add host to peers
add_host_to_peers(received_msg);
}
preferences.end();
// delay(50);
msg_recv = true;
} }
// delay(50);
default:{ default:{
break; break;
} }
...@@ -117,7 +86,7 @@ esp_err_t espnow_setup() ...@@ -117,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;
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <Preferences.h> #include <Preferences.h>
#include <WiFi.h> #include <WiFi.h>
#include <esp_now.h> #include <esp_now.h>
#include <algorithm>
enum MessageType{ enum MessageType{
dataAck, dataAck,
......
...@@ -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);
} }
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