diff --git a/client/client/lib/espnow/src/ESPNow.cpp b/client/client/lib/espnow/src/ESPNow.cpp index a92f79b459369a84d7b7cea564f5955bcfe9122d..99b4abd94fd88a8ba9c449c54c856a2339fd653a 100644 --- a/client/client/lib/espnow/src/ESPNow.cpp +++ b/client/client/lib/espnow/src/ESPNow.cpp @@ -28,15 +28,29 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) // go to sleep } +bool are_mac_addresses_equal(uint8_t first_mac[], uint8_t second_mac[]){ + bool result = true; + for(int i=0; i<6; i++){ + result && (first_mac[i] == second_mac[i]); + } + return result; +} + void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) { - ESP_LOGI(TAG, "Message recieved"); + ESP_LOGE(TAG, "Message recieved"); + preferences.begin("config", false); config new_config; memcpy(&new_config, incomingData, sizeof(new_config)); // TODO: check for valid mac + // all the esp32 macs so far use the same first 3(?) bytes so maybe use that + + uint8_t stored[6]; + preferences.getBytes("host", stored, sizeof(uint8_t) * 6); // put the host address in flash mem - preferences.begin("config", false); - if (!preferences.isKey("host")) { + // this does not work as long as we don't know whether a message was received + // save received host address if there is none saved or received one is different than before + if (!preferences.isKey("host") || !are_mac_addresses_equal(stored, new_config.host)) { preferences.putBytes("host", new_config.host, sizeof(new_config.host)); ESP_LOGI(TAG, "host MAC address saved to flash"); @@ -50,7 +64,18 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) ESP_LOGE(TAG, "Adding host to peers failed"); } - } // host change shouldn't be an issue + if(!are_mac_addresses_equal(stored, new_config.host)){ + esp_now_del_peer(stored); + } + + } // changed Oct. 24th: now has the capability to change the mac in flash + else{ + uint8_t stored[6]; + preferences.getBytes("host", stored, sizeof(uint8_t) * 6); + if(!are_mac_addresses_equal(stored, new_config.host)){ + + } + } preferences.end(); // sync time Time::getInstance().setTime( @@ -77,6 +102,7 @@ esp_err_t espnow_setup() hostInfo.encrypt = false; esp_now_add_peer(&hostInfo); + preferences.clear(); esp_now_register_recv_cb(on_data_recv); esp_now_register_send_cb(on_data_sent); diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp index 4656cf0ef9a3aacb60654fa8e5fa683769a81803..b3fa6135522679b4395de22b1d4fff641e5ad142 100644 --- a/client/client/src/main.cpp +++ b/client/client/src/main.cpp @@ -32,21 +32,12 @@ void loop() auto messages = drs26.buildMessages(); for (const Message &message : messages) { - // if(message.send() != ESP_OK){ - // RtcMemory::store(message.getMessageAsMinifiedJsonString()); - // } + if(message.send() != ESP_OK){ + RtcMemory::store(message.getMessageAsMinifiedJsonString()); + } // some sort of if(message received) is needed as well - RtcMemory::store(message.getMessageAsMinifiedJsonString()); - // Serial.println("original string:"); - // Serial.println(message.getMessageAsMinifiedJsonString().c_str()); - // Serial.println("length: " + (String) sizeof(message.getMessageAsMinifiedJsonString().c_str())); - // Serial.println("\n after retrieval:"); - String test = RtcMemory::get_from_storage(); - // Serial.println(test); - // Serial.println("length: " + (String) sizeof(test)); - Serial.println("------------------------"); + // String test = RtcMemory::get_from_storage(); } - Serial.println("================================"); } catch (const NoDataAvailableException &e) { std::cerr << e.what() << '\n'; @@ -60,5 +51,5 @@ void loop() Serial.println(WiFi.macAddress()); Serial.println("\n"); delay(10000); - // DeepSleep::deep_sleep(5); + DeepSleep::deep_sleep(5); }