BLE and ESP NOW

yukardo
Posts: 2
Joined: Tue Jan 29, 2019 12:02 am

BLE and ESP NOW

Postby yukardo » Tue Jan 29, 2019 12:11 am

Hi Everybody,

I am working in a project using BLE and ESP now.

But the sketch is big and overflow the memory.

El Sketch usa 1422010 bytes (108%) del espacio de almacenamiento de programa. El máximo es 1310720 bytes.
Las variables Globales usan 79268 bytes (24%) de la memoria dinámica, dejando 248412 bytes para las variables locales. El máximo es 327680 bytes.

I like to know if there is a way to optimize the sketch.

I hope you can help me. Thanks.

Here is the sketch:
  1.  
  2. #include <BLEDevice.h>
  3. #include <BLEUtils.h>
  4. #include <BLEServer.h>
  5.  
  6.  
  7. #include <esp_now.h>
  8. #include <WiFi.h>
  9.  
  10. // Global copy of slave
  11. #define NUMSLAVES 20
  12. esp_now_peer_info_t slaves[NUMSLAVES] = {};
  13. int SlaveCnt = 0;
  14.  
  15. #define CHANNEL 3
  16. #define PRINTSCANRESULTS 0
  17.  
  18. // See the following for generating UUIDs:
  19. // https://www.uuidgenerator.net/
  20.  
  21. #define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
  22. #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
  23.  
  24. //GPIO
  25. const int ADCPin = 36;
  26.  
  27. //Variables
  28. int valor;
  29. int val_pwm;
  30. char contador = 0;
  31.  
  32. // Init ESP Now with fallback
  33. void InitESPNow() {
  34.   WiFi.disconnect();
  35.   if (esp_now_init() == ESP_OK) {
  36.     Serial.println("ESPNow Init Success");
  37.   }
  38.   else {
  39.     Serial.println("ESPNow Init Failed");
  40.     // Retry InitESPNow, add a counte and then restart?
  41.     // InitESPNow();
  42.     // or Simply Restart
  43.     ESP.restart();
  44.   }
  45. }
  46.  
  47. // Scan for slaves in AP mode
  48. void ScanForSlave() {
  49.   int8_t scanResults = WiFi.scanNetworks();
  50.   //reset slaves
  51.   memset(slaves, 0, sizeof(slaves));
  52.   SlaveCnt = 0;
  53.   Serial.println("");
  54.   if (scanResults == 0) {
  55.     Serial.println("No WiFi devices in AP Mode found");
  56.   } else {
  57.     Serial.print("Found "); Serial.print(scanResults); Serial.println(" devices ");
  58.     for (int i = 0; i < scanResults; ++i) {
  59.       // Print SSID and RSSI for each device found
  60.       String SSID = WiFi.SSID(i);
  61.       int32_t RSSI = WiFi.RSSI(i);
  62.       String BSSIDstr = WiFi.BSSIDstr(i);
  63.  
  64.       if (PRINTSCANRESULTS) {
  65.         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("");
  66.       }
  67.       delay(10);
  68.       // Check if the current device starts with `Slave`
  69.       if (SSID.indexOf("Slave") == 0) {
  70.         // SSID of interest
  71.         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("");
  72.         // Get BSSID => Mac Address of the Slave
  73.         int mac[6];
  74.  
  75.         if ( 6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x%c",  &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] ) ) {
  76.           for (int ii = 0; ii < 6; ++ii ) {
  77.             slaves[SlaveCnt].peer_addr[ii] = (uint8_t) mac[ii];
  78.           }
  79.         }
  80.         slaves[SlaveCnt].channel = CHANNEL; // pick a channel
  81.         slaves[SlaveCnt].encrypt = 0; // no encryption
  82.         SlaveCnt++;
  83.       }
  84.     }
  85.   }
  86.  
  87.   if (SlaveCnt > 0) {
  88.     Serial.print(SlaveCnt); Serial.println(" Slave(s) found, processing..");
  89.   } else {
  90.     Serial.println("No Slave Found, trying again.");
  91.   }
  92.  
  93.   // clean up ram
  94.   WiFi.scanDelete();
  95. }
  96.  
  97. // Check if the slave is already paired with the master.
  98. // If not, pair the slave with master
  99. void manageSlave() {
  100.   if (SlaveCnt > 0) {
  101.     for (int i = 0; i < SlaveCnt; i++) {
  102.       const esp_now_peer_info_t *peer = &slaves[i];
  103.       const uint8_t *peer_addr = slaves[i].peer_addr;
  104.       Serial.print("Processing: ");
  105.       for (int ii = 0; ii < 6; ++ii ) {
  106.         Serial.print((uint8_t) slaves[i].peer_addr[ii], HEX);
  107.         if (ii != 5) Serial.print(":");
  108.       }
  109.       Serial.print(" Status: ");
  110.       // check if the peer exists
  111.       bool exists = esp_now_is_peer_exist(peer_addr);
  112.       if (exists) {
  113.         // Slave already paired.
  114.         Serial.println("Already Paired");
  115.       } else {
  116.         // Slave not paired, attempt pair
  117.         esp_err_t addStatus = esp_now_add_peer(peer);
  118.         if (addStatus == ESP_OK) {
  119.           // Pair success
  120.           Serial.println("Pair success");
  121.         } else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) {
  122.           // How did we get so far!!
  123.           Serial.println("ESPNOW Not Init");
  124.         } else if (addStatus == ESP_ERR_ESPNOW_ARG) {
  125.           Serial.println("Add Peer - Invalid Argument");
  126.         } else if (addStatus == ESP_ERR_ESPNOW_FULL) {
  127.           Serial.println("Peer list full");
  128.         } else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) {
  129.           Serial.println("Out of memory");
  130.         } else if (addStatus == ESP_ERR_ESPNOW_EXIST) {
  131.           Serial.println("Peer Exists");
  132.         } else {
  133.           Serial.println("Not sure what happened");
  134.         }
  135.         delay(100);
  136.       }
  137.     }
  138.   } else {
  139.     // No slave found to process
  140.     Serial.println("No Slave found to process");
  141.   }
  142. }
  143.  
  144.  
  145. uint8_t data = 0;
  146. // send data
  147. void sendData() {
  148.  
  149.   for (int i = 0; i < SlaveCnt; i++) {
  150.     const uint8_t *peer_addr = slaves[i].peer_addr;
  151.     if (i == 0) { // print only for first slave
  152.       Serial.print("Sending: ");
  153.       Serial.println(data);
  154.     }
  155.     esp_err_t result = esp_now_send(peer_addr, &data, sizeof(data));
  156.     Serial.print("Send Status: ");
  157.     if (result == ESP_OK) {
  158.       Serial.println("Success");
  159.     } else if (result == ESP_ERR_ESPNOW_NOT_INIT) {
  160.       // How did we get so far!!
  161.       Serial.println("ESPNOW not Init.");
  162.     } else if (result == ESP_ERR_ESPNOW_ARG) {
  163.       Serial.println("Invalid Argument");
  164.     } else if (result == ESP_ERR_ESPNOW_INTERNAL) {
  165.       Serial.println("Internal Error");
  166.     } else if (result == ESP_ERR_ESPNOW_NO_MEM) {
  167.       Serial.println("ESP_ERR_ESPNOW_NO_MEM");
  168.     } else if (result == ESP_ERR_ESPNOW_NOT_FOUND) {
  169.       Serial.println("Peer not found.");
  170.     } else {
  171.       Serial.println("Not sure what happened");
  172.     }
  173.     delay(100);
  174.   }
  175. }
  176.  
  177. // callback when data is sent from Master to Slave
  178. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  179.   char macStr[18];
  180.   snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
  181.            mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  182.   Serial.print("Last Packet Sent to: "); Serial.println(macStr);
  183.   Serial.print("Last Packet Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  184. }
  185.  
  186. void setup() {
  187.   Serial.begin(115200);
  188.   //Set device in STA mode to begin with
  189.   WiFi.mode(WIFI_STA);
  190.   Serial.println("ESPNow/Multi-Slave/Master Example");
  191.   // This is the mac address of the Master in Station Mode
  192.   Serial.print("STA MAC: "); Serial.println(WiFi.macAddress());
  193.   // Init ESPNow with a fallback logic
  194.   InitESPNow();
  195.   // Once ESPNow is successfully Init, we will register for Send CB to
  196.   // get the status of Trasnmitted packet
  197.   esp_now_register_send_cb(OnDataSent);
  198.  
  199.   //BLE
  200.   BLEDevice::init("MyESP32");
  201.   BLEServer *pServer = BLEDevice::createServer();
  202.   BLEService *pService = pServer->createService(SERVICE_UUID);
  203.   BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  204.                                          CHARACTERISTIC_UUID,
  205.                                          BLECharacteristic::PROPERTY_READ |
  206.                                          BLECharacteristic::PROPERTY_WRITE
  207.                                        );
  208.  
  209.   pCharacteristic->setValue("Hello World says Neil");
  210.   pService->start();
  211.   BLEAdvertising *pAdvertising = pServer->getAdvertising();
  212.   pAdvertising->start();
  213.   Serial.println("Characteristic defined! Now you can read it in your phone!");
  214.  
  215.  
  216. }
  217.  
  218. void loop() {
  219.   if (contador < 5){
  220.       // In the loop we scan for slave
  221.       ScanForSlave();
  222.  
  223.       contador++;
  224.       // If Slave is found, it would be populate in `slave` variable
  225.       // We will check if `slave` is defined and then we proceed further
  226.       if (SlaveCnt > 0) { // check if slave channel is defined
  227.       // `slave` is defined
  228.       // Add slave as peer if it has not been added already
  229.       manageSlave();
  230.       // pair success or already paired
  231.       // Send data to device
  232.       valor = analogRead(ADCPin);
  233.  
  234.       data = map(valor, 0, 4095, 0, 255);
  235.    
  236.       sendData();
  237.    
  238.       } else {
  239.         // No slave found to process
  240.     }
  241.   }
  242.   else {
  243.       valor = analogRead(ADCPin);
  244.  
  245.       data = map(valor, 0, 4095, 0, 255);
  246.    
  247.       sendData();
  248.     }
  249.   // wait for 3seconds to run the logic again
  250.   //delay(300);
  251. }
  252.  

yukardo
Posts: 2
Joined: Tue Jan 29, 2019 12:02 am

Re: BLE and ESP NOW

Postby yukardo » Thu Feb 07, 2019 10:06 am


ShreyJ
Posts: 1
Joined: Thu Jun 18, 2020 10:20 am

Re: BLE and ESP NOW

Postby ShreyJ » Thu Jun 18, 2020 10:28 am

Hi i have a similar application where i need to use esp-now and ble together. I need multiple esp32 boards to communicate with each other (2 way communication) using ble in a 2 meter radius. Can anyone help me with this? I am using Arduino 1.8.9.

Witwicked
Posts: 1
Joined: Tue Jun 22, 2021 6:14 am

Re: BLE and ESP NOW

Postby Witwicked » Wed Jun 23, 2021 6:00 am

I have a similar problem, do esp_now and ble work together?

JopWerff
Posts: 1
Joined: Sun Dec 24, 2023 9:52 am

Re: BLE and ESP NOW

Postby JopWerff » Sun Dec 24, 2023 10:32 am

yukardo wrote:
Thu Feb 07, 2019 10:06 am
Here is the solution.

https://desire.giesecke.tk/index.php/20 ... duino-ide/
Unfortunately this link is now a dead link. From the URL I can distract that it is about the same as I tried succesful.
I chose another partition schedule.
See this image: https://share.cleanshot.com/tF21Q1sR
This will compile.

Another suggestion (that I did not have tried) is to use for the BLE-stuff this alternative library:
https://www.arduino.cc/reference/en/lib ... e-arduino/
The author claims less use of memory and it is rather compatible with existing code.

I will need same kind of solution in future: combination of ESP-NOW and BLE.
So keep us informed about what solutions are working gooed.

Thnx, Jop

Who is online

Users browsing this forum: Google [Bot] and 105 guests