{"id":97209,"date":"2020-06-12T10:39:01","date_gmt":"2020-06-12T10:39:01","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=97209"},"modified":"2020-06-12T12:57:07","modified_gmt":"2020-06-12T12:57:07","slug":"esp-now-many-to-one-esp8266-nodemcu","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp-now-many-to-one-esp8266-nodemcu\/","title":{"rendered":"ESP-NOW with ESP8266: Receive Data from Multiple Boards (many-to-one)"},"content":{"rendered":"\n<p>This tutorial shows how to setup an ESP8266 board to receive data from multiple ESP8266 boards via ESP-NOW communication protocol (many-to-one configuration). This configuration is ideal if you want to collect data from several sensors nodes into one ESP8266 board. The boards will be programmed using Arduino IDE.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-ESP8266-NodeMCU-Receive-Data-Multiple-Boards-Many-to-One-Arduino-IDE.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP-NOW with ESP8266 Receive Data from Multiple Boards many-to-one\" class=\"wp-image-97245\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-ESP8266-NodeMCU-Receive-Data-Multiple-Boards-Many-to-One-Arduino-IDE.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-ESP8266-NodeMCU-Receive-Data-Multiple-Boards-Many-to-One-Arduino-IDE.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-ESP8266-NodeMCU-Receive-Data-Multiple-Boards-Many-to-One-Arduino-IDE.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-ESP8266-NodeMCU-Receive-Data-Multiple-Boards-Many-to-One-Arduino-IDE.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>Other ESP-NOW Guides with the ESP8266:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp-now-esp8266-nodemcu-arduino-ide\/\">Getting Started with ESP-NOW (ESP8266 NodeMCU with Arduino IDE)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp-now-two-way-communication-esp8266-nodemcu\/\">ESP-NOW Two-Way Communication Between ESP8266 Boards<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp-now-one-to-many-esp8266-nodemcu\/\">ESP-NOW with ESP8266: Send Data to Multiple Boards (one-to-many)<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>This tutorial shows how to setup an ESP8266 board to receive data from multiple ESP8266 boards via ESP-NOW communication protocol (many-to-one configuration) as shown in the following figure.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"412\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP8266-ESP-NOW-One-Slave-Multiple-Masters.PNG.png?resize=750%2C412&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Project overview ESP-NOW with ESP8266 NodeMCU Receive Data from Multiple Boards many-to-one configuration\" class=\"wp-image-93800\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP8266-ESP-NOW-One-Slave-Multiple-Masters.PNG.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP8266-ESP-NOW-One-Slave-Multiple-Masters.PNG.png?resize=300%2C165&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>One ESP8266 board acts as a receiver\/slave;<\/li><li>Multiple ESP8266 boards act as senders\/masters. As an example, we&#8217;ll use two senders. You should be able to add more boards to your setup;<\/li><li>The sender board receives an acknowledge message indicating if the message was successfully delivered or not;<\/li><li>The ESP8266 receiver board receives the messages from all senders and identifies which board sent the message;<\/li><li>As an example, we\u2019ll exchange random values between the boards. You should modify this example to send commands or sensor readings.<\/li><\/ul>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> in the ESP-NOW documentation there isn\u2019t such thing as \u201csender\/master\u201d and \u201creceiver\/slave\u201d. Every board can be a sender or receiver. However, to keep things clear we\u2019ll use the terms \u201csender\u201d and \u201creceiver\u201d or \u201cmaster\u201d and \u201cslave\u201d.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>We\u2019ll program the ESP8266 boards using Arduino IDE, so before proceeding with this tutorial, make sure you have these boards installed in your Arduino IDE.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\">Installing ESP8266 Board in Arduino IDE (Windows, Mac OS X, and Linux)<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Getting the Receiver Board MAC Address<\/h2>\n\n\n\n<p>To send messages via ESP-NOW, you need to know the receiver board\u2019s&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/get-change-esp32-esp8266-mac-address-arduino\/\">MAC address<\/a>. Each board has a unique MAC address (learn how to&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/get-change-esp32-esp8266-mac-address-arduino\/\">Get and Change the ESP8266 MAC Address<\/a>).<\/p>\n\n\n\n<p>Upload the following code to your ESP8266 receiver board to get is MAC address.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/get-change-esp32-esp8266-mac-address-arduino\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.  \n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*\/\n#ifdef ESP32\n  #include &lt;WiFi.h&gt;\n  #include &lt;esp_wifi.h&gt;\n#else\n  #include &lt;ESP8266WiFi.h&gt;\n#endif\n\nvoid setup(){\n  Serial.begin(115200);\n\n  Serial.print(&quot;ESP Board MAC Address: &quot;);\n  #ifdef ESP32\n    WiFi.mode(WIFI_STA);\n    WiFi.STA.begin();\n    uint8_t baseMac[6];\n    esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);\n    if (ret == ESP_OK) {\n      Serial.printf(&quot;%02x:%02x:%02x:%02x:%02x:%02x\\n&quot;,\n                    baseMac[0], baseMac[1], baseMac[2],\n                    baseMac[3], baseMac[4], baseMac[5]);\n    } else {\n      Serial.println(&quot;Failed to read MAC address&quot;);\n    }\n  #else\n    Serial.println(WiFi.macAddress());\n  #endif\n}\n \nvoid loop(){\n\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_Get_MAC_Address.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>After uploading the code, press the RST\/EN button, and the MAC address should be displayed on the Serial Monitor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"666\" height=\"320\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/Get-ESP8266-MAC-Address-Serial-Monitor.jpg?resize=666%2C320&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Getting the ESP8266 NodeMCU Receiver Board MAC Address\" class=\"wp-image-97196\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/Get-ESP8266-MAC-Address-Serial-Monitor.jpg?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/Get-ESP8266-MAC-Address-Serial-Monitor.jpg?resize=300%2C144&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">ESP8266 NodeMCU Sender Code (ESP-NOW)<\/h2>\n\n\n\n<p>The receiver can identify each sender by its unique MAC address. However, dealing with different MAC addresses on the Receiver side to identify which board sent which message can be a little tricky.<\/p>\n\n\n\n<p>So, to make things easier, we\u2019ll identify each board with a unique number (<span class=\"rnthl rntliteral\">id<\/span>) that starts at 1. If you have three boards, one will have ID number 1, the other number 2, and finally number 3. The ID will be sent to the receiver alongside the other variables.<\/p>\n\n\n\n<p>As an example, we\u2019ll exchange a structure that contains the board <span class=\"rnthl rntliteral\">id<\/span> number and two random numbers <span class=\"rnthl rntliteral\">x<\/span> and <span class=\"rnthl rntliteral\">y<\/span> as shown in the figure below.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"933\" height=\"543\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-Many-to-one-ESP8266.png?resize=933%2C543&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP-NOW with ESP32 Receive Data from Multiple Boards (many-to-one) Sample Data\" class=\"wp-image-97228\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-Many-to-one-ESP8266.png?w=933&amp;quality=100&amp;strip=all&amp;ssl=1 933w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-Many-to-one-ESP8266.png?resize=300%2C175&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-Many-to-one-ESP8266.png?resize=768%2C447&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 933px) 100vw, 933px\" \/><\/figure><\/div>\n\n\n\n<p>Upload the following code to each of your sender boards. Don\u2019t forget to increment the <span class=\"rnthl rntliteral\">id<\/span> number for each sender board.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp-now-many-to-one-esp8266-nodemcu\/\n  \n  Permission is hereby granted, free of charge, to any person obtaining a copy\n  of this software and associated documentation files.\n  \n  The above copyright notice and this permission notice shall be included in all\n  copies or substantial portions of the Software.\n*\/\n\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;espnow.h&gt;\n\n\/\/ REPLACE WITH RECEIVER MAC Address\nuint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\n\n\/\/ Set your Board ID (ESP32 Sender #1 = BOARD_ID 1, ESP32 Sender #2 = BOARD_ID 2, etc)\n#define BOARD_ID 2\n\n\/\/ Structure example to send data\n\/\/ Must match the receiver structure\ntypedef struct struct_message {\n    int id;\n    int x;\n    int y;\n} struct_message;\n\n\/\/ Create a struct_message called test to store variables to be sent\nstruct_message myData;\n\nunsigned long lastTime = 0;\nunsigned long timerDelay = 10000;\n\n\/\/ Callback when data is sent\nvoid OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {\n  Serial.print(&quot;\\r\\nLast Packet Send Status: &quot;);\n  if (sendStatus == 0){\n    Serial.println(&quot;Delivery success&quot;);\n  }\n  else{\n    Serial.println(&quot;Delivery fail&quot;);\n  }\n}\n \nvoid setup() {\n  \/\/ Init Serial Monitor\n  Serial.begin(115200);\n \n  \/\/ Set device as a Wi-Fi Station\n  WiFi.mode(WIFI_STA);\n  WiFi.disconnect();\n\n  \/\/ Init ESP-NOW\n  if (esp_now_init() != 0) {\n    Serial.println(&quot;Error initializing ESP-NOW&quot;);\n    return;\n  } \n  \/\/ Set ESP-NOW role\n  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);\n\n  \/\/ Once ESPNow is successfully init, we will register for Send CB to\n  \/\/ get the status of Trasnmitted packet\n  esp_now_register_send_cb(OnDataSent);\n  \n  \/\/ Register peer\n  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);\n\n}\n \nvoid loop() {\n  if ((millis() - lastTime) &gt; timerDelay) {\n    \/\/ Set values to send\n    myData.id = BOARD_ID;\n    myData.x = random(1, 50);\n    myData.y = random(1, 50);\n\n    \/\/ Send message via ESP-NOW\n    esp_now_send(0, (uint8_t *) &amp;myData, sizeof(myData));\n    lastTime = millis();\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/ESP_NOW\/ESP_NOW_Sender_Many_to_One.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Include the&nbsp;<span class=\"rnthl rntliteral\">ESP8266WiFi<\/span> and&nbsp;<span class=\"rnthl rntliteral\">espnow<\/span>&nbsp;libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;ESP8266WiFi.h>\n#include &lt;espnow.h><\/code><\/pre>\n\n\n\n<p>Insert the receiver\u2019s MAC address on the following line.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>uint8_t broadcastAddress&#091;] = {0x5C, 0xCF, 0x7F, 0x99, 0xA1, 0x70};<\/code><\/pre>\n\n\n\n<p>Set the board ID on the <span class=\"rnthl rntliteral\">BOARD_ID<\/span> variable. Don&#8217;t forget to change this variable for each of your sender boards.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Set your Board ID (ESP8266 Sender #1 = BOARD_ID 1, ESP8266 Sender #2 = BOARD_ID 2, etc)\n#define BOARD_ID 2<\/code><\/pre>\n\n\n\n<p>Then, create a structure that contains the data we want to send. We called this structure <span class=\"rnthl rntliteral\">struct_message<\/span> and it contains three integer variables: the board <span class=\"rnthl rntliteral\">id<\/span>, <span class=\"rnthl rntliteral\">x<\/span> and <span class=\"rnthl rntliteral\">y<\/span>. You can change this to send whatever variable types you want (but don\u2019t forget to change that on the receiver side too).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>typedef struct struct_message {\n    int id;\n    int x;\n    int y;\n} struct_message;<\/code><\/pre>\n\n\n\n<p>Create a new variable of type <span class=\"rnthl rntliteral\">struct_message<\/span> that is called <span class=\"rnthl rntliteral\">myData<\/span> that will store the variables values.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>struct_message myData;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">OnDataSent() callback function<\/h4>\n\n\n\n<p>Next, define the <span class=\"rnthl rntliteral\">OnDataSent()<\/span> function. This is a callback function that will be executed when a message is sent. In this case, this function prints if the message was successfully delivered or not.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {\n  Serial.print(\"\\r\\nLast Packet Send Status: \");\n  if (sendStatus == 0){\n    Serial.println(\"Delivery success\");\n  }\n  else{\n    Serial.println(\"Delivery fail\");\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the serial monitor for debugging purposes:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Set the device as a Wi-Fi station and disconnect Wi-Fi:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFi.mode(WIFI_STA);\nWiFi.disconnect();<\/code><\/pre>\n\n\n\n<p>Initialize ESP-NOW:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (esp_now_init() != 0) {\n  Serial.println(\"Error initializing ESP-NOW\");\n  return;\n}<\/code><\/pre>\n\n\n\n<p>Set the board role. This is a sender board, so we&#8217;ll set it to <span class=\"rnthl rntliteral\">ESP_NOW_ROLE_CONTROLLER<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);<\/code><\/pre>\n\n\n\n<p>After successfully initializing ESP-NOW, register the callback function that will be called when a message is sent. In this case, register for the <span class=\"rnthl rntliteral\">OnDataSent()<\/span> function created previously.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_now_register_send_cb(OnDataSent);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Add peer device<\/h4>\n\n\n\n<p>To send data to another board (the receiver), you need to pair it as a peer. The following line registers a new peer.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);<\/code><\/pre>\n\n\n\n<p>The&nbsp;<span class=\"rnthl rntliteral\">esp_now_add_peer()<\/span>&nbsp;function&nbsp;accepts the following arguments, in this order: mac address, peer role, wi-fi channel, key, and key length.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we\u2019ll send a message via ESP-NOW every 10 seconds (you can change this delay time in the <span class=\"rnthl rntliteral\">timerDelay<\/span> variable).<\/p>\n\n\n\n<p>Assign a value to each variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>myData.id = BOARD_ID;\nmyData.x = random(1, 50);\nmyData.y = random(1, 50);<\/code><\/pre>\n\n\n\n<p><strong>Don\u2019t forget to change the id for each sender board.<\/strong><\/p>\n\n\n\n<p>Remember that <span class=\"rnthl rntliteral\">myData<\/span> is a structure. Here assign the values that you want to send inside the structure. In this case, we\u2019re just sending the <span class=\"rnthl rntliteral\">id<\/span> and random values <span class=\"rnthl rntliteral\">x<\/span> and <span class=\"rnthl rntliteral\">y<\/span>. In a practical application these should be replaced with commands or sensor readings, for example.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Send ESP-NOW message<\/h4>\n\n\n\n<p>Finally, send the message via ESP-NOW.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_now_send(0, (uint8_t *) &amp;myData, sizeof(myData));<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">ESP8266 NodeMCU Receiver Code (ESP-NOW)<\/h2>\n\n\n\n<p>Upload the following code to your ESP8266 receiver board. The code is prepared to receive data from two different boards. You can easily modify the code to receive data from a different number of boards.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp-now-many-to-one-esp8266-nodemcu\/\n  \n  Permission is hereby granted, free of charge, to any person obtaining a copy\n  of this software and associated documentation files.\n  \n  The above copyright notice and this permission notice shall be included in all\n  copies or substantial portions of the Software.\n*\/\n\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;espnow.h&gt;\n\n\/\/ Structure example to receive data\n\/\/ Must match the sender structure\ntypedef struct struct_message {\n    int id;\n    int x;\n    int y;\n} struct_message;\n\n\/\/ Create a struct_message called myData\nstruct_message myData;\n\n\/\/ Create a structure to hold the readings from each board\nstruct_message board1;\nstruct_message board2;\n\n\/\/ Create an array with all the structures\nstruct_message boardsStruct[2] = {board1, board2};\n\n\/\/ Callback function that will be executed when data is received\nvoid OnDataRecv(uint8_t * mac_addr, uint8_t *incomingData, uint8_t len) {\n  char macStr[18];\n  Serial.print(&quot;Packet received from: &quot;);\n  snprintf(macStr, sizeof(macStr), &quot;%02x:%02x:%02x:%02x:%02x:%02x&quot;,\n           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);\n  Serial.println(macStr);\n  memcpy(&amp;myData, incomingData, sizeof(myData));\n  Serial.printf(&quot;Board ID %u: %u bytes\\n&quot;, myData.id, len);\n  \/\/ Update the structures with the new incoming data\n  boardsStruct[myData.id-1].x = myData.x;\n  boardsStruct[myData.id-1].y = myData.y;\n  Serial.printf(&quot;x value: %d \\n&quot;, boardsStruct[myData.id-1].x);\n  Serial.printf(&quot;y value: %d \\n&quot;, boardsStruct[myData.id-1].y);\n  Serial.println();\n}\n \nvoid setup() {\n  \/\/ Initialize Serial Monitor\n  Serial.begin(115200);\n  \n  \/\/ Set device as a Wi-Fi Station\n  WiFi.mode(WIFI_STA);\n  WiFi.disconnect();\n\n  \/\/ Init ESP-NOW\n  if (esp_now_init() != 0) {\n    Serial.println(&quot;Error initializing ESP-NOW&quot;);\n    return;\n  }\n  \n  \/\/ Once ESPNow is successfully Init, we will register for recv CB to\n  \/\/ get recv packer info\n  esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);\n  esp_now_register_recv_cb(OnDataRecv);\n}\n\nvoid loop(){\n  \/\/ Access the variables for each board\n  \/*int board1X = boardsStruct[0].x;\n  int board1Y = boardsStruct[0].y;\n  int board2X = boardsStruct[1].x;\n  int board2Y = boardsStruct[1].y;\n  *\/\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/ESP_NOW\/ESP_NOW_Receiver_Many_to_One.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Similarly to the sender, start by including the libraries:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;ESP8266WiFi.h>\n#include &lt;espnow.h><\/code><\/pre>\n\n\n\n<p>Create a structure to receive the data. This structure should be the same defined in the sender sketch.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>typedef struct struct_message {\n    int id;\n    int x;\n    int y;\n} struct_message;<\/code><\/pre>\n\n\n\n<p>Create a <span class=\"rnthl rntliteral\">struct_message<\/span> variable called <span class=\"rnthl rntliteral\">myData<\/span> that will hold the data received.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>struct_message myData;<\/code><\/pre>\n\n\n\n<p>Then, create a <span class=\"rnthl rntliteral\">struct_message<\/span> variable for each board, so that we can assign the received data to the corresponding board. Here we\u2019re creating structures for two sender boards. If you have more sender boards, you need to create more structures.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>struct_message board1;\nstruct_message board2;<\/code><\/pre>\n\n\n\n<p>Create an array that contains all the board structures. If you\u2019re using a different number of boards you need to change that.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>struct_message boardsStruct&#091;2] = {board1, board2};<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">onDataRecv()<\/h4>\n\n\n\n<p>Create a callback function that is called when the ESP8266 receives the data via ESP-NOW. The function is called&nbsp;<span class=\"rnthl rntliteral\">onDataRecv()<\/span>&nbsp;and should accept several parameters as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void OnDataRecv(uint8_t * mac_addr, uint8_t *incomingData, uint8_t len) {<\/code><\/pre>\n\n\n\n<p>Get the sender board&#8217;s MAC address:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(\"Packet received from: \");\nsnprintf(macStr, sizeof(macStr), \"%02x:%02x:%02x:%02x:%02x:%02x\",\n         mac_addr&#091;0], mac_addr&#091;1], mac_addr&#091;2], mac_addr&#091;3], mac_addr&#091;4], mac_addr&#091;5]);\nSerial.println(macStr);<\/code><\/pre>\n\n\n\n<p>Copy the content of the <span class=\"rnthl rntliteral\">incomingData<\/span> data variable into the <span class=\"rnthl rntliteral\">myData<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>memcpy(&amp;myData, incomingData, sizeof(myData));<\/code><\/pre>\n\n\n\n<p>Now, the <span class=\"rnthl rntliteral\">myData<\/span> structure contains several variables with the values sent by one of the ESP8266 senders. We can identify which board send the packet by its ID: <span class=\"rnthl rntliteral\">myData.id<\/span>.<\/p>\n\n\n\n<p>This way, we can assign the values received to the corresponding boards on the <span class=\"rnthl rntliteral\">boardsStruct<\/span> array:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>boardsStruct&#091;myData.id-1].x = myData.x;\nboardsStruct&#091;myData.id-1].y = myData.y;<\/code><\/pre>\n\n\n\n<p>For example, imagine you receive a packet from board with id 2. The value of <span class=\"rnthl rntliteral\">myData.id<\/span> is 2.<\/p>\n\n\n\n<p>So, you want to update the values of the <span class=\"rnthl rntliteral\">board2<\/span> structure. The <span class=\"rnthl rntliteral\">board2<\/span> structure is the element with index 1 on the <span class=\"rnthl rntliteral\">boardsStruct<\/span> array. That\u2019s why we subtract 1, because arrays in C have 0 indexing. It may help if you take a look at the following image.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"482\" height=\"271\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-Many-to-one-ESP8266-structure-array.png?resize=482%2C271&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP-NOW ESP8266 NodeMCU data structure\" class=\"wp-image-97232\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-Many-to-one-ESP8266-structure-array.png?w=482&amp;quality=100&amp;strip=all&amp;ssl=1 482w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-Many-to-one-ESP8266-structure-array.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 482px) 100vw, 482px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">setup()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Set the device as a Wi-Fi Station and disconnect Wi-Fi.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFi.mode(WIFI_STA);\nWiFi.disconnect();<\/code><\/pre>\n\n\n\n<p>Initialize ESP-NOW:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (esp_now_init() != ESP_OK) {\n  Serial.println(\"Error initializing ESP-NOW\");\n  return;\n}<\/code><\/pre>\n\n\n\n<p>Set the board role. This is a receiver board, so we&#8217;ll set it to <span class=\"rnthl rntliteral\">ESP_NOW_ROLE_SLAVE<\/span>.<\/p>\n\n\n\n<p>Register for a callback function that will be called when data is received. In this case, we register for the <span class=\"rnthl rntliteral\">OnDataRecv()<\/span> function that was created previously.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_now_register_recv_cb(OnDataRecv);<\/code><\/pre>\n\n\n\n<p>The following lines commented on the loop exemplify what you need to do if you want to access the variables of each board structure. For example, to access the <span class=\"rnthl rntliteral\">x<\/span> value of <span class=\"rnthl rntliteral\">board1<\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int board1X = boardsStruct&#091;0].x;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Upload the sender code to each of your sender boards. Don\u2019t forget to give a different ID to each board.<\/p>\n\n\n\n<p>Upload the receiver code to the ESP8266 receiver board. Don\u2019t forget to modify the structure to match the number of sender boards.<\/p>\n\n\n\n<p>On the senders\u2019 Serial Monitor, you should get a \u201cDelivery Success\u201d message if the messages are delivered successfully.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"652\" height=\"231\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP8266-ESP-NOW-delivery-success-serial-monitor.png?resize=652%2C231&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP-NOW Send Data from Multiple Boards to one ESP8266 NodeMCU Receiver Serial Monitor Arduino IDE\" class=\"wp-image-97236\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP8266-ESP-NOW-delivery-success-serial-monitor.png?w=652&amp;quality=100&amp;strip=all&amp;ssl=1 652w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP8266-ESP-NOW-delivery-success-serial-monitor.png?resize=300%2C106&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/figure><\/div>\n\n\n\n<p>On the receiver board, you should be receiving the packets from all the other boards. In this test, we were receiving data from 2 different boards.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"849\" height=\"589\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-ESP8266-Receive-from-Many-ESP8266.jpg?resize=849%2C589&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP-NOW Receive Data from Multiple Boards ESP8266 NodeMCU Arduino IDE Serial Monitor\" class=\"wp-image-97233\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-ESP8266-Receive-from-Many-ESP8266.jpg?w=849&amp;quality=100&amp;strip=all&amp;ssl=1 849w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-ESP8266-Receive-from-Many-ESP8266.jpg?resize=300%2C208&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-ESP8266-Receive-from-Many-ESP8266.jpg?resize=768%2C533&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial you\u2019ve learned how setup an ESP8266 to receive data from multiple ESP8266 boards using ESP-NOW (many-to-one configuration). You can do something similar with ESP32 boards (<a href=\"https:\/\/randomnerdtutorials.com\/esp-now-many-to-one-esp32\/\">ESP32 ESP-NOW: Many-to-one<\/a>).<\/p>\n\n\n\n<p>As an example, we\u2019ve exchanged random numbers. In a practical application those should be replaced with actual sensor readings or commands. This is ideal if you want to collect data from several sensor nodes.<\/p>\n\n\n\n<p>We have other tutorials related with ESP-NOW that you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp-now-esp8266-nodemcu-arduino-ide\/\"><a href=\"https:\/\/randomnerdtutorials.com\/esp-now-esp8266-nodemcu-arduino-ide\/\">Getting Started with ESP-NOW (ESP8266 NodeMCU with Arduino IDE)<\/a><\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp-now-esp8266-nodemcu-arduino-ide\/\"><a href=\"https:\/\/randomnerdtutorials.com\/esp-now-two-way-communication-esp8266-nodemcu\/\">ESP-NOW Two-Way Communication Between ESP8266 Boards<\/a><\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp-now-esp8266-nodemcu-arduino-ide\/\"><a href=\"https:\/\/randomnerdtutorials.com\/esp-now-one-to-many-esp8266-nodemcu\/\">ESP-NOW with ESP8266: Send Data to Multiple Boards (one-to-many)<\/a><\/a><\/li><\/ul>\n\n\n\n<p>Learn more about ESP8266 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">More ESP8266 NodeMCU Projects and Tutorials&#8230;<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to setup an ESP8266 board to receive data from multiple ESP8266 boards via ESP-NOW communication protocol (many-to-one configuration). This configuration is ideal if you want to &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP-NOW with ESP8266: Receive Data from Multiple Boards (many-to-one)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp-now-many-to-one-esp8266-nodemcu\/#more-97209\" aria-label=\"Read more about ESP-NOW with ESP8266: Receive Data from Multiple Boards (many-to-one)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":97245,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[214,265,246,240,300,264],"tags":[],"class_list":["post-97209","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266","category-esp8266-project","category-esp8266-arduino-ide","category-esp8266-projects","category-0-esp8266","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/05\/ESP-NOW-ESP8266-NodeMCU-Receive-Data-Multiple-Boards-Many-to-One-Arduino-IDE.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/97209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=97209"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/97209\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/97245"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=97209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=97209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=97209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}