Breaking

Wednesday, December 6, 2023

ESP-NOW step-by-step One Way Communication with Esp8266 NodeMCU

In this article, we will see how we can use ESP-NOW to establish 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 One-Way-Communication with ESP8266

Let's build a simple project, where we will connect 2 push buttons to 1st NodeMCU board, and connect 2 led's to 2nd NodeMCU board. after setting up both of our boards we will be able to control led's connected to the 2nd NodeMCU board by pressing push buttons connected to the 1st NodeMCU board.
According to this diagram, Push Buttons are connected to 1st NodeMCU (Transmitter) boards D0 and D4 pin.

LEDs Connected to 2nd NodeMCU (Receiver) boards D3 and D8 pin.

Now to initiate One-way Communication we need the MAC Address for the Receiver NodeMCU Board.

Get MacAddress of NodeMCU (Receiver)

Upload this code to get the MAC Address of your board.

After uploading the code go to Serial Monitor and set baud rate of 115200, press RST button of NodeMCU once. You will get the MAC Address of your NodeMCU board as Follows.

After getting MAC Address we will program both NodeMCU Boards to establish ESP-NOW communication.

Let's discuss some key points of both sketch

We will upload the sketch to both of our NodeMCU boards 

   Transmitter NodeMCU sketch 
  • Initialize ESP NOW.
  • Register a callback function onDataSent() function to confirm message delivery.
  • Add the receiver device as a peer on the Transmitter board.
  • Send the message to the peer device.
  Receiver NodeMCU sketch
  •  Initialize ESP NOW. 
  •  Register a receive callback function onDataRcev() to receive the message from the sender.
  •  Inside the callback function save data to variables and execute the task.

ESP8266 NodeMCU Transmitter Sketch
  • Make sure to replace the MAC Address in the sketch 

ESP8266 NodeMCU Receiver Sketch

Now Let's test the communication
   After uploading the sketch to both the boards, open the serial monitor for both the transmitter and receiver board, you should see different COM ports.



The transmitter side serial monitor should look like this.

The receiver side serial monitor should look like this.

Receiving button states from transmitter NodeMCU board on Receiver NodeMCU board. By applying conditions or using the turnery operator with the values we toggle leds.

Here is the video of testing the project


Conclusion
In this article, we have seen how we can establish One-Way Communication between two NodeMCU boards (transmitter and receiver) with ESP-NOW communication protocol by espressif. We connected two boards and controlled two led's with buttons, it was One_Way Communication, and we can also establish Two-Way Communication, also more devices can be added. we will cover more on this topic in the future. 


🙏We hope you have found this article useful, Thank you for reading🙏




No comments:

Post a Comment