diff --git a/src/fdrs_gateway_espnow.h b/src/fdrs_gateway_espnow.h index e9f30d0..3c06018 100644 --- a/src/fdrs_gateway_espnow.h +++ b/src/fdrs_gateway_espnow.h @@ -14,7 +14,7 @@ const uint8_t espnow_size = 250 / sizeof(DataReading); #ifdef ESP32 esp_now_peer_info_t peerInfo; #endif - +bool esp_now_sent_flag; const uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; const uint8_t mac_prefix[] = {MAC_PREFIX}; @@ -29,12 +29,14 @@ uint8_t ESPNOW2[] = {MAC_PREFIX, ESPNOW_NEIGHBOR_2}; #if defined(ESP8266) void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) { + esp_now_sent_flag = true; } void OnDataRecv(uint8_t *mac, uint8_t *incomingData, uint8_t len) { #elif defined(ESP32) void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { + esp_now_sent_flag = true; } void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) { @@ -296,6 +298,8 @@ void sendESPNowNbr(uint8_t interface) } } + + void sendESPNowPeers() { DBG("Sending to ESP-NOW peers."); @@ -311,9 +315,25 @@ void sendESPNowPeers() thePacket[j] = theData[i]; j++; } - esp_now_send(0, (uint8_t *)&thePacket, j * sizeof(DataReading)); + for (int i = 0; i < 16; i++) + { + if (peer_list[i].last_seen != 0 && (millis() - peer_list[i].last_seen) < PEER_TIMEOUT) + { + //uint32_t clktm = millis(); + esp_now_sent_flag = false; + esp_now_send(peer_list[i].mac, (uint8_t *)&thePacket, j * sizeof(DataReading)); + while (!esp_now_sent_flag) yield(); + //DBG(millis() - clktm); + } + } + + } + + + + void sendESPNow(uint8_t address) { DBG("Sending ESP-NOW."); diff --git a/src/fdrs_node.h b/src/fdrs_node.h index 3d32070..166b4bf 100644 --- a/src/fdrs_node.h +++ b/src/fdrs_node.h @@ -271,15 +271,15 @@ void loopFDRS() #ifdef USE_LORA handleLoRa(); #endif +if (is_controller){ handleIncoming(); - - if (is_added) - { +#ifdef USE_ESPNOW if ((millis() - last_refresh) >= gtwy_timeout) { refresh_registration(); last_refresh = millis(); } +#endif } } diff --git a/src/fdrs_node_espnow.h b/src/fdrs_node_espnow.h index e56722a..6aeabcc 100644 --- a/src/fdrs_node_espnow.h +++ b/src/fdrs_node_espnow.h @@ -13,7 +13,7 @@ bool is_added = false; bool pingFlag = false; uint32_t last_refresh = 0; uint32_t gtwy_timeout = 300000; - +bool is_controller = false; // Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32 #if defined(ESP8266) void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) @@ -95,7 +95,7 @@ bool refresh_registration() #ifdef USE_ESPNOW SystemPacket sys_packet = {.cmd = cmd_add, .param = 0}; esp_now_send(gatewayAddress, (uint8_t *)&sys_packet, sizeof(SystemPacket)); - DBG("ESP-NOW peer registration request submitted to " + String(gatewayAddress[5])); + DBG("Refreshing registration to " + String(gatewayAddress[5])); uint32_t add_start = millis(); is_added = false; while ((millis() - add_start) <= 1000) // 1000ms timeout @@ -116,6 +116,7 @@ bool refresh_registration() bool addFDRS(void (*new_cb_ptr)(DataReading)) { + is_controller = true; callback_ptr = new_cb_ptr; #ifdef USE_ESPNOW SystemPacket sys_packet = {.cmd = cmd_add, .param = 0}; @@ -141,6 +142,7 @@ bool addFDRS(void (*new_cb_ptr)(DataReading)) bool addFDRS(int timeout, void (*new_cb_ptr)(DataReading)) { + is_controller = true; callback_ptr = new_cb_ptr; #ifdef USE_ESPNOW SystemPacket sys_packet = {.cmd = cmd_add, .param = 0};