diff --git a/client/client/lib/espnow/src/ESPNow.cpp b/client/client/lib/espnow/src/ESPNow.cpp index 11dafda7be07016e2f8a3ab10f362105eebdf20c..200c1b47846a006fe8af4335f6b4d18277dcb260 100644 --- a/client/client/lib/espnow/src/ESPNow.cpp +++ b/client/client/lib/espnow/src/ESPNow.cpp @@ -13,12 +13,18 @@ void get_host_mac(uint8_t *destination) preferences.getBytes("host", destination, sizeof(uint8_t) * 6); } else { memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC)); - ESP_LOGW(TAG, "Backup MAC address used"); + ESP_LOGE(TAG, "Backup MAC address used"); } preferences.end(); } void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) { + ESP_LOGE(TAG, "Message sent to"); + for(int i=0; i<6; i++){ + Serial.print(mac_addr[i], HEX); + Serial.print(":"); + } + Serial.println(); // go to sleep } @@ -33,11 +39,22 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) if (!preferences.isKey("host")) { preferences.putBytes("host", new_config.host, sizeof(new_config.host)); ESP_LOGI(TAG, "host MAC address saved to flash"); + // add host to peers + esp_now_peer_info_t host; + memset(&host, 0, sizeof(host)); + memcpy(host.peer_addr, new_config.host, sizeof(new_config.host)); + host.encrypt = false; + host.channel = 0; + if(esp_now_add_peer(&host) != ESP_OK){ + ESP_LOGE(TAG, "Adding host to peers failed"); + } } // host change shouldn't be an issue preferences.end(); // sync time Time::getInstance().setTime( new_config.time_millis); // see https://www.esp32.com/viewtopic.php?t=9965, maybe this needs an offset + Serial.println("received time: " + (String) new_config.time_millis); + Serial.println("new time: " + (String) Time::getInstance().getMillis()); } esp_err_t espnow_setup() diff --git a/client/client/lib/espnow/src/Message.cpp b/client/client/lib/espnow/src/Message.cpp index 7de1fc9d38ed4ea322994c52bb6a608ff74d332a..bd1e08a62901746d43300c4f45ff98514e687f8f 100644 --- a/client/client/lib/espnow/src/Message.cpp +++ b/client/client/lib/espnow/src/Message.cpp @@ -14,6 +14,7 @@ esp_err_t Message::send() const success = esp_now_send(recipient, (uint8_t *)messageData.c_str(), (messageData.length() + 1) * sizeof(char)); if (success != ESP_OK) { ESP_LOGE(TAG, "Error sending the data"); + Serial.println(success, HEX); // TODO REWRITE FOR JSON // if (!ram_cache_is_full()) { // // ram_cache_push(messageData); diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp index bf964c0c5c0280b41dc8e0a514f76408276314d0..1f6a5e2844fd51fe51072d99ce031fe5747fe203 100644 --- a/client/client/src/main.cpp +++ b/client/client/src/main.cpp @@ -36,9 +36,12 @@ void loop() std::cerr << e.what() << '\n'; } - ESP_LOGE(TAG.c_str(), "Sensor Circumference: "); + // ESP_LOGE(TAG.c_str(), "Sensor Circumference: "); // log_e("Temperature: "); // log_e("Id: "); + Serial.print("This device: "); + Serial.println(WiFi.macAddress()); + Serial.println("\n"); delay(5000); } diff --git a/host/esp32-espnow-recv/src/main.cpp b/host/esp32-espnow-recv/src/main.cpp index 62893ed9eccd49d909034cbaa51324aa70131525..b9a1f093b2a4b332970a03f4b584af5f960f3b49 100644 --- a/host/esp32-espnow-recv/src/main.cpp +++ b/host/esp32-espnow-recv/src/main.cpp @@ -9,8 +9,24 @@ #define RXD2 18 #define TXD2 19 +long time1 = 0; + //SoftwareSerial mySerial(RXD2, TXD2); +typedef struct response{ + uint8_t mac[6]; + long time; +}response; + +void print_mac(const uint8_t *mac){ + for (int i = 0; i < 6; i++) { + Serial.print(mac[i], HEX); + Serial.print(":"); + } + Serial.println(); + Serial.flush(); +} + void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) { // go to sleep @@ -19,17 +35,44 @@ 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) { // print mac - Serial.println("Message recieved"); - for (int i = 0; i < 6; i++) { - Serial.print(mac[i], HEX); - Serial.print(":"); - } - Serial.println(); + Serial.print("Message recieved from: "); + print_mac(mac); char data[len]; memcpy(data, incomingData, len); Serial.println(data); Serial1.write(data); + + // add client if not already peer + if(!esp_now_is_peer_exist(mac)){ + Serial.println("adding client to peers"); + + esp_now_peer_info_t client = {}; + memcpy(client.peer_addr, mac, 6); + client.encrypt = false; + client.channel = 0; + + esp_err_t status = esp_now_add_peer(&client); + if(status != ESP_OK){ + Serial.print("Failed to add peer: "); + Serial.println(status, HEX); + } + Serial.flush(); + } + + // respond with own mac + time + response response; + esp_read_mac(response.mac, ESP_MAC_WIFI_STA); + response.time = 7 * time1++; + esp_err_t success = esp_now_send(mac, (uint8_t *) &response, sizeof(response)); + if(success != ESP_OK){ + Serial.print("Failed to respond: "); + Serial.println(success, HEX); + } + else{ + Serial.println("Response sent: " + (String) response.time); + } + Serial.println("\n"); } void setup() @@ -51,4 +94,8 @@ void setup() } void loop() { + Serial.print("This device: "); + Serial.println(WiFi.macAddress()); + Serial.println("\n"); + delay(1000000); } \ No newline at end of file