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:
- #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(100);
- 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();
- }