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