Making a WiFi button to control devices was interesting, however it took about 7 seconds
to connect to the WiFi network and this is too long for certain applications. In addition
to this, sometimes, you simply want to send data wirelessly from one Arduino board to
another without the need for a WiFi router. In these cases, using a RF module like the
nRF24L01 can be useful. They are cost effective, they look like this and in order to use them,
you need to connect each one to an Arduino which ends up looking something like this.
If you are a beginner then all these connections might make things a bit complicated. ESP-NOW
is an alternative that can be used to quickly set up a wireless link and we will take a
closer look at it in this video.
ESP-NOW is a wireless protocol designed by Espressif, the same company behind the different
ESP modules. It uses the 2.4GHz frequency that is also used by modules like the nRF24
and standard WiFi. However, unlike standard WiFi, there are no IP addresses assigned to
the devices and as we will see in the video, this allows you to quickly connect to a device
and send data. With the initial testing, I was able to connect to a node, send data and
then go back to sleep within 280 milliseconds, which is much quicker than the time it takes
the WiFi button to do the same. In order for ESP-NOW to work, all the nodes need to have
an ESP module - either an ESP8266 or an ESP32.
You can communicate directly with up to 10 peers and the maximum payload is 250 bytes
which is more than enough for most sensor nodes that we will be building. What’s more
is that it has a much higher range of several hundred meters in line of sight, depending
on the conditions. There is documentation to help you understand the protocol further
and you can also read through the user guide to learn more. For this video, I am going
to use the Wemos D1 mini to keep things simple and we will take a look at 3 different use
cases.
First, let’s try to send data from one node to another. The node that sends the data is
going to be called the controller, while the node that receives the data is called the
slave. In order to send data to the correct node, we need to know it’s address. Every
internet enabled device has something called a MAC address. This is unique to it and is
used for identification. Since an ESP8266 or ESP32 module can connect to the internet,
they have their own MAC addresses. ESP-NOW uses this MAC address to identify different
nodes.
To obtain the MAC address of a board, you can use this sketch which prints it to the
terminal. This is what the output looks like – a group of 6 hexadecimal characters which
forms the MAC address of the board.
Now that we have the MAC address of the board, we can take a look at the sketch for the slave.
We start by printing a bunch of messages to the screen. I have defined a name at the top
so that you know which sketch is running. Similarly, I have printed out the MAC address
of the board so that you can quickly identify the boards. We then set the WiFi mode and
we make sure that we are disconnected from any WiFi network. All that we do next is initialize
ESP-NOW and make sure that it has been successfully setup. We also specify a receiving call back
function that is defined here. Whenever any data is received, this function will get called
and we receive three variables: the MAC address of the sender, the data itself and the length
of the data. We print out the MAC address of the sender so that we know who sent it
and we then process the data.
We will be using ESP-NOW to send data from different sensors and the easiest way to do
this is by using something called a structure. Think of a structure as a group of variables.
This structure or struct contains 2 integers and a float and has a name dataPacket. You
can add different types of variables to it, depending on your requirements. When we receive
some data, we simply copy it to the struct called packet, which is defined here. After
doing this, we can easily access the internal variables and print their values to the terminal.
This makes it super easy for you to obtain the data that is being sent. Keep in mind
that you need to use the same structure when sending the data and receiving it for things
to work correctly.
That is the code for the slave and when I run it you will see that it prints out it’s
MAC address and does nothing else as no data is being sent to it. All we need to do next
is send data to this MAC address with the same data structure that we just defined.
Here I have the sketch for the controller and it is similar in many ways. We start by
printing out the name along with the MAC address of the board. We then initialize ESP-NOW and
set the role of the board so that it acts like a controller. We then assign the sending
call-back function which will be called after data has been sent. This gives us the MAC
address of the board that we send the data to along with the transmission status. We
can check the status and take the necessary action. After defining the sending call-back
function, we need to add the receiver and for this we need to specify the receiver MAC
address, it’s role and the WiFi channel. This receiver role is going to be slave and
we need to match the address to the address we obtained on the serial terminal. Once that
is done, we can simply send the data to it. Once again, I am using a data structure to
define the data that will be sent. This is the same structure used on the slave. I simply
create a copy of it, assign some values to it and then send the packet to the receiver.
This is repeated every three seconds. Here is the output after uploading it to the board.
We have the board name; MAC address and you can also see that the messages are being sent
successfully. This is what the setup looks like. The controller sends data to the slave
board and we can to view all of this through the serial terminal.
The Arduino IDE allows you to use the serial monitor for only one port. In order to view
the other board, I will be using software called CoolTerm which is simply a serial monitor.
Once you install it, you need to use the options window to scan the available ports. Then select
the port you want, the baud rate and then hit OK. Once done, you can then click the
connect button here to open up the port and view the data. Here I have the receiver node
and it is able to pick up the messages which are then printed out. This will continue indefinitely.
To send data to another board, all you have to do is update the receiver address. If you
set all the values to 0xFF then the data will be sent to all the ESP-NOW nodes around this
one and you can think of this as a broadcast mode.
What we have here is a very simple setup. One board is sending data to another. What
if we wanted to send data from another node? For that, all you need to do is open up the
controller sketch and upload it to a new node. However, I will change the values so that
you can view this as well. Once done, you will see that the receiver or the slave picks
up the messages from both the boards. As you can tell, this makes it so much easier to
add more nodes to your setup.
Let’s now take a look at the third setup, where we want the boards to both receive and
send data. These nodes are called combo nodes and this is an example sketch. It is simply
a combination of the previous two sketches. We have the debug messages like before followed
by the ESP-NOW initialization. We then set the role for this node which is going to be
combo and we then define the two call-back functions. One when data has been sent and
the other when we receive data. Both these functions are the same as before. After assigning
these functions, we add the peer by assigning its MAC address, role and the WiFi channel.
Once that is done, we simply send some sample data every three seconds as seen here and
it’s all as simple as that.
I have defined these statements here so that you can use the same sketch with multiple
boards. Each one has a unique name and the MAC address of the receiver. I am going to
upload this to the board and it will be called the sensor node. For the other board, I am
going to comment out these lines and uncomment the others and then upload it. We can then
open up two serial terminal windows to view the output from the boards. As you can see,
they are both sending data to each other and they are also displaying the data that they
are receiving. We can confirm this as we already know their mac addresses.
Before we conclude this video, I would like to measure the current consumption. To do
this, I am going to go back to the controller-slave sketches. Here I have the same slave sketch
as before and this has been uploaded to a one node. For the controller, I will use the
same sketch as before which sends the data every three seconds. The measured current
of the controller board is about 68 milliamps on average which is similar to the value we
obtained with the WiFi button. Here I have a slight variation of the controller sketch
and instead of using a delay function, I am putting the board in deep sleep mode for three
seconds. This function takes a value in microseconds so I am using this definition to obtain the
value in microseconds. Once uploaded to the board, you need to connect a jumper across
pins D0 and RST. This is required as we want the board to wake up automatically. You will
see that there is a small pulse every 3 seconds as the board wakes up, but it is too quick
for the multi-meter to react to. To overcome this, I am going to use a one-ohm power resistor
and I will measure the voltage drop across it. This is what the oscilloscope output looks
like and as you can see, we have a couple of peaks but what is interesting is that it
only takes 280 milliseconds to complete the data transmission. This is 25 times quicker
than the time it took for the WiFi button and it means that we can extend the battery
life even further. I will be creating version 2 of the WiFi button using this which should
be more efficient.
I do hope you liked this video. I have shared the sketches in the description for you to
use. Thank you for watching and I will see you in the next one!