-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Add ESP-NOW Arduino library #9395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
2c68fdc
feat(libs): Add ESP-NOW Arduino library
P-R-O-C-H-Y f1a6b90
Update libraries/ESP_NOW/src/ESP32_NOW.cpp
P-R-O-C-H-Y 26410d9
Update libraries/ESP_NOW/src/ESP32_NOW.cpp
P-R-O-C-H-Y 1f34934
fix(esp-now): Add check if Wifi is started.
P-R-O-C-H-Y c5ed3b1
Fix ESP_NOW_Serial
me-no-dev 20489b3
Add ESP NOW Serial Example
lucasssvaz e3b6715
Add comment
lucasssvaz 2b7730c
Skip esp-now example for esp32h2
P-R-O-C-H-Y 6bf369a
Merge branch 'master' into feat/espnow-library
P-R-O-C-H-Y deacaac
Add broadcast address constant
lucasssvaz 271f94b
Change return value to align with other APIs
lucasssvaz 8751b98
Apply suggested changes
lucasssvaz 6d04c83
Improve example
lucasssvaz 8a6025c
Fix example
lucasssvaz fac9f95
Merge branch 'master' into feat/espnow-library
lucasssvaz 6bf981e
Improve serial example
lucasssvaz 07f7de0
Add argument to receive callback to know if a message was broadcasted
lucasssvaz 03d386b
Update libraries/ESP_NOW/src/ESP32_NOW_Serial.cpp
lucasssvaz 52c4ec2
Simplify example
lucasssvaz 98a6ae0
Merge branch 'feat/espnow-library' of ssh://github.com/P-R-O-C-H-Y/ar…
lucasssvaz 03e837c
Add broadcast example
lucasssvaz 6a0774c
Change comments
lucasssvaz 34168a1
Change comment
lucasssvaz ab31ab8
Improve broadcast master example
lucasssvaz 5ca2af1
Remove examples using IDF's API
lucasssvaz c435a7f
Fix example
lucasssvaz 6ad83cf
Add network example
lucasssvaz c0b2042
Add skip file
lucasssvaz 4085a2f
Add LMK back
lucasssvaz f7278fa
Add logs
lucasssvaz 6f6e889
Improve example
lucasssvaz 705b1ee
Fix example
lucasssvaz 5fe5f77
Apply @suglider suggestions from code review
P-R-O-C-H-Y a815a15
Merge branch 'master' into feat/espnow-library
P-R-O-C-H-Y 9017067
Add documentation
P-R-O-C-H-Y 2b43448
fix examples links in docs
P-R-O-C-H-Y fc7616e
Apply @lucasssvaz suggestions to docs
P-R-O-C-H-Y 14f19be
Update espnow.rst
P-R-O-C-H-Y ad193ae
Update examples
lucasssvaz 439840b
Merge branch 'feat/espnow-library' of ssh://github.com/P-R-O-C-H-Y/ar…
lucasssvaz 51ce71f
make onSent optional and remove underscores for virtual functions
P-R-O-C-H-Y 55a6193
Make onRecieve also optional and make constructor protected
P-R-O-C-H-Y File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
107 changes: 107 additions & 0 deletions
107
libraries/ESP_NOW/examples/ESP_NOW_Serial/ESP_NOW_Serial.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
ESP-NOW Serial Example - Unicast transmission | ||
Lucas Saavedra Vaz - 2024 | ||
Send data between two ESP32s using the ESP-NOW protocol in one-to-one (unicast) configuration. | ||
Note that different MAC addresses are used for different interfaces. | ||
To properly visualize the data being sent, set the line ending in the Serial Monitor to "Both NL & CR". | ||
*/ | ||
|
||
#include "ESP32_NOW_Serial.h" | ||
#include "MacAddress.h" | ||
#include "WiFi.h" | ||
|
||
// 0: AP mode, 1: Station mode | ||
#define ESPNOW_WIFI_MODE_STATION 0 | ||
|
||
// Channel to be used by the ESP-NOW protocol from 1 to 13 | ||
#define ESPNOW_WIFI_CHANNEL 1 | ||
|
||
#if ESPNOW_WIFI_MODE_STATION // ESP-NOW using WiFi Station mode | ||
#define ESPNOW_WIFI_IF WIFI_IF_STA | ||
#define GET_IF_MAC WiFi.macAddress | ||
|
||
// Station mode MAC addresses of the devices in the network | ||
// Device 1 - F4:12:FA:42:B6:E8 | ||
const MacAddress mac1({0xF4, 0x12, 0xFA, 0x42, 0xB6, 0xE8}); | ||
// Device 2 - F4:12:FA:40:64:4C | ||
const MacAddress mac2({0xF4, 0x12, 0xFA, 0x40, 0x64, 0x4C}); | ||
#else // ESP-NOW using WiFi AP mode | ||
#define ESPNOW_WIFI_IF WIFI_IF_AP | ||
#define GET_IF_MAC WiFi.softAPmacAddress | ||
|
||
// AP mode MAC addresses of the devices in the network | ||
// Device 1 - F6:12:FA:42:B6:E8 | ||
const MacAddress mac1({0xF6, 0x12, 0xFA, 0x42, 0xB6, 0xE8}); | ||
// Device 2 - F6:12:FA:40:64:4C | ||
const MacAddress mac2({0xF6, 0x12, 0xFA, 0x40, 0x64, 0x4C}); | ||
#endif | ||
|
||
ESP_NOW_Serial_Class *esp_now_serial; | ||
|
||
void setup() { | ||
MacAddress current_mac; | ||
|
||
Serial.begin(115200); | ||
|
||
Serial.print("WiFi Mode: "); | ||
|
||
#if ESPNOW_WIFI_MODE_STATION | ||
Serial.println("STA"); | ||
WiFi.mode(ESPNOW_WIFI_MODE); | ||
esp_wifi_set_channel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE); | ||
lucasssvaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#else | ||
Serial.println("AP"); | ||
WiFi.softAP(WiFi.getHostname(), NULL, ESPNOW_WIFI_CHANNEL, 1); | ||
#endif | ||
|
||
Serial.print("Channel: "); | ||
Serial.println(ESPNOW_WIFI_CHANNEL); | ||
|
||
String mac = GET_IF_MAC(); | ||
Serial.print("MAC Address: "); | ||
current_mac.fromString(mac); | ||
Serial.println(mac); | ||
|
||
WiFi.disconnect(); | ||
lucasssvaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (current_mac == mac1) | ||
{ | ||
Serial.println("I'm the Device 1\n"); | ||
// Pass the MAC address of the other device to the ESP_NOW_Serial_Class | ||
esp_now_serial = new ESP_NOW_Serial_Class(mac2, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF); | ||
} | ||
else if (current_mac == mac2) | ||
{ | ||
Serial.println("I'm the Device 2\n"); | ||
// Pass the MAC address of the other device to the ESP_NOW_Serial_Class | ||
esp_now_serial = new ESP_NOW_Serial_Class(mac1, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF); | ||
} | ||
else | ||
{ | ||
Serial.println("Unknown MAC address. Please update the mac addresses in the sketch\n"); | ||
while(1); | ||
} | ||
|
||
// Start the ESP-NOW communication | ||
Serial.println("ESP-NOW communication starting..."); | ||
esp_now_serial->begin(115200); | ||
Serial.println("You can now send data between the devices using the Serial Monitor\n"); | ||
} | ||
|
||
void loop() { | ||
while (esp_now_serial->available() > 0) | ||
{ | ||
Serial.write(esp_now_serial->read()); | ||
} | ||
|
||
while (Serial.available() && esp_now_serial->availableForWrite()) | ||
{ | ||
if (esp_now_serial->write(Serial.read()) <= 0) | ||
{ | ||
Serial.println("Failed to send data"); | ||
break; | ||
} | ||
} | ||
|
||
delay(1); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name=ESP_NOW | ||
version=1.0.0 | ||
author=me-no-dev | ||
maintainer=P-R-O-C-H-Y | ||
sentence=Library for ESP_NOW | ||
paragraph=Supports ESP32 Arduino platforms. | ||
category=Sensor | ||
url=https://github.com/espressif/arduino-esp32/ | ||
architectures=esp32 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.