In this article, we will see how we can use ESP-NOW to establish Two-Way communication between two ESP8266 NodeMCU boards. ESP-NOW is a wireless communication protocol developed by Espressif Systems, it is designed to achieve efficiency with low power consumption.
ESP-NOW Two-Way-Communication with ESP8266
Let's build a simple project to send and receive data, for better understanding, we will call both NodeMCU as NodeMCU#1 and NodeMCU#2, where we will connect 2 push buttons to NodeMCU#1, and connect 2 led's and 1 DHT 11 sensor to NodeMCU#2. after setting up both of our boards we will be able to control led's connected to the NodeMCU#2 board by pressing push buttons connected to the NodeMCU#1 board. and also visualize the DHT data(DHT connected to NodeMCU#2) on NodeMCU#1s Serial Monitor.
According to this diagram, Push Buttons are connected to NodeMCU#1 boards D0 and D4 pin.
LEDs Connected to NodeMCU#2 boards D3 and D8 pin. DHT 11 sensor connected to D7 pin.
Now to initiate Two-Way Communication we need the MAC Addresses of both of the NodeMCU Boards.
Get the MacAddress of the NodeMCU Board.
Upload this code to get the MAC Address of your board.
After uploading the code to both NodeMCU boards by selecting the respective COM port, go to Serial Monitor set a baud rate of 115200, and press the RST button of NodeMCU once. You will get the MAC Address of your NodeMCU board as Follows.
MAC Address of NodeMCU#1
MAC Address of NodeMCU#2
After getting the MAC Addresses of both of the NodeMCU boards, we will program both NodeMCU Boards to establish ESP-NOW Two-Way Communication.
Let's discuss some key points of both sketch
We will upload the sketch to both of our NodeMCU boards
NodeMCU#1
Initialize ESP NOW.
Register a callback function onDataSent() function to confirm message delivery.
Register a callback function onDataRcev() function to run when receiving the message.
Add the NodeMCU#2 as a peer on the NodeMCU#1 board.
Send and receive messages.
NodeMCU#2
Initialize ESP NOW.
Register a callback function onDataSent() function to confirm message delivery.
Register a callback function onDataRcev() function to run when receiving the message.
Add the NodeMCU#1 as a peer on the NodeMCU#2 board.
Send and receive messages.
ESP8266 NodeMCU#1 Sketch
Make sure to replace the MAC Address of NodeMCU#2 in the sketch
ESP8266 NodeMCU#2 Sketch
Make sure to replace the MAC Address of NodeMCU#1 in the sketch
Now Let's test the communication
After uploading the sketch to both boards and opening the serial monitor for both the transmitter and receiver board, you should see different COM ports.
The NodeMCU#1 side serial monitor should look like this getting DHT values from NodeMCU#2.
The NodeMCU#2 side serial monitor should look like this getting Button values from NodeMCU#1.
Receiving button states from NodeMCU#1. By applying conditions or using the turnery operator with the values we toggle leds on NodeMCU#2. Also, we can visualize the DHT data on NodeMCU#1 serial monitor.
Here is the video of testing the project
Conclusion
In this article, we have seen how we can establish Two-Way Communication between two NodeMCU boards with ESP-NOW communication protocol by espressif. We connected two boards controlled two led's with buttons and sent Dht 11 Data to NodeMCU#1 from NodeMCU#2, it was Two-Way Communication with Two Devices on the network, and more devices could be added and establish a many-to-many device communication. we will cover more on this topic in the future.
No comments:
Post a Comment