Post Top Ad

Thursday, March 2, 2023

on video ESP-NOW - Peer to Peer ESP32 Network


 The ESP32 and its cousin, the ESP8266, are undoubtedly remarkable microcontrollers. Aside from a high-speed 32-bit architecture, they also have built-in Bluetooth and WiFi.


The Bluetooth and WiFi capabilities on these devices are made possible by an integrated 2.4GHz radio transceiver module. And this module can also be used for other communications applications that use the unlicensed 2.4GHz band.


Espressif, the makers of the ESP8266 and ESP32, have developed a protocol that allows all these devices to create a private, wireless network using the 2.5GHz transceivers. This is a separate network from the WiFi network and can only be used by ESP-type microcontrollers.

The protocol is called ESP-NOW.

Yes, I said 250 bytes! Not quite enough for voice and video perhaps, although there are ways of packetizing both of those, but quite sufficient to deliver remote control commands or data from sensors.

The data can be unidirectional or bidirectional, i.e. single-duplex or full-duplex. Most data types are supported.

Data can be encrypted or unencrypted, and no external source of WiFi or a router is required. Depending upon your configuration, you can have anywhere from 2 to 20 devices communicating between themselves.

The range can vary dramatically due to the environment, but under the right conditions (and with proper antennas) you can achieve over 400 meters. Just using the built-in antennas on the modules should still allow you to communicate through a medium-sized home without a problem.


ESP-NOW Networking Modes

You can place your ESP-NOW network in many configurations. You can mix and match ESP32 and ESP8266 devices within the same network.

n this arrangement, the Initiator ESP32 transmits data to the Responder ESP32. The Initiator can tell if the Responder received the message successfully.

This is a simple arrangement, but it has many uses in remote control applications.

One Initiator & Multiple Responders

This setup consists of one Initiator that is communicating with multiple responders.

The configuration can be used in two fashions:

The Initiator communicates with each Responder individually.

The Initiator initiates a broadcast message to communicate with all the Responders.

An alarm system might use this sort of configuration to activate remote sounders or communicate with remote monitors when an alarm has been triggered

One Responder & Multiple Initiators

This is the reverse of the previous ESP-NOW network configuration. In this arrangement, we have one Responder and multiple Initiators.

This arrangement is very common as it is used for remote sensor applications, with the Responder gathering data sent by the Initiator.

Two-Way Communications

The ESP-NOW protocol can also handle bidirectional, or full-duplex, communications.

This illustrates the simplest arrangement, with two devices communicating with one another.

In this, and all bidirectional communications configurations, the ESP-32 acts as both Initiator and Resolver.

Two-Way Networking

Expanding upon the previous configuration even further, we come up with this arrangement, four boards that have bidirectional communications established with one another.

Note that the boards can be a mix of different models of both ESP32 and ESP8266 devices.

MAC Addresses

When Initiators communicate with Responders, they need to know the Responder’s MAC Address.

A MAC, or Media Access Control, Address is a unique 6-digit hexadecimal number assigned to every device on a network. It is generally burned into the device by the manufacturer, although it is possible to manually set it.

All we are doing is including the WiFi Library, initializing the serial monitor, placing the ESP32 into Station mode, and then asking it for its MAC address. The result is printed on the serial monitor.

Running The MAC Sketch

The entire sketch runs in the Setup section, so after loading it to the ESP32, it will likely run before you get a chance to view it on the Serial monitor.


You can press the Reset key on your module to force it to run again.

The MAC Address will be at the bottom of the screen. Copy it to a safe location, so that you can use it later.

Coding for ESP-NOW

The ESP-NOW Library is included in your ESP device boards manager installation.  It has a number of functions and methods to assist with coding for ESP-NOW.


In order to see how it works, you need to examine the sending and receiving of an ESP-NOW message packet.

In the ESP-NOW protocol, there are two callbacks of interest:


Sending Data – The esp_now_register_send_cb() callback is called whenever data is sent.

Receiving Data – The esp_now_register_rcv_cb() callback is called when data is received.

In your code, you will create a callback function that you will bind to either the sending or receiving callback using the functions listed above.  Your function will run every time the event occurs.


The callback functions also return some useful data:


The Sending callback returns the status of the sent data.

The Receiving callback includes the received data packet.

Coding for ESP-NOW – Sending

If you are writing code to use the ESP32 or ESP8266 as the Initiator, then this is what you need to accomplish:

You need to initialize the ESP-NOW library.

Next, you’ll register your send callback function

You need to add a peer device, which is the responder device. You add the peer by specifying its MAC address.

Finally, you can packetize and send the message.

Coding for ESP-NOW – Receiving

To write code for the ESP-NOW responder, you’ll need to do the following

You need to initialize the ESP-NOW library.

Next, you’ll register your receive callback function.

In the receive callback, you’ll capture the incoming message data and pass it to a variable.

Let’s dig out some ESP boards and start experimenting with ESP-NOW.


Experimenting with ESP-NOW

I’ll be playing with the ESP32 today, but you should be able to run everything on an ESP8266 as well. If you do, you’ll need to change the WiFi library, as the ESP8266 uses a different one.


Gather a few modules, a mix of different manufacturers boards is fine, and run the MAC Address sketch on each of them and save the address to a text file or spreadsheet.

One-Way Data Test

Our first test is very simple and is a great way to learn the fundamentals of sending and receiving data using ESP-NOW.


We will need two ESP32 boards for this test. One board will be the Initiator (sender), and the other will be our Responder (receiver).  We don’t require any additional hardware on the modules, as we’ll be monitoring the results with the serial monitor in the Arduino IDE.


As one board is Initiator and the other Responder, they will be running different sketches, which are shown below. You’ll need the MAC address of the Responder, as it goes into the Initiators code.


One-Way Initiator Sketch

The first sketch we will be running is the One-Way Initiator Sketch. This is run on the board that is transmitting the data.


 The ESP32 and its cousin, the ESP8266, are undoubtedly remarkable microcontrollers. Aside from a high-speed 32-bit architecture, they also have built-in Bluetooth and WiFi.


The Bluetooth and WiFi capabilities on these devices are made possible by an integrated 2.4GHz radio transceiver module. And this module can also be used for other communications applications that use the unlicensed 2.4GHz band.


Espressif, the makers of the ESP8266 and ESP32, have developed a protocol that allows all these devices to create a private, wireless network using the 2.5GHz transceivers. This is a separate network from the WiFi network and can only be used by ESP-type microcontrollers.

The protocol is called ESP-NOW.

Yes, I said 250 bytes! Not quite enough for voice and video perhaps, although there are ways of packetizing both of those, but quite sufficient to deliver remote control commands or data from sensors.

The data can be unidirectional or bidirectional, i.e. single-duplex or full-duplex. Most data types are supported.

Data can be encrypted or unencrypted, and no external source of WiFi or a router is required. Depending upon your configuration, you can have anywhere from 2 to 20 devices communicating between themselves.

The range can vary dramatically due to the environment, but under the right conditions (and with proper antennas) you can achieve over 400 meters. Just using the built-in antennas on the modules should still allow you to communicate through a medium-sized home without a problem.


ESP-NOW Networking Modes

You can place your ESP-NOW network in many configurations. You can mix and match ESP32 and ESP8266 devices within the same network.

n this arrangement, the Initiator ESP32 transmits data to the Responder ESP32. The Initiator can tell if the Responder received the message successfully.

This is a simple arrangement, but it has many uses in remote control applications.

One Initiator & Multiple Responders

This setup consists of one Initiator that is communicating with multiple responders.

The configuration can be used in two fashions:

The Initiator communicates with each Responder individually.

The Initiator initiates a broadcast message to communicate with all the Responders.

An alarm system might use this sort of configuration to activate remote sounders or communicate with remote monitors when an alarm has been triggered

One Responder & Multiple Initiators

This is the reverse of the previous ESP-NOW network configuration. In this arrangement, we have one Responder and multiple Initiators.

This arrangement is very common as it is used for remote sensor applications, with the Responder gathering data sent by the Initiator.

Two-Way Communications

The ESP-NOW protocol can also handle bidirectional, or full-duplex, communications.

This illustrates the simplest arrangement, with two devices communicating with one another.

In this, and all bidirectional communications configurations, the ESP-32 acts as both Initiator and Resolver.

Two-Way Networking

Expanding upon the previous configuration even further, we come up with this arrangement, four boards that have bidirectional communications established with one another.

Note that the boards can be a mix of different models of both ESP32 and ESP8266 devices.

MAC Addresses

When Initiators communicate with Responders, they need to know the Responder’s MAC Address.

A MAC, or Media Access Control, Address is a unique 6-digit hexadecimal number assigned to every device on a network. It is generally burned into the device by the manufacturer, although it is possible to manually set it.

All we are doing is including the WiFi Library, initializing the serial monitor, placing the ESP32 into Station mode, and then asking it for its MAC address. The result is printed on the serial monitor.

Running The MAC Sketch

The entire sketch runs in the Setup section, so after loading it to the ESP32, it will likely run before you get a chance to view it on the Serial monitor.


You can press the Reset key on your module to force it to run again.

The MAC Address will be at the bottom of the screen. Copy it to a safe location, so that you can use it later.

Coding for ESP-NOW

The ESP-NOW Library is included in your ESP device boards manager installation.  It has a number of functions and methods to assist with coding for ESP-NOW.


In order to see how it works, you need to examine the sending and receiving of an ESP-NOW message packet.

In the ESP-NOW protocol, there are two callbacks of interest:


Sending Data – The esp_now_register_send_cb() callback is called whenever data is sent.

Receiving Data – The esp_now_register_rcv_cb() callback is called when data is received.

In your code, you will create a callback function that you will bind to either the sending or receiving callback using the functions listed above.  Your function will run every time the event occurs.


The callback functions also return some useful data:


The Sending callback returns the status of the sent data.

The Receiving callback includes the received data packet.

Coding for ESP-NOW – Sending

If you are writing code to use the ESP32 or ESP8266 as the Initiator, then this is what you need to accomplish:

You need to initialize the ESP-NOW library.

Next, you’ll register your send callback function

You need to add a peer device, which is the responder device. You add the peer by specifying its MAC address.

Finally, you can packetize and send the message.

Coding for ESP-NOW – Receiving

To write code for the ESP-NOW responder, you’ll need to do the following

You need to initialize the ESP-NOW library.

Next, you’ll register your receive callback function.

In the receive callback, you’ll capture the incoming message data and pass it to a variable.

Let’s dig out some ESP boards and start experimenting with ESP-NOW.


Experimenting with ESP-NOW

I’ll be playing with the ESP32 today, but you should be able to run everything on an ESP8266 as well. If you do, you’ll need to change the WiFi library, as the ESP8266 uses a different one.


Gather a few modules, a mix of different manufacturers boards is fine, and run the MAC Address sketch on each of them and save the address to a text file or spreadsheet.

One-Way Data Test

Our first test is very simple and is a great way to learn the fundamentals of sending and receiving data using ESP-NOW.


We will need two ESP32 boards for this test. One board will be the Initiator (sender), and the other will be our Responder (receiver).  We don’t require any additional hardware on the modules, as we’ll be monitoring the results with the serial monitor in the Arduino IDE.


As one board is Initiator and the other Responder, they will be running different sketches, which are shown below. You’ll need the MAC address of the Responder, as it goes into the Initiators code.


One-Way Initiator Sketch

The first sketch we will be running is the One-Way Initiator Sketch. This is run on the board that is transmitting the data.

No comments:

Post a Comment

Post Top Ad

Pages