diff --git a/.gitignore b/.gitignore index 88fc45a5d3e6393ec741bdcde9b09edfd146f48e..b539c117fcc142b4f55d30a75ce7b212ed68869f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ downloads/ eggs/ .eggs/ lib/ +!client/client/lib lib64/ parts/ sdist/ diff --git a/client/client/lib/espnow/src/espnow.cpp b/client/client/lib/espnow/src/espnow.cpp index 0e3ab626b10c5e82796d20e9493da344983816ed..d21e50bb1cfd6f0db95443b43f77ed7aef0f35ae 100644 --- a/client/client/lib/espnow/src/espnow.cpp +++ b/client/client/lib/espnow/src/espnow.cpp @@ -4,7 +4,7 @@ #include "espnow.hpp" uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // leaving this in as possible backup -uint8_t host_mac[6]; +bool host_defined = false; esp_now_peer_info_t hostInfo; Preferences preferences; @@ -13,14 +13,16 @@ 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){ - // sync time 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); - } + }// host change shouldn't be an issue + preferences.end(); + // sync time } @@ -31,13 +33,22 @@ esp_err_t espnow_setup(){ result = esp_now_init(); if(result != ESP_OK){ //initialization failed - return result; + return result; // not sure about this } - memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC)); + preferences.begin("config", true); + if(preferences.isKey("host")){ + // add host from flash mem as peer + preferences.getBytes("host", hostInfo.peer_addr, sizeof(uint8_t) * 6); + } + else{ + // add Broadcast Mac as default + memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC)); + } + preferences.end(); hostInfo.channel = 0; - hostInfo.encrypt = true; //if we encrypt espnow we can have no more than 10 clients per host - result = esp_now_add_peer(&hostInfo); //there are a couple errors that can happen here + hostInfo.encrypt = true; + result = esp_now_add_peer(&hostInfo); if(result != ESP_OK){ //peer couldn't be added return result; @@ -50,31 +61,38 @@ esp_err_t espnow_setup(){ } void Message::add_data(float value, int identifier){ - data.values[amountData] = value; - data.identifiers[amountData] = identifier; - amountData++; + if(amountData < NUM_SENSORS){ + data->values[amountData] = value; + data->identifiers[amountData] = identifier; + amountData++; + } - if(data.timestamp == NULL){ + if(data->timestamp == 0){ //add timestamp } } -// BROADCAST_MAC is to be changed esp_err_t Message::send(){ esp_err_t success; - if(host_mac == NULL){ - success = esp_now_send(BROADCAST_MAC, (uint8_t *) &data, sizeof(data)); - }// if no host defined broadcast message - success = esp_now_send(host_mac, (uint8_t *) &data, sizeof(data)); - + success = esp_now_send(recipient, (uint8_t *) &data, sizeof(data)); // TODO: cache data before resetting - data_struct empty_data; - data = empty_data; - amountData = 0; + free((void*) data); return success; } +// maybe do this in constructor void Message::set_timestamp(long millis){ - data.timestamp = 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); + preferences.end(); + + amountData = 0; + data->timestamp = 0; // 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 b2cc687afd518630fb0f79c9942054599c482980..0cfd9772a929588000aff0d212c72aa12bf766fe 100644 --- a/client/client/lib/espnow/src/espnow.hpp +++ b/client/client/lib/espnow/src/espnow.hpp @@ -9,8 +9,9 @@ typedef struct data_struct{ int identifiers[NUM_SENSORS]; float values[NUM_SENSORS]; long timestamp; //maybe make this array -}data; +}data_struct; +// Format of the message sent from host to client // if more things are sent from the host the name might not be accurate anymore typedef struct config{ uint8_t host[6]; @@ -19,13 +20,15 @@ typedef struct config{ 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 = 0; + data_struct *data; + int amountData; + uint8_t recipient[6]; }; diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp index 91da067ae710afa516736eee9e828bacd5627a23..0ce3c6368236d8a249cc4eb087985559c5b2ad09 100644 --- a/client/client/src/main.cpp +++ b/client/client/src/main.cpp @@ -7,9 +7,12 @@ void setup() { Serial.begin(115200); while(!Serial); - int result = espnow_setup(); + esp_err_t result = espnow_setup(); } void loop() { // put your main code here, to run repeatedly: + // Message* new_data = new Message(); + // new_data->send(); + // delete new_data; } \ No newline at end of file