View All Posts
read
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi

Learn how to use ESP-NOW to facilitate communication between multiple ESP32 devices without any Wi-Fi connection. This powerful protocol enables both broadcasting and peer-to-peer messaging with a maximum message length of 250 bytes.

Related Content
Transcript

[0:04] Hello YouTube!
[0:06] So, on my desk, I have a motley selection of ESP 32 devices.
[0:15] We have a couple of standard boards we have one of my custom PCB boards
[0:22] And we have a HELTEC OLED based device.
[0:26] I’ve downloaded a simple sketch onto these devices
[0:31] that just wires up the boot button as a general purpose button
[0:37] to you turn the built-in LED on and off.
[0:41] So on the dev boards that’s the blue light
[0:45] On my custom board is this bottom LED here.
[0:49] And we’ll see what happens on the OLED display when we push the button.
[0:55] So push the button the LED has turned off
[0:58] But, mysteriously the LEDs on the other boards have also turned off!
[1:06] So, push the button again. The LED is on and the other LEDs go on.
[1:13] Let’s try the other board that all the lights turn off.
[1:17] All the lights turn on
[1:19] What kind of magic is this??
[1:22] We’re using ESP-NOW in broadcast mode
[1:28] So if we look at the OLED display
[1:31] We can see there’s a bunch of hexadecimal numbers.
[1:35] Now if you examine these carefully you will see they’re from different devices.
[1:41] So this is the MAC address of each device.
[1:43] So when I push the button you can see a new message arrives.
[1:48] And it says to turn the LEDs off.
[1:50] If I push this button a new
[1:52] A message arrives to turn the LEDs on and.
[1:55] They’re coming from the two different devices.
[1:58] So each device is sending out a message to all the other devices.
[2:05] And the other devices are receiving that message and acting upon it.
[2:11] So how does this work under the hood? Let’s take a look at a simple code sample.
[2:20] So I’ve included the WiFi header and I’ve included the ESP-NOW header.
[2:27] So let’s jump down to the setup method.
[2:31] We have a standard set up for the serial line.
[2:36] Then we need to activate the Wi-Fi module
[2:40] ff you don’t do this then the ESP-NOW will crash with an error.
[2:45] and then we’ll print out our MAC address
[2:49] this will be useful when we do some peer-to-peer transmission.
[2:54] Then we just disconnect the Wi-Fi from any base stations that it may be connected to.
[2:59] So now we can activate ESP-NOW so the call the ESP now init.
[3:05] Then we can register some callbacks so a callback for receiving messages
[3:10] and a callback for when we have sent a message.
[3:14] So that’s all you need to do to to initialize ESP-NOW.
[3:19] Let’s jump into our loop.
[3:22] So we read the built-in button.
[3:24] If the button is pushed
[3:26] we toggle the LED and then we send a broadcast message to say turn the LEDs on or turn the LEDs off.
[3:34] Let’s take a look at the broadcast function.
[3:40] So we create a broadcast address so to broadcast in ESP-NOW you just send a
[3:47] message to a MAC address that has all the values set to 255.
[3:54] So we create a new peer info.
[3:59] We set the peer address to the broadcast address
[4:03] and then we register this new peer with the ESP-NOW system.
[4:09] it’s a little bit strange to have
[4:12] to register a broadcast peer but if you don’t do this then you can’t send
[4:17] messages to the broadcast address.
[4:19] So now we’ve registered the broadcast peer we can simply call ESP-NOW send.
[4:27] We sent to the broadcast address and we
[4:30] just send our message on our message length.
[4:33] So that will either return ESP_OK
[4:37] or there will be an error message.
[4:40] Now you also get a callback on your sent callback function.
[4:45] So this gives you the MAC address that the message was sent to
[4:49] and a status as well. So we can log out if the message was actually sent successfully
[4:56] So that’s sending messages. So it’s very simple you add a peer and you call ESP-NOW send.
[5:04] Receiving message is equally simple.
[5:08] When a message is received by ESP-NOW it calls our receive callback.
[5:14] That gives us the MAC address that the message was received from.
[5:18] Gives us the data in the message and the length of the message.
[5:23] So we can easily turn that into a string
[5:27] We just set up a buffer with the ESP_NOW_MAX_DATA_LENGTH which is 250 bytes
[5:34] plus an extra byte for our null terminator.
[5:38] We’ll copy the contents of the message into our buffer
[5:42] and then we just make sure that we have a null terminator on the end.
[5:48] We can format the MAC address into a nice string and put that on the serial port.
[5:53] Or in the case of the HelTec with the OLED display we can show that on our screen.
[5:59] Then we just check to see that the buffer contains
[6:02] the message “on” in which case we change the state of our LED
[6:07] or if the message contained “off”
[6:09] we’ll change the states are LED to false.
[6:11] and then we just set our
[6:13] LED on or off depending on the message that was received.
[6:17] So obviously sending strings it’s not the most efficient way of communicating
[6:23] when you only have 250 bytes to send.
[6:26] So if you’re building this in a more production environment
[6:31] You would build a binary message and use that to communicate.
[6:37] But, it’s a very simple way broadcasting messages from one a ESP to another set of ESP devices.
[6:44] So let’s look at how you can actually send messages to a specific device.
[6:51] So I’ll show you that working and then we’ll walk through the code
[6:55] which is very similar to the broadcast code but it’s still equally simple.
[7:01] So let’s move on to sending peer-to-peer messages
[7:08] So I’ve downloaded a new sketch to the bottom device
[7:13] and I’ve set it up so that it only sends messages to this device on the right.
[7:19] So, if I push the button on the right-hand device
[7:24] this is still set up for broadcast mode so it will transmit messages to all of the devices
[7:30] and they’ll act upon it and turn their LEDs on and off.
[7:34] And if I push the button on this bottom device the message is only received by this device on the right
[7:41] So the other devices do not receive any of the messages.
[7:46] So this is an example of a direct connection that’s peer-to-peer
[7:51] between the bottom device and the right-hand device.
[7:55] So click the button.
[7:58] Only the right-hand device responds.
[8:01] So let’s have a look at the code changes for this
[8:05] and then we’ll do a quick summary of the ESP-NOW system.
[8:11] So the code changes are very minimal.
[8:16] All I’ve done is change from the broadcast address
[8:20] to the specific MAC address of the device I want to send the message to.
[8:24] So that’s all we need to do to send a message to a specific device instead of broadcasting it.
[8:33] So let’s summarize what we’ve learned about ESP-NOW.
[8:39] It enables multiple devices to communicate without using Wi-Fi
[8:46] It’s a proprietary protocol developed by Espressif.
[8:52] It uses the same spectrum as Wi-Fi so 2.4 gigahertz.
[8:59] It can do broadcast or peer to peer messaging.
[9:05] It has a maximum message length of 250 bytes.
[9:10] You can enable in encryption on your messages.
[9:15] There’s an important thing to remember that you need to set up the key on both sides.
[9:20] So both the sender and the receiver in order for encryption to work.
[9:27] And then finally I think after this simple demonstration
[9:32] you’ll agree that it’s a fairly simple and easy to use system has been developed.
[9:38] So that’s ESP-NOW in a nutshell. I hope you found this video useful.
[9:46] If you did please hit the subscribe button and let me know what you think in the comments.
[9:52] If you subscribe you’ll be first in line for new videos
[9:58] and also it gives me a nice boost to know that people are watching and enjoying the videos
[10:04] So thanks for watching
[10:07] I’ll see you in the next video


HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening

> Image

atomic14

A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts