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

added functionality to change mac address in flash

parent 38205401
No related branches found
No related tags found
4 merge requests!39Merge Develop into Main,!19development into master,!17Inital Host, initial Client,!6Espnow
...@@ -28,15 +28,29 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) ...@@ -28,15 +28,29 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status)
// go to sleep // 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) 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; config new_config;
memcpy(&new_config, incomingData, sizeof(new_config)); // TODO: check for valid mac 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 // put the host address in flash mem
preferences.begin("config", false); // this does not work as long as we don't know whether a message was received
if (!preferences.isKey("host")) { // 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)); preferences.putBytes("host", new_config.host, sizeof(new_config.host));
ESP_LOGI(TAG, "host MAC address saved to flash"); 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) ...@@ -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"); 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(); preferences.end();
// sync time // sync time
Time::getInstance().setTime( Time::getInstance().setTime(
...@@ -77,6 +102,7 @@ esp_err_t espnow_setup() ...@@ -77,6 +102,7 @@ esp_err_t espnow_setup()
hostInfo.encrypt = false; hostInfo.encrypt = false;
esp_now_add_peer(&hostInfo); esp_now_add_peer(&hostInfo);
preferences.clear();
esp_now_register_recv_cb(on_data_recv); esp_now_register_recv_cb(on_data_recv);
esp_now_register_send_cb(on_data_sent); esp_now_register_send_cb(on_data_sent);
......
...@@ -32,21 +32,12 @@ void loop() ...@@ -32,21 +32,12 @@ void loop()
auto messages = drs26.buildMessages(); auto messages = drs26.buildMessages();
for (const Message &message : messages) { for (const Message &message : messages) {
// if(message.send() != ESP_OK){ if(message.send() != ESP_OK){
// RtcMemory::store(message.getMessageAsMinifiedJsonString()); RtcMemory::store(message.getMessageAsMinifiedJsonString());
// } }
// some sort of if(message received) is needed as well // some sort of if(message received) is needed as well
RtcMemory::store(message.getMessageAsMinifiedJsonString()); // String test = RtcMemory::get_from_storage();
// 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("------------------------");
} }
Serial.println("================================");
} catch (const NoDataAvailableException &e) { } catch (const NoDataAvailableException &e) {
std::cerr << e.what() << '\n'; std::cerr << e.what() << '\n';
...@@ -60,5 +51,5 @@ void loop() ...@@ -60,5 +51,5 @@ void loop()
Serial.println(WiFi.macAddress()); Serial.println(WiFi.macAddress());
Serial.println("\n"); Serial.println("\n");
delay(10000); delay(10000);
// DeepSleep::deep_sleep(5); DeepSleep::deep_sleep(5);
} }
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