Can i run a Mesh network and BLE Scan on the same divice?

Duarte
Posts: 1
Joined: Fri Mar 25, 2022 2:08 pm

Can i run a Mesh network and BLE Scan on the same divice?

Postby Duarte » Fri Mar 25, 2022 2:14 pm

Hi there!

I'm trying to make a project where ill use 3 eps32 in a mesh network that scans for a 4th esp32 BLE advertisement that isn't in the mesh network to get the RSSI of the 4th esp32.

Here is some code that I'm testing but it doesn't work:
  1. #include "painlessMesh.h"
  2.  
  3. #define   MESH_PREFIX     "cowFarmMeshNetwork"
  4. #define   MESH_PASSWORD   "cowFarmMeshNetwork"
  5. #define   MESH_PORT       5555
  6.  
  7. #define   COW1_UUID       "8ec76ea3-6668-48da-9866-75be8bc86f4d"
  8. #define   COW2_UUID       "8ec76ea3-6668-48da-9866-75be8bc86f4e"
  9. #define   DEVICE_NAME     "Device2"
  10.  
  11. String cowRssi = "";
  12. String CowUUID = "";
  13.  
  14. Scheduler userScheduler; // to control your personal task
  15. painlessMesh  mesh;
  16.  
  17. // User stub
  18. void sendMessage() ; // Prototype so PlatformIO doesn't complain
  19.  
  20. Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );
  21.  
  22.  
  23. void sendMessage() {
  24.   String msg = "";
  25.   String testS= ",";
  26.   if(CowUUID == COW1_UUID){
  27.     String cow = "cow1";
  28.     msg = DEVICE_NAME + testS + cow + testS + cowRssi;
  29.   } else if(CowUUID == COW2_UUID){
  30.     String cow = "cow2";
  31.     msg = DEVICE_NAME + testS + cow + testS + cowRssi;
  32.   }
  33.  
  34.   mesh.sendBroadcast( msg );
  35.   taskSendMessage.setInterval( random( TASK_SECOND * 1, TASK_SECOND * 5 ));
  36.  
  37. }
  38.  
  39. // Needed for painless library
  40. void receivedCallback( uint32_t from, String &msg ) {
  41.   Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
  42. }
  43.  
  44. void newConnectionCallback(uint32_t nodeId) {
  45.     Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
  46. }
  47.  
  48. void changedConnectionCallback() {
  49.   Serial.printf("Changed connections\n");
  50. }
  51.  
  52. void nodeTimeAdjustedCallback(int32_t offset) {
  53.     Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
  54. }
  55.  
  56. #include <Arduino.h>
  57.  
  58. #include <BLEDevice.h>
  59. #include <BLEUtils.h>
  60. #include <BLEScan.h>
  61. #include <BLEAdvertisedDevice.h>
  62. #include <BLEEddystoneURL.h>
  63. #include <BLEEddystoneTLM.h>
  64. #include <BLEBeacon.h>
  65.  
  66. #define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8))
  67.  
  68. int scanTime = 5; //In seconds
  69. BLEScan *pBLEScan;
  70.  
  71. String strDistance = "";
  72.  
  73. class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
  74. {
  75.     void onResult(BLEAdvertisedDevice advertisedDevice)
  76.     {
  77.       if (advertisedDevice.haveManufacturerData() == true)
  78.         {
  79.           std::string strManufacturerData = advertisedDevice.getManufacturerData();
  80.  
  81.           uint8_t cManufacturerData[100];
  82.           strManufacturerData.copy((char *)cManufacturerData, strManufacturerData.length(), 0);
  83.  
  84.           if (strManufacturerData.length() == 25 && cManufacturerData[0] == 0x4C && cManufacturerData[1] == 0x00)
  85.           {
  86.             BLEBeacon oBeacon = BLEBeacon();
  87.             oBeacon.setData(strManufacturerData);
  88.             String beacon = oBeacon.getProximityUUID().toString().c_str();;
  89.             if( beacon == "8ec76ea3-6668-48da-9866-75be8bc86f4d" || beacon == "8ec76ea3-6668-48da-9866-75be8bc86f4e"){
  90.            
  91.               Serial.println("inside");
  92.               CowUUID = beacon;
  93.               cowRssi = String(advertisedDevice.getRSSI());
  94.             }
  95.           }
  96.         }
  97.       return;
  98.     }
  99. };
  100.  
  101. void setup() {
  102.   Serial.begin(115200);
  103.  
  104. //mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
  105.   mesh.setDebugMsgTypes( ERROR | STARTUP );  // set before init() so that you can see startup messages
  106.  
  107.   mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT );
  108.   mesh.onReceive(&receivedCallback);
  109.   mesh.onNewConnection(&newConnectionCallback);
  110.   mesh.onChangedConnections(&changedConnectionCallback);
  111.   mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
  112.  
  113.   userScheduler.addTask( taskSendMessage );
  114.   taskSendMessage.enable();
  115.  
  116.   BLEDevice::init("");
  117.   pBLEScan = BLEDevice::getScan(); //create new scan
  118.   pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  119.   pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  120.   pBLEScan->setInterval(100);
  121.   pBLEScan->setWindow(99); // less or equal setInterval value
  122. }
  123.  
  124. void loop() {
  125.   // it will run the user scheduler as well
  126.   BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
  127.   pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
  128.   mesh.update();
  129.  
  130. }

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: Can i run a Mesh network and BLE Scan on the same divice?

Postby chegewara » Mon Apr 11, 2022 10:51 am

Sure, you can, especially that painlessMesh is wifi mesh library.

rhyssng
Posts: 1
Joined: Mon Jul 04, 2022 5:56 am

Re: Can i run a Mesh network and BLE Scan on the same divice?

Postby rhyssng » Mon Jul 04, 2022 5:58 am

Hi,
Did you manage to solve the issue?

Who is online

Users browsing this forum: Bing [Bot] and 93 guests