esp-idf
esp-idf copied to clipboard
Implementing Mesh Network with ESPNOW in IDF Example (IDFGH-14842)
Is your feature request related to a problem?
Hi,
I am currently using the ESPNOW code from the IDF example and want to implement a mesh network scenario. In this setup, I have three devices: Slave 1, Slave 2, and Master. The issue is that Slave 2 is out of range from the Master, but in range with Slave 1.
My goal is to have Slave 2 send a message through Slave 1 to the Master, so that even though Slave 2 is out of range from the Master, it can still communicate via Slave 1.
Could you please guide me on how to modify the code to implement this functionality, where Slave 1 forwards the message from Slave 2 to the Master?
Thanks in advance for your assistance!
Describe the solution you'd like.
No response
Describe alternatives you've considered.
No response
Additional context.
No response
Here is the esp-mesh-lite user guide. This includes the ESPNOW Usage Guide. Perhaps it is possible with esp-mesh-lite. Unfortunately, it does not include sample code for ESPNOW.
https://github.com/espressif/esp-mesh-lite/blob/master/components/mesh_lite/User_Guide.md#espnow-usage-guide
Thanks for your response,
I am able to do mesh network using above code.
Now i want to use API of ESPNOW
receive api for master :
/ Callback function to handle received data void recv_cb(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
ESP_LOGI(TAG, "Received from %02X:%02X:%02X:%02X:%02X:%02X: %.*s",
info->src_addr[0], info->src_addr[1], info->src_addr[2],
info->src_addr[3], info->src_addr[4], info->src_addr[5],
len, data);
// Ensure slave is added as a peer
add_peer_if_needed(info->src_addr);
}
Send api slave 1 & slave 2:
// Function to send data (MAC + Device Number) void send_data_to_master(void *pvParameter) { uint8_t device_mac[ESP_NOW_ETH_ALEN]; esp_wifi_get_mac(ESP_IF_WIFI_STA, device_mac);
while (1) {
// Prepare data (MAC + device number)
char data[120];
int written_bytes = snprintf(data, sizeof(data), "\nDevice No: %d \nMAC: %02X:%02X:%02X:%02X:%02X:%02X\nData : %s:%d\n" ,
DEVICE_NUMBER,
device_mac[0], device_mac[1], device_mac[2],
device_mac[3], device_mac[4], device_mac[5],"Testing 100 bytes data slave device No",DEVICE_NUMBER);
uint8_t actual_data_length = strlen(data);
snprintf(data + written_bytes, sizeof(data) - written_bytes, "Data written: %d\n", actual_data_length);
// Send data to master
esp_err_t result = esp_now_send(master_mac, (uint8_t *)data, strlen(data));
if (result == ESP_OK) {
ESP_LOGI(TAG, "Data sent: %s", data);
} else {
ESP_LOGE(TAG, "Failed to send data");
}
vTaskDelay(pdMS_TO_TICKS(2000)); // Delay for 2 seconds
}
}
Is there an available example?
The mesh structure should be followed for sending messages through the slave device if any slave is out of range.
Thanks in advance.
@gajananmaske1234 you can refer to this : https://github.com/espressif/esp-mesh-lite/blob/master/examples/rainmaker/led_light/components/group_control/app_espnow.c
Thanks for your response,
As shown in above diagram I am using mesh lite example and espnow and send receive API.
Now using esp lite example i am expecting master to layer 2 device should communicate through Slave 1 device but not working as expected.
slave to connecting with slave1 as a child confirmed in logs.
Want to know can i do communication as above if yes then how? is there any example please guide.
@gajananmaske1234 Do you mean that you’ve already established the mesh network as shown in the diagram above, and you want to send ESPNOW data from slave3 to the master? Your expected behavior is that slave3 first sends the data to slave1, and then slave1 forwards it to the master?
ESPNOW is essentially a point-to-point communication protocol. Once two nodes have added each other as peers, they can send and receive data directly. So, if you want slave3 to send data to the master, it’s sufficient for them to be added as peers.
However, if you require the data to be forwarded through slave1, you’ll need to implement this logic at the application level yourself. For example, slave3 can include a flag in the data packet to indicate that the data is intended for the master. Slave3 would send the packet to slave1, and upon receiving it, slave1 would parse the flag and then forward the ESPNOW data to the master.
@zhangyanjiaoesp
However, if you require the data to be forwarded through slave1, you’ll need to implement this logic at the application level yourself.
Slave1 can use WiFi to get the MAC addresses of Master and Slave3, but in that case, there is no INTERFACE that ESPNOW can use because both the STA-INTERFACE and SoftAP-INTERFACE are in use by WiFi.
@nopnop2002 Even though the STA and softAP interfaces have already established Wi-Fi connections, ESPNOW can still use them to add peers and send packets. I don't quite understand why you said there are no interfaces available for ESPNOW.
ESPNOW can still use them to add peers and send packets.
Oh!! I didn't know. Thank you.