This article is all about ESP-NOW ESP8266, what is ESP-NOW and how it is useful in exchanging data between Arduino IDE and ESP8266 NodeMCU boards. Keep reading if you want to know about ESP-NOW.Usage of ESP-NOW ESP8266 NodeMCU on Arduino IDE
What is meant by ESP-NOW?
ESP-NOW is used in home automation project and industrial automation. ESP-NOW is a connectionless communication protocol that is defined by Espressif. And it can be used with ESP8266 boards. This is a lo power wireless connectivity and this protocol is used in wireless mouse that’s why it enables multiple devices to communicate with each other even without Wi-Fi and with safe and peer-to-peer connections. Therefore, it means that the connection is persistent and if one of the boards is disconnected it will automatically connect to its peer and continue its communication.
Features of ESP-NOW
ESP-NOW supports the following features:
- Encrypted and unencrypted unicast communication.
- Mixed encrypted and unencrypted peer devices.
- Up to 250-byte payload can be carried.
- The sending callback function that can be set to inform the application layer of transmission success or failure.
- ESP-NOW is versatile, it means one can have one-way or two-way communications within different setups.
Limitations of ESP-NOW
ESP-NOW technology also has the following limitations:
- Broadcast is not supported.
- Payload is limited to 250 bytes.
ESP-NOW ONE-WAY Communication
One ESP8266 board to another ESP8266 board
In one-way communication, one ESP8266 board is sending data to another ESP8266 board. This way of communication is very easy to install and it is great way to send data from one ESP8266 to another ESP8266 board.
One ESP8266 boards is sending data to multiple ESP8266 boards, we can say that on is “master” and all the others are “slaves”. It can control many others boards with a single main ESP8266 board.
Multiple ESP8266 board to one ESP8266
This setup is ideal because in this many boards send data to only one boards. We can say that one is slave and others are master. As shown in the figure:
ESP-NOW Two-Way Communication
In ESP-NOW, boards can transfer data from one board to another board at the same time. So, we can establish a two-way communication. We have two boards for communication.
And if you can more boards in two- way communication, you can easily add and it may be look something like that:
In fact, ESP-NOW is very ideal network and we can connect several ESP8266 boards with each other.
Preparing ESP8266 Board MAC Address
Firs of all we need to know the MAC address to communicate with ESP boards via ESP-NOW. So that we know which device we’ll send the information to. As you know each ESP8266 has unique MAC address.
Upload the code to get MAC address.
Now open serial monitors at baud rate 115200 and press the RESET button on ESP8266 board. You can see the printed MAC address like below:
Then save the MAC address because it helps you in sending data from one board to another board.
ESP-NOW One-way communication to another with ESP8266 board.
Now, this structure of one-way communication contains a variable of type char, float, int and string. Then you can send a structure in whichever variable you can choose.
Suppose we call one board ESP8266#1 and other is ESP8266#2.
These are what we should include in the sender sketch;
- Initialize ESP-NOW
- Register a callback function (OnDataSent) function. It will be executed when the message is sent.
- For MAC address you need to add the Peer device.
- Message to the peer deice.
On the receiver device, what should include in the sketch.
- Initialize ESP-NOW
- Register a callback function (OnDataRecv). It will be executed when the message is received.
- Now save the message into a variable inside that callback to execute any task with that information.
ESP-NOW Useful Function
The most useful ESP-NOW function are:
|Function Name and Description
|esp_now_init() Initializes ESP-NOW. You must initialize Wi-Fi before initializing ESP-NOW. Returns 0, if succeed.
|esp_now_set_self_role(role) the role can be: ESP_NOW_ROLE_IDLE = 0,
ESP_NOW_ROLE_CONTROLLER, ESP_NOW_ROLE_SLAVE, ESP_NOW_ROLE_COMBO,ESP_NOW_ROLE_MAX
esp_now_add_peer(uint8 mac_addr, uint8 role, uint8 channel, uint8 key, uint8 key_len) Call this function to pair a device.
|esp_now_send(uint8 mac_address, uint8 data, int len) Send data with ESP-NOW.
|esp_now_register_send_cb() Register a callback function that is triggered upon sending data. When a message is sent, a function is called – this function returns whether the delivery was successful or not.
|esp_now_register_rcv_cb() Register a callback function that is triggered upon receiving data. When data is received via ESP-NOW, a function is called.
ESP8266 NodeMCU Sender Sketch (ESP-NOW)
Copy the given code n the Arduino IDE but before uploading it make some changes in it.
How the Code Work
First of all, we need to include ESP8266 libraries:
Your Reliable Custom Wiring Harness and Cable Assembly Supplier in China
Getting Started with ESP-NOW ESP8266 with Arduino IDE