ESP32 Scan for BLE devices with in mesh network

998 Views Asked by At

I'm working on a project where I have 3 esp32s in a mesh network that each scans for a 4th to 5th esp32 that inst in the mesh network to get the RSSI of the BLE advertisement.

I made code where I'm doing these 2 parts separately but then I try to merge them it doesn't work.

Could it be that its not possible to scan for a BLE advertisement while in a Mesh network?

Updated with Source code:


#include "painlessMesh.h"

#define   MESH_PREFIX     "cowFarmMeshNetwork"
#define   MESH_PASSWORD   "cowFarmMeshNetwork"
#define   MESH_PORT       5555

#define   COW1_UUID       "8ec76ea3-6668-48da-9866-75be8bc86f4d"
#define   COW2_UUID       "8ec76ea3-6668-48da-9866-75be8bc86f4e"
#define   DEVICE_NAME     "Device2"

String cowRssi = "";
String CowUUID = "";

Scheduler userScheduler; // to control your personal task
painlessMesh  mesh;

// User stub
void sendMessage() ; // Prototype so PlatformIO doesn't complain

Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );


void sendMessage() {
  String msg = "";
  String testS= ",";
  if(CowUUID == COW1_UUID){
    String cow = "cow1";
    msg = DEVICE_NAME + testS + cow + testS + cowRssi;
  } else if(CowUUID == COW2_UUID){
    String cow = "cow2";
    msg = DEVICE_NAME + testS + cow + testS + cowRssi;
  }

  mesh.sendBroadcast( msg );
  taskSendMessage.setInterval( random( TASK_SECOND * 1, TASK_SECOND * 5 ));
  
}

// Needed for painless library
void receivedCallback( uint32_t from, String &msg ) {
  Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
}

void newConnectionCallback(uint32_t nodeId) {
    Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}

void changedConnectionCallback() {
  Serial.printf("Changed connections\n");
}

void nodeTimeAdjustedCallback(int32_t offset) {
    Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
}

#include <Arduino.h>

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLEEddystoneURL.h>
#include <BLEEddystoneTLM.h>
#include <BLEBeacon.h>

#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8))

int scanTime = 5; //In seconds
BLEScan *pBLEScan;

String strDistance = "";

class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{
    void onResult(BLEAdvertisedDevice advertisedDevice)
    {
      if (advertisedDevice.haveManufacturerData() == true)
        {
          std::string strManufacturerData = advertisedDevice.getManufacturerData();

          uint8_t cManufacturerData[100];
          strManufacturerData.copy((char *)cManufacturerData, strManufacturerData.length(), 0);

          if (strManufacturerData.length() == 25 && cManufacturerData[0] == 0x4C && cManufacturerData[1] == 0x00)
          {
            BLEBeacon oBeacon = BLEBeacon();
            oBeacon.setData(strManufacturerData);
            String beacon = oBeacon.getProximityUUID().toString().c_str();;
            if( beacon == "8ec76ea3-6668-48da-9866-75be8bc86f4d" || beacon == "8ec76ea3-6668-48da-9866-75be8bc86f4e"){
            
              Serial.println("inside");
              CowUUID = beacon;
              cowRssi = String(advertisedDevice.getRSSI());
            }
          }
        }
      return;
    }
};

void setup() {
  Serial.begin(115200);

//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
  mesh.setDebugMsgTypes( ERROR | STARTUP );  // set before init() so that you can see startup messages

  mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT );
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);

  userScheduler.addTask( taskSendMessage );
  taskSendMessage.enable();

  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  pBLEScan->setInterval(60000);
  pBLEScan->setWindow(99); // less or equal setInterval value
}

void loop() {
  // it will run the user scheduler as well
  BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
  pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
  mesh.update();
  
}

0

There are 0 best solutions below