The new Arduino Nano ESP32 was just launched, anyway I'll not write about specs of the board since there is already a lot about on the Arduino.cc website. I'll say you only about some things you must know for following this tutorial:
- There are 4 default leds on board: there is the yellow one you can drive using the classical LED_BUILTIN definition, and then there are 3 new small Red, Green and Blue leds (it's an RGB led having 3 separate cathodes for each color). Blue is connected on a dedicate GPIO that is not routed to the external board pins, while the Green one is connected to B1 pin and the Red on B0 pin. Since those leds are directly driven from GPIOs, those leds will work in opposite way than the LED_BUILTIN: they will turn on when you tie low the respective GPIO. You can refer to these leds as LED_RED, LED_GREEN and LED_BLUE.
- The board pinout is the same of all Arduino Nano family
Click on the Board Icon on the left menù bar (or, in the old way, go in the menù Tools > Board > Board Manager) and type ESP32 in the search bar:
Click on the Install button in the ESP32 by Arduino - titled box.
How to program the Arduino Nano ESP32The Arduino Nano ESP32 has no USB-to-serial bridge but ESP32 native USB port is used, anyway it works like any other Arduino board.
You must only select the Board and the COM port as usually:
If board don't respond, you can try manually entering the bootloader mode by doing a double click on the reset button: the green led will glow indicating board is entered in bootloader mode, anyway ESP32 will exit from this modality after tenth of seconds.ESP-NOW protocol
What is this? I'll report here the original description by Espressif:
ESP-NOW is a wireless communication protocol defined by Espressif, which enables the direct, quick and low-power control of smart devices, without the need of a router. ESP-NOW can work with Wi-Fi and Bluetooth LE, and supports the ESP8266, ESP32, ESP32-S and ESP32-C series of SoCs. It’s widely used in smart-home appliances, remote controlling, sensors, etc.
So it's a simple way letting two or more ESP devices talk each other without using a wi-fi infrastructure.
The physical layer of this protocol is the IEEE 802.11 b/g/n so is the same of the Wi-Fi but can coexists with the Wi-Fi and Bluetooth LE (the only limitation is that the ESP-NOW must use the same channel of the AP),
This protocol was designed for applications not needing a lot of data to be exchanged (the limit is 250 bytes) and so it needs few CPU and memory resources. Using the ESP-NOW protocol you can connect one-to-many or many-to-many devices, pairing up to 20 devices and Encryption is supported too.
You can find more about the ESP-NOW here: https://docs.espressif.com/[...]
What will we doYou'll need 2 Arduino Nano ESP32 : they will exchange some examples data each other, so I called them "Device A" and "Device B" since in this situation makes no sense talk about "master" and "slave" or "client" and "server" and so on: each device act the same and then code is pretty similar for each board.
I made this example with another idea in my mind: to have a remote command for a robot that can receive some telemetry data from robot too, so 2 bi-directional devices. The difference between my idea and this example will be only the intefacing with external components. I added some "text" data example (a char array) since, during my experience, I noticed lot of people had some difficulties handling this kind of data.
Some Preliminary WorkSince this example will make 2 boards exchanging data, but there can be a lot of similar applications in the same environment, every board needs to know with which board to talk and this is achieved by knowing the MAC address of the paired boards. MAC address is unique and so there is no (apparent) possibility to exchange data (this is not really true but we'll made things simplier here!).
A paired device is also called peer
So, first than run the main example, the obtaining of MAC addresses of boards is needed. You can do this running the attached NanoESP32_MacAddress.ino example sketch in each board: this sketch will write continuously the board MAC Address in the Serial Terminal.
After uploaded the sketch, open the serial terminal at 115200 and look at response: you can detach the board for stopping the data flow and then copy/paste the MAC address.
Code explainedThe ESP-NOW framework will transfer data as a simple byte sequence (a byte array), so if we want to transfer, as in this example, a char array and a 16-bit number, the received sequence will contain this stuff as an entire array (single bytes in sequence).
Then we must separate single elements from the array: this can be achieved in a lot of ways. In the past, I used to do it in a very obsolete manner by adding commas for separating elements and then extract and convert every piece... but this a very boring task and now there are lot of easier ways.
This time I used structures for storing single elements in a more tidy way and then I applied the memcpy function for transfer the received buffer in the structure (that is treated at low level as a simple byte array).
I defined 2 different structures for data received and data to be transmitted: this is not needed everytime ad depends upon data you want to transfer! If you want to transfer only a numerical value (for example: a temperature), a simple number (maybe a float) can be used instead of a structure
Probably usage of a JSON is not suitable since we said the ESP-NOW can transfer only 250bytes at once, and transferring a byte sequence with data occupying always the same place is more compact.
Now we need a place for storing the informations about the coupled device (the MAC address): a structure having the type esp_now_peer_info_t is used for this, than a function called esp_now_add_peer will be used from the framework for acquiring and store those data.
There are further 2 functions to be defined for using the ESP-NOW data transfer: those are callback functions: this means those procedures will be called automatically by the CPU at a precise event and so there is no need to call them during the code execution (are properly interrupt functions).
So there is a callback will be called at the end of data transmission, and we must defin it as (you can change the names, but parameters must be as written):
void payloadSentCB(const uint8_t *mac_addr, esp_now_send_status_t status)
this function will store the transmission status in the status variable and can assume two values:
- ESP_NOW_SEND_SUCCESS
- ESP_NOW_SEND_FAIL
I think there is no need to explain. There could be many reasons for having a FAIL response: one can be there is a too short interval between data sending, or simply the device supposed to receive data is not working (not plugged?).
The other callback will be called at the end of data receiving:
void payloadReceivedCB(const uint8_t *mac_addr, const uint8_t *payload, int len)
I called payload the array of data received following some other conventions. As I said, all of the data will stored in this array that will have len bytes in it. For transferring data in my structure I've used the memcpy function using my structure as target.
Notice both callback functions returns the MAC address of the peer: I don't use it but this can be useful in case you build an application when a device will talk to many other devices and you want to understand from which of them you received the response.
Once you defined those 2 callbacks, you need to register them so the ESP-NOW framework will know which function call at the specific event:
// Register callback on payload received
esp_now_register_recv_cb(payloadReceivedCB);
// Register callback on payload sendt
esp_now_register_send_cb(payloadSentCB);
The ESP-NOW framework is initiated using the esp_now_init() function, that will return true in positive (good) case. After a successful init you must couple the devices you want talk to, and this is done using the esp_now_add_peer() function passing the esp_now_peer_info_t structure type we defined earlier.
In the loop I'm only preparing and then sending data. I added some Arduino Nano ESP32 led flashing just for fun and for showing how to use them. The data receiving is entirely managed in the callback.
You'll see the data transfer result by seeing the messages in the serial terminal, so you need to open 2 serial terminals (you can open 2 different Arduino IDE windows).
Comments
Please log in or sign up to comment.