2024-08-20 05:39:25 -04:00
|
|
|
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-S2 | ESP32-S3 |
|
|
|
|
| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- |
|
2022-05-20 05:50:08 -04:00
|
|
|
|
2017-09-13 08:38:04 -04:00
|
|
|
# ESPNOW Example
|
|
|
|
|
2018-08-29 08:16:20 -04:00
|
|
|
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
2017-09-13 08:38:04 -04:00
|
|
|
|
2018-08-29 08:16:20 -04:00
|
|
|
This example shows how to use ESPNOW of wifi. Example does the following steps:
|
2017-09-13 08:38:04 -04:00
|
|
|
|
2018-08-29 08:16:20 -04:00
|
|
|
* Start WiFi.
|
|
|
|
* Initialize ESPNOW.
|
|
|
|
* Register ESPNOW sending or receiving callback function.
|
|
|
|
* Add ESPNOW peer information.
|
2022-05-20 05:50:08 -04:00
|
|
|
* Send and receive ESPNOW data.
|
2017-09-13 08:38:04 -04:00
|
|
|
|
2018-08-29 08:16:20 -04:00
|
|
|
This example need at least two ESP devices:
|
2017-09-13 08:38:04 -04:00
|
|
|
|
2022-05-20 05:50:08 -04:00
|
|
|
* In order to get the MAC address of the other device, Device1 firstly send broadcast ESPNOW data with 'state' set as 0.
|
|
|
|
* When Device2 receiving broadcast ESPNOW data from Device1 with 'state' as 0, adds Device1 into the peer list.
|
|
|
|
Then start sending broadcast ESPNOW data with 'state' set as 1.
|
|
|
|
* When Device1 receiving broadcast ESPNOW data with 'state' as 1, compares the local magic number with that in the data.
|
|
|
|
If the local one is bigger than that one, stop sending broadcast ESPNOW data and starts sending unicast ESPNOW data to Device2.
|
|
|
|
* If Device2 receives unicast ESPNOW data, also stop sending broadcast ESPNOW data.
|
|
|
|
|
|
|
|
In practice, if the MAC address of the other device is known, it's not required to send/receive broadcast ESPNOW data first,
|
2018-08-29 08:16:20 -04:00
|
|
|
just add the device into the peer list and send/receive unicast ESPNOW data.
|
2017-09-13 08:38:04 -04:00
|
|
|
|
2022-05-20 05:50:08 -04:00
|
|
|
There are a lot of "extras" on top of ESPNOW data, such as type, state, sequence number, CRC and magic in this example. These "extras" are
|
|
|
|
not required to use ESPNOW. They are only used to make this example to run correctly. However, it is recommended that users add some "extras"
|
2017-09-13 08:38:04 -04:00
|
|
|
to make ESPNOW data more safe and more reliable.
|
|
|
|
|
2018-08-29 08:16:20 -04:00
|
|
|
## How to use example
|
|
|
|
|
|
|
|
### Configure the project
|
|
|
|
|
|
|
|
```
|
2019-08-01 23:31:20 -04:00
|
|
|
idf.py menuconfig
|
2018-08-29 08:16:20 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
* Set WiFi mode (station or SoftAP) under Example Configuration Options.
|
2022-05-20 05:50:08 -04:00
|
|
|
* Set ESPNOW primary master key under Example Configuration Options.
|
2018-08-29 08:16:20 -04:00
|
|
|
This parameter must be set to the same value for sending and recving devices.
|
|
|
|
* Set ESPNOW local master key under Example Configuration Options.
|
|
|
|
This parameter must be set to the same value for sending and recving devices.
|
|
|
|
* Set Channel under Example Configuration Options.
|
|
|
|
The sending device and the recving device must be on the same channel.
|
|
|
|
* Set Send count and Send delay under Example Configuration Options.
|
|
|
|
* Set Send len under Example Configuration Options.
|
2018-10-23 22:31:58 -04:00
|
|
|
* Set Enable Long Range Options.
|
|
|
|
When this parameter is enabled, the ESP32 device will send data at the PHY rate of 512Kbps or 256Kbps
|
2022-05-20 05:50:08 -04:00
|
|
|
then the data can be transmitted over long range between two ESP32 devices.
|
2018-08-29 08:16:20 -04:00
|
|
|
|
|
|
|
### Build and Flash
|
|
|
|
|
|
|
|
Build the project and flash it to the board, then run monitor tool to view serial output:
|
|
|
|
|
|
|
|
```
|
2019-08-01 23:31:20 -04:00
|
|
|
idf.py -p PORT flash monitor
|
2018-08-29 08:16:20 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
(To exit the serial monitor, type ``Ctrl-]``.)
|
|
|
|
|
|
|
|
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
|
|
|
|
|
|
|
## Example Output
|
|
|
|
|
|
|
|
Here is the example of ESPNOW receiving device console output.
|
|
|
|
|
|
|
|
```
|
|
|
|
I (898) phy: phy_version: 3960, 5211945, Jul 18 2018, 10:40:07, 0, 0
|
|
|
|
I (898) wifi: mode : sta (30:ae:a4:80:45:68)
|
|
|
|
I (898) espnow_example: WiFi started
|
|
|
|
I (898) ESPNOW: espnow [version: 1.0] init
|
|
|
|
I (5908) espnow_example: Start sending broadcast data
|
|
|
|
I (6908) espnow_example: send data to ff:ff:ff:ff:ff:ff
|
|
|
|
I (7908) espnow_example: send data to ff:ff:ff:ff:ff:ff
|
|
|
|
I (52138) espnow_example: send data to ff:ff:ff:ff:ff:ff
|
|
|
|
I (52138) espnow_example: Receive 0th broadcast data from: 30:ae:a4:0c:34:ec, len: 200
|
|
|
|
I (53158) espnow_example: send data to ff:ff:ff:ff:ff:ff
|
|
|
|
I (53158) espnow_example: Receive 1th broadcast data from: 30:ae:a4:0c:34:ec, len: 200
|
|
|
|
I (54168) espnow_example: send data to ff:ff:ff:ff:ff:ff
|
|
|
|
I (54168) espnow_example: Receive 2th broadcast data from: 30:ae:a4:0c:34:ec, len: 200
|
|
|
|
I (54168) espnow_example: Receive 0th unicast data from: 30:ae:a4:0c:34:ec, len: 200
|
|
|
|
I (54678) espnow_example: Receive 1th unicast data from: 30:ae:a4:0c:34:ec, len: 200
|
|
|
|
I (55668) espnow_example: Receive 2th unicast data from: 30:ae:a4:0c:34:ec, len: 200
|
|
|
|
```
|
|
|
|
|
|
|
|
Here is the example of ESPNOW sending device console output.
|
|
|
|
|
|
|
|
```
|
|
|
|
I (915) phy: phy_version: 3960, 5211945, Jul 18 2018, 10:40:07, 0, 0
|
|
|
|
I (915) wifi: mode : sta (30:ae:a4:0c:34:ec)
|
|
|
|
I (915) espnow_example: WiFi started
|
|
|
|
I (915) ESPNOW: espnow [version: 1.0] init
|
|
|
|
I (5915) espnow_example: Start sending broadcast data
|
|
|
|
I (5915) espnow_example: Receive 41th broadcast data from: 30:ae:a4:80:45:68, len: 200
|
|
|
|
I (5915) espnow_example: Receive 42th broadcast data from: 30:ae:a4:80:45:68, len: 200
|
|
|
|
I (5925) espnow_example: Receive 44th broadcast data from: 30:ae:a4:80:45:68, len: 200
|
|
|
|
I (5935) espnow_example: Receive 45th broadcast data from: 30:ae:a4:80:45:68, len: 200
|
|
|
|
I (6965) espnow_example: send data to ff:ff:ff:ff:ff:ff
|
|
|
|
I (6965) espnow_example: Receive 46th broadcast data from: 30:ae:a4:80:45:68, len: 200
|
|
|
|
I (7975) espnow_example: send data to ff:ff:ff:ff:ff:ff
|
|
|
|
I (7975) espnow_example: Receive 47th broadcast data from: 30:ae:a4:80:45:68, len: 200
|
|
|
|
I (7975) espnow_example: Start sending unicast data
|
|
|
|
I (7975) espnow_example: send data to 30:ae:a4:80:45:68
|
|
|
|
I (9015) espnow_example: send data to 30:ae:a4:80:45:68
|
|
|
|
I (9015) espnow_example: Receive 48th broadcast data from: 30:ae:a4:80:45:68, len: 200
|
|
|
|
I (10015) espnow_example: send data to 30:ae:a4:80:45:68
|
|
|
|
I (16075) espnow_example: send data to 30:ae:a4:80:45:68
|
|
|
|
I (17075) espnow_example: send data to 30:ae:a4:80:45:68
|
|
|
|
I (24125) espnow_example: send data to 30:ae:a4:80:45:68
|
|
|
|
```
|
|
|
|
|
|
|
|
## Troubleshooting
|
|
|
|
|
2022-05-20 05:50:08 -04:00
|
|
|
If ESPNOW data can not be received from another device, maybe the two devices are not
|
|
|
|
on the same channel or the primary key and local key are different.
|
2017-09-13 08:38:04 -04:00
|
|
|
|
2022-05-20 05:50:08 -04:00
|
|
|
In real application, if the receiving device is in station mode only and it connects to an AP,
|
2018-08-29 08:16:20 -04:00
|
|
|
modem sleep should be disabled. Otherwise, it may fail to revceive ESPNOW data from other devices.
|