From b2ec27de1d2d4703a15f6397550a46896413828a Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Fri, 19 Aug 2022 14:05:18 +0200 Subject: [PATCH 1/4] Removed option to change CHANNEL --- .../examples/ESPNow/Basic/Master/Master.ino | 109 +++++++++--------- .../examples/ESPNow/Basic/Slave/Slave.ino | 5 +- 2 files changed, 54 insertions(+), 60 deletions(-) diff --git a/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino b/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino index ea340980a42..98f33290d9a 100644 --- a/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino +++ b/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino @@ -34,17 +34,16 @@ // Global copy of slave esp_now_peer_info_t slave; -#define CHANNEL 1 -#define PRINTSCANRESULTS 0 -#define DELETEBEFOREPAIR 0 +bool slaveFound; +#define PRINT_ALL_SCAN_RESULTS 0 +#define DELETE_PEER_BEFORE_PAIRING 0 // Init ESP Now with fallback void InitESPNow() { WiFi.disconnect(); if (esp_now_init() == ESP_OK) { Serial.println("ESPNow Init Success"); - } - else { + } else { Serial.println("ESPNow Init Failed"); // Retry InitESPNow, add a counte and then restart? // InitESPNow(); @@ -57,53 +56,59 @@ void InitESPNow() { void ScanForSlave() { int8_t scanResults = WiFi.scanNetworks(); // reset on each scan - bool slaveFound = 0; + slaveFound = false; memset(&slave, 0, sizeof(slave)); Serial.println(""); if (scanResults == 0) { Serial.println("No WiFi devices in AP Mode found"); } else { - Serial.print("Found "); Serial.print(scanResults); Serial.println(" devices "); - for (int i = 0; i < scanResults; ++i) { - // Print SSID and RSSI for each device found - String SSID = WiFi.SSID(i); - int32_t RSSI = WiFi.RSSI(i); - String BSSIDstr = WiFi.BSSIDstr(i); - - if (PRINTSCANRESULTS) { - Serial.print(i + 1); + if (PRINT_ALL_SCAN_RESULTS) { + Serial.print("Found "); Serial.print(scanResults); Serial.println(" devices "); + for (int i = 0; i < scanResults; ++i) { + // Print SSID and RSSI for each device found + String SSID = WiFi.SSID(i); + int32_t RSSI = WiFi.RSSI(i); + String BSSIDstr = WiFi.BSSIDstr(i); + Serial.print(i); Serial.print(": "); Serial.print(SSID); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println(""); - } - delay(10); + } // for - loop through scanResults + } // if PRINT_ALL_SCAN_RESULTS + + for (int i = 0; i < scanResults; ++i) { + // Print SSID and RSSI for each device found + String SSID = WiFi.SSID(i); + int32_t RSSI = WiFi.RSSI(i); + String BSSIDstr = WiFi.BSSIDstr(i); + // Check if the current device starts with `Slave` if (SSID.indexOf("Slave") == 0) { // SSID of interest - Serial.println("Found a Slave."); - Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println(""); + Serial.print("Found a Slave: "); Serial.print(i); Serial.print(": "); Serial.print(SSID); + Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); + Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println(""); // Get BSSID => Mac Address of the Slave - int mac[6]; - if ( 6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] ) ) { - for (int ii = 0; ii < 6; ++ii ) { - slave.peer_addr[ii] = (uint8_t) mac[ii]; - } + uint8_t mac[6]; + if (6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] ) ) { + Serial.printf("Adding peer with MAC [%02x:%02x:%02x:%02x:%02x:%02x]\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + memcpy(slave.peer_addr, mac, 6); } - slave.channel = CHANNEL; // pick a channel - slave.encrypt = 0; // no encryption + slave.channel = 0; // 0 = Use whatever channel the Slave (AP) is using + slave.encrypt = false; // no encryption - slaveFound = 1; + slaveFound = true; // we are planning to have only one slave in this example; // Hence, break after we find one, to be a bit efficient break; - } - } - } + } // if SSID starts with "Slave" + } // for - loop in scanResults + } // scanResults > 0 if (slaveFound) { Serial.println("Slave Found, processing.."); @@ -118,15 +123,14 @@ void ScanForSlave() { // Check if the slave is already paired with the master. // If not, pair the slave with master bool manageSlave() { - if (slave.channel == CHANNEL) { - if (DELETEBEFOREPAIR) { + if (slaveFound) { + if (DELETE_PEER_BEFORE_PAIRING) { deletePeer(); } Serial.print("Slave Status: "); // check if the peer exists - bool exists = esp_now_is_peer_exist(slave.peer_addr); - if ( exists) { + if (esp_now_is_peer_exist(slave.peer_addr)) { // Slave already paired. Serial.println("Already Paired"); return true; @@ -140,23 +144,18 @@ bool manageSlave() { } else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) { // How did we get so far!! Serial.println("ESPNOW Not Init"); - return false; } else if (addStatus == ESP_ERR_ESPNOW_ARG) { Serial.println("Invalid Argument"); - return false; } else if (addStatus == ESP_ERR_ESPNOW_FULL) { Serial.println("Peer list full"); - return false; } else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) { Serial.println("Out of memory"); - return false; } else if (addStatus == ESP_ERR_ESPNOW_EXIST) { Serial.println("Peer Exists"); - return true; } else { - Serial.println("Not sure what happened"); - return false; + Serial.println("Undefined Error"); } + return false; } } else { // No slave found to process @@ -183,16 +182,19 @@ void deletePeer() { } } -uint8_t data = 0; // send data void sendData() { - data++; + static uint8_t data = 0; const uint8_t *peer_addr = slave.peer_addr; - Serial.print("Sending: "); Serial.println(data); + char peer_addr_str[18]; + snprintf(peer_addr_str, sizeof(peer_addr_str), "%02x:%02x:%02x:%02x:%02x:%02x", + peer_addr[0], peer_addr[1], peer_addr[2], peer_addr[3], peer_addr[4], peer_addr[5]); + Serial.print("Sending: "); Serial.print(data); Serial.print(" to addr: "); Serial.println(peer_addr_str); esp_err_t result = esp_now_send(peer_addr, &data, sizeof(data)); - Serial.print("Send Status: "); + Serial.print("Send Status code= "); Serial.print(result); Serial.print(": "); if (result == ESP_OK) { Serial.println("Success"); + data++; } else if (result == ESP_ERR_ESPNOW_NOT_INIT) { // How did we get so far!! Serial.println("ESPNOW not Init."); @@ -209,13 +211,13 @@ void sendData() { } } -// callback when data is sent from Master to Slave +// Callback when data is sent from Master to Slave void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { char macStr[18]; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); Serial.print("Last Packet Sent to: "); Serial.println(macStr); - Serial.print("Last Packet Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); + Serial.print("Last Packet Send Status code="); Serial.print(status); Serial.print(" : "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); } void setup() { @@ -235,13 +237,10 @@ void setup() { void loop() { // In the loop we scan for slave ScanForSlave(); - // If Slave is found, it would be populate in `slave` variable - // We will check if `slave` is defined and then we proceed further - if (slave.channel == CHANNEL) { // check if slave channel is defined - // `slave` is defined + // Check if slave was found + if (slaveFound) { // Add slave as peer if it has not been added already - bool isPaired = manageSlave(); - if (isPaired) { + if(manageSlave()){ // pair success or already paired // Send data to device sendData(); @@ -250,10 +249,6 @@ void loop() { Serial.println("Slave pair failed!"); } } - else { - // No slave found to process - } - // wait for 3seconds to run the logic again delay(3000); } diff --git a/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino b/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino index d9029e961e3..e61499ca6e2 100644 --- a/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino +++ b/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino @@ -32,7 +32,6 @@ #include #include -#define CHANNEL 1 // Init ESP Now with fallback void InitESPNow() { @@ -52,7 +51,7 @@ void InitESPNow() { // config AP SSID void configDeviceAP() { const char *SSID = "Slave_1"; - bool result = WiFi.softAP(SSID, "Slave_1_Password", CHANNEL, 0); + bool result = WiFi.softAP(SSID, "Slave_1_Password"); if (!result) { Serial.println("AP Config failed."); } else { @@ -87,5 +86,5 @@ void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { } void loop() { - // Chill + // Behavior is handled in callback function } From fe3ea447d5e34677fa16671ffc712400295ea829 Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Wed, 24 Aug 2022 15:22:36 +0200 Subject: [PATCH 2/4] Revert "Removed option to change CHANNEL" This reverts commit b2ec27de1d2d4703a15f6397550a46896413828a. --- .../examples/ESPNow/Basic/Master/Master.ino | 109 +++++++++--------- .../examples/ESPNow/Basic/Slave/Slave.ino | 5 +- 2 files changed, 60 insertions(+), 54 deletions(-) diff --git a/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino b/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino index 98f33290d9a..ea340980a42 100644 --- a/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino +++ b/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino @@ -34,16 +34,17 @@ // Global copy of slave esp_now_peer_info_t slave; -bool slaveFound; -#define PRINT_ALL_SCAN_RESULTS 0 -#define DELETE_PEER_BEFORE_PAIRING 0 +#define CHANNEL 1 +#define PRINTSCANRESULTS 0 +#define DELETEBEFOREPAIR 0 // Init ESP Now with fallback void InitESPNow() { WiFi.disconnect(); if (esp_now_init() == ESP_OK) { Serial.println("ESPNow Init Success"); - } else { + } + else { Serial.println("ESPNow Init Failed"); // Retry InitESPNow, add a counte and then restart? // InitESPNow(); @@ -56,59 +57,53 @@ void InitESPNow() { void ScanForSlave() { int8_t scanResults = WiFi.scanNetworks(); // reset on each scan - slaveFound = false; + bool slaveFound = 0; memset(&slave, 0, sizeof(slave)); Serial.println(""); if (scanResults == 0) { Serial.println("No WiFi devices in AP Mode found"); } else { - if (PRINT_ALL_SCAN_RESULTS) { - Serial.print("Found "); Serial.print(scanResults); Serial.println(" devices "); - for (int i = 0; i < scanResults; ++i) { - // Print SSID and RSSI for each device found - String SSID = WiFi.SSID(i); - int32_t RSSI = WiFi.RSSI(i); - String BSSIDstr = WiFi.BSSIDstr(i); - Serial.print(i); - Serial.print(": "); - Serial.print(SSID); - Serial.print(" ("); - Serial.print(RSSI); - Serial.print(")"); - Serial.println(""); - } // for - loop through scanResults - } // if PRINT_ALL_SCAN_RESULTS - + Serial.print("Found "); Serial.print(scanResults); Serial.println(" devices "); for (int i = 0; i < scanResults; ++i) { // Print SSID and RSSI for each device found String SSID = WiFi.SSID(i); int32_t RSSI = WiFi.RSSI(i); String BSSIDstr = WiFi.BSSIDstr(i); + if (PRINTSCANRESULTS) { + Serial.print(i + 1); + Serial.print(": "); + Serial.print(SSID); + Serial.print(" ("); + Serial.print(RSSI); + Serial.print(")"); + Serial.println(""); + } + delay(10); // Check if the current device starts with `Slave` if (SSID.indexOf("Slave") == 0) { // SSID of interest - Serial.print("Found a Slave: "); Serial.print(i); Serial.print(": "); Serial.print(SSID); - Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); - Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println(""); + Serial.println("Found a Slave."); + Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println(""); // Get BSSID => Mac Address of the Slave - uint8_t mac[6]; - if (6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] ) ) { - Serial.printf("Adding peer with MAC [%02x:%02x:%02x:%02x:%02x:%02x]\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - memcpy(slave.peer_addr, mac, 6); + int mac[6]; + if ( 6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] ) ) { + for (int ii = 0; ii < 6; ++ii ) { + slave.peer_addr[ii] = (uint8_t) mac[ii]; + } } - slave.channel = 0; // 0 = Use whatever channel the Slave (AP) is using - slave.encrypt = false; // no encryption + slave.channel = CHANNEL; // pick a channel + slave.encrypt = 0; // no encryption - slaveFound = true; + slaveFound = 1; // we are planning to have only one slave in this example; // Hence, break after we find one, to be a bit efficient break; - } // if SSID starts with "Slave" - } // for - loop in scanResults - } // scanResults > 0 + } + } + } if (slaveFound) { Serial.println("Slave Found, processing.."); @@ -123,14 +118,15 @@ void ScanForSlave() { // Check if the slave is already paired with the master. // If not, pair the slave with master bool manageSlave() { - if (slaveFound) { - if (DELETE_PEER_BEFORE_PAIRING) { + if (slave.channel == CHANNEL) { + if (DELETEBEFOREPAIR) { deletePeer(); } Serial.print("Slave Status: "); // check if the peer exists - if (esp_now_is_peer_exist(slave.peer_addr)) { + bool exists = esp_now_is_peer_exist(slave.peer_addr); + if ( exists) { // Slave already paired. Serial.println("Already Paired"); return true; @@ -144,18 +140,23 @@ bool manageSlave() { } else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) { // How did we get so far!! Serial.println("ESPNOW Not Init"); + return false; } else if (addStatus == ESP_ERR_ESPNOW_ARG) { Serial.println("Invalid Argument"); + return false; } else if (addStatus == ESP_ERR_ESPNOW_FULL) { Serial.println("Peer list full"); + return false; } else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) { Serial.println("Out of memory"); + return false; } else if (addStatus == ESP_ERR_ESPNOW_EXIST) { Serial.println("Peer Exists"); + return true; } else { - Serial.println("Undefined Error"); + Serial.println("Not sure what happened"); + return false; } - return false; } } else { // No slave found to process @@ -182,19 +183,16 @@ void deletePeer() { } } +uint8_t data = 0; // send data void sendData() { - static uint8_t data = 0; + data++; const uint8_t *peer_addr = slave.peer_addr; - char peer_addr_str[18]; - snprintf(peer_addr_str, sizeof(peer_addr_str), "%02x:%02x:%02x:%02x:%02x:%02x", - peer_addr[0], peer_addr[1], peer_addr[2], peer_addr[3], peer_addr[4], peer_addr[5]); - Serial.print("Sending: "); Serial.print(data); Serial.print(" to addr: "); Serial.println(peer_addr_str); + Serial.print("Sending: "); Serial.println(data); esp_err_t result = esp_now_send(peer_addr, &data, sizeof(data)); - Serial.print("Send Status code= "); Serial.print(result); Serial.print(": "); + Serial.print("Send Status: "); if (result == ESP_OK) { Serial.println("Success"); - data++; } else if (result == ESP_ERR_ESPNOW_NOT_INIT) { // How did we get so far!! Serial.println("ESPNOW not Init."); @@ -211,13 +209,13 @@ void sendData() { } } -// Callback when data is sent from Master to Slave +// callback when data is sent from Master to Slave void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { char macStr[18]; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); Serial.print("Last Packet Sent to: "); Serial.println(macStr); - Serial.print("Last Packet Send Status code="); Serial.print(status); Serial.print(" : "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); + Serial.print("Last Packet Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); } void setup() { @@ -237,10 +235,13 @@ void setup() { void loop() { // In the loop we scan for slave ScanForSlave(); - // Check if slave was found - if (slaveFound) { + // If Slave is found, it would be populate in `slave` variable + // We will check if `slave` is defined and then we proceed further + if (slave.channel == CHANNEL) { // check if slave channel is defined + // `slave` is defined // Add slave as peer if it has not been added already - if(manageSlave()){ + bool isPaired = manageSlave(); + if (isPaired) { // pair success or already paired // Send data to device sendData(); @@ -249,6 +250,10 @@ void loop() { Serial.println("Slave pair failed!"); } } + else { + // No slave found to process + } + // wait for 3seconds to run the logic again delay(3000); } diff --git a/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino b/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino index e61499ca6e2..d9029e961e3 100644 --- a/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino +++ b/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino @@ -32,6 +32,7 @@ #include #include +#define CHANNEL 1 // Init ESP Now with fallback void InitESPNow() { @@ -51,7 +52,7 @@ void InitESPNow() { // config AP SSID void configDeviceAP() { const char *SSID = "Slave_1"; - bool result = WiFi.softAP(SSID, "Slave_1_Password"); + bool result = WiFi.softAP(SSID, "Slave_1_Password", CHANNEL, 0); if (!result) { Serial.println("AP Config failed."); } else { @@ -86,5 +87,5 @@ void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { } void loop() { - // Behavior is handled in callback function + // Chill } From 06a9ca9b2a75e3909d9e13d3e3457a6fb265775c Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Wed, 24 Aug 2022 15:40:53 +0200 Subject: [PATCH 3/4] Fixed the ability to change CHANNEL --- libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino | 3 +++ libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino | 1 + 2 files changed, 4 insertions(+) diff --git a/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino b/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino index ea340980a42..daa0eb3cd6d 100644 --- a/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino +++ b/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino @@ -31,6 +31,7 @@ #include #include +#include // only for esp_wifi_set_channel() // Global copy of slave esp_now_peer_info_t slave; @@ -222,9 +223,11 @@ void setup() { Serial.begin(115200); //Set device in STA mode to begin with WiFi.mode(WIFI_STA); + esp_wifi_set_channel(CHANNEL, WIFI_SECOND_CHAN_NONE); Serial.println("ESPNow/Basic/Master Example"); // This is the mac address of the Master in Station Mode Serial.print("STA MAC: "); Serial.println(WiFi.macAddress()); + Serial.print("STA CHANNEL "); Serial.println(WiFi.channel()); // Init ESPNow with a fallback logic InitESPNow(); // Once ESPNow is successfully Init, we will register for Send CB to diff --git a/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino b/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino index d9029e961e3..d2b5b09b10c 100644 --- a/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino +++ b/libraries/ESP32/examples/ESPNow/Basic/Slave/Slave.ino @@ -57,6 +57,7 @@ void configDeviceAP() { Serial.println("AP Config failed."); } else { Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID)); + Serial.print("AP CHANNEL "); Serial.println(WiFi.channel()); } } From 38931d223dc68013af32c042f69079fe3fd384f9 Mon Sep 17 00:00:00 2001 From: Tomas Pilny Date: Fri, 26 Aug 2022 13:10:21 +0200 Subject: [PATCH 4/4] WiFi scan only on selected channel --- libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino b/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino index daa0eb3cd6d..b2e6b35506c 100644 --- a/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino +++ b/libraries/ESP32/examples/ESPNow/Basic/Master/Master.ino @@ -56,7 +56,7 @@ void InitESPNow() { // Scan for slaves in AP mode void ScanForSlave() { - int8_t scanResults = WiFi.scanNetworks(); + int16_t scanResults = WiFi.scanNetworks(false, false, false, 300, CHANNEL); // Scan only on one channel // reset on each scan bool slaveFound = 0; memset(&slave, 0, sizeof(slave));