Nobody has tried something like what I am looking for?
I have also tried to put the pBLEScan->start
in the void loop
but i am getting some ‘pBLEscan was not declared in this scope’
/**
* A BLE client example that is rich in capabilities.
* There is a lot new capabilities implemented.
* author unknown
* updated by chegewara
*/
#include "rpcBLEDevice.h"
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include"TFT_eSPI.h"
TFT_eSPI tft;
// The remote service we wish to connect to.
//static BLEUUID serviceUUID(0xFEE0);
static BLEUUID serviceUUID(0x1101);
// The characteristic of the remote service we are interested in.
//static BLEUUID charUUID(0x2A2B);
static BLEUUID XcharUUID(0x2101);
static BLEUUID YcharUUID(0x2102);
static BLEUUID ZcharUUID(0x2103);
static BLEUUID TcharUUID(0x2104);
static BLEUUID McharUUID(0x2105);
static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
//static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLERemoteCharacteristic* pTChar;
static BLERemoteCharacteristic* pXChar;
static BLERemoteCharacteristic* pYChar;
static BLERemoteCharacteristic* pZChar;
static BLERemoteCharacteristic* pMChar;
static BLEAdvertisedDevice* myDevice;
uint8_t bd_addr[6] = {0xD3, 0xE3, 0xAE, 0xC0, 0xB9, 0x38};
//uint8_t bd_addr[6] = {0x7d, 0x18, 0x1b, 0xf1, 0xf7, 0x2c}; // MAC address: 2c:f7:f1:1b:18:7d
BLEAddress BattServer(bd_addr);
float tmp_T(0),deltaTmp_T(0);
static void temperatureNotifyCallback(
BLERemoteCharacteristic* pTChar,
uint8_t* pTData,
size_t length,
bool isNotify) {
Serial.print("Notify callback for temperature characteristic ");
Serial.print(pTChar->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
Serial.print("data: ");
Serial.println(*(uint8_t *)pTData);
Serial.println(*pTData);
Serial.println(pTData[0]);
tft.fillRect(0, 40, 100, 30, TFT_WHITE);
tft.drawString(String(*pTData), 0, 40);
deltaTmp_T=millis()-tmp_T;
Serial.print("deltaTmp_T:");
Serial.print(deltaTmp_T);
Serial.println("ms");
tmp_T=millis();
//Serial.println(pData);
}
static void accelerationModuleNotifyCallback(
BLERemoteCharacteristic* pMChar,
uint8_t* pMData,
size_t length,
bool isNotify) {
Serial.print("Notify callback for acceleration for module characteristic ");
Serial.print(pMChar->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
Serial.print("data: ");
Serial.println(*(uint8_t *)pMData);
Serial.println(*pMData);
Serial.println(pMData[0]);
//Serial.println(pData);
//fillRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color);
tft.fillRect(0, 110, 100, 30, TFT_WHITE);
tft.drawString(String(*pMData), 0, 110);
}
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
}
void onDisconnect(BLEClient* pclient) {
connected = false;
Serial.println("onDisconnect");
}
};
bool connectToServer() {
Serial.print("Forming a connection to ");
Serial.println(myDevice->getAddress().toString().c_str());
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
// Connect to the remove BLE Server.
pClient->connect(myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
Serial.println(" - Connected to server");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
Serial.println(serviceUUID.toString().c_str());
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our service");
// Obtain a reference to the characteristic in the service of the remote BLE server.
//pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
pTChar = pRemoteService->getCharacteristic(TcharUUID);
pXChar = pRemoteService->getCharacteristic(XcharUUID);
pYChar = pRemoteService->getCharacteristic(YcharUUID);
pZChar = pRemoteService->getCharacteristic(ZcharUUID);
pMChar = pRemoteService->getCharacteristic(McharUUID);
if (pTChar == nullptr || pMChar == nullptr) {
Serial.print("Failed to find our characteristic UUID ");
// Serial.println(TcharUUID.toString().c_str());
// Serial.println(XcharUUID.toString().c_str());
// Serial.println(YcharUUID.toString().c_str());
// Serial.println(ZcharUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our characteristic");
// Read the temperature characteristic value
if(pTChar->canRead()) {
Serial.println(" - can read start");
std::string value = pTChar->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
if(pTChar->canNotify())
pTChar->registerForNotify(temperatureNotifyCallback);
/*
// Read the X characteristic value
if(pXChar->canRead()) {
Serial.println(" - can read start");
std::string value = pXChar->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
if(pXChar->canNotify())
pXChar->registerForNotify(notifyCallback);
// Read the Y characteristic value
if(pYChar->canRead()) {
Serial.println(" - can read start");
std::string value = pYChar->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
if(pYChar->canNotify())
pYChar->registerForNotify(notifyCallback);
// Read the Z characteristic value
if(pZChar->canRead()) {
Serial.println(" - can read start");
std::string value = pZChar->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
if(pZChar->canNotify())
pZChar->registerForNotify(notifyCallback);
*/
// Read the acceleration module characteristic value
if(pMChar->canRead()) {
Serial.println(" - can read start");
std::string value = pMChar->readValue();
Serial.print("The acceleration characteristic value was: ");
Serial.println(value.c_str());
}
if(pMChar->canNotify())
pMChar->registerForNotify(accelerationModuleNotifyCallback);
connected = true;
return true;
}
/**
* Scan for BLE servers and find the first one that advertises the service we are looking for.
* Also in BLEscan example but with a shorter onresult
*/
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { // crée une classe MyAdvertisedDeviceCallbacks qui hérite de la classe BLEAdvertisedDeviceCallbacks
/**
* Called for each advertising BLE server.
*/
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
// We have found a device, let us now see if it contains the service we are looking for.
//int memcmp ( const void * ptr1, const void * ptr2, size_t num );
//: Compares the first num bytes of the block of memory pointed by ptr1 to the first num bytes pointed by ptr2,
//returning zero if they all match or a value different from zero representing which is greater if they do not.
if (memcmp(advertisedDevice.getAddress().getNative(),BattServer.getNative(), 6) == 0) {
Serial.print("BATT Device found: ");
Serial.println(advertisedDevice.toString().c_str());
BLEDevice::getScan()->stop();
Serial.println("new BLEAdvertisedDevice");
myDevice = new BLEAdvertisedDevice(advertisedDevice);
Serial.println("new BLEAdvertisedDevice done");
doConnect = true;
doScan = true;
} // onResult
}
}; // MyAdvertisedDeviceCallbacks
void setup() {
//initialisation of the screen
tft.begin();
tft.setRotation(3);//
tft.fillScreen(TFT_WHITE);
tft.setTextSize(2); //sets the size of text
tft.setTextColor(TFT_BLACK); //sets the text colour to black
tft.drawString("Temperature", 0, 10);
tft.drawString("Acceleration Module", 0, 80);
//initialisation of the serial connection
Serial.begin(115200);
while(!Serial){};
//delay(2000);
Serial.println("Starting Arduino BLE Client application...");
BLEDevice::init("");
// Retrieve a Scanner and set the callback we want to use to be informed when we
// have detected a new device. Specify that we want active scanning and start the
// scan to run for 5 seconds.
//BLEScan* pBLEScan = BLEDevice::getScan(); // get scanner from our client device
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); // behaviour of the scanner each time it find another bluetooth device
//setAdvertisedDeviceCallbacks, setInterval, setWindow, setActiveScan methods from the librairy BLEscan
pBLEScan->setInterval(1349); // period between 2 active scans
pBLEScan->setWindow(449); // active scan time range
pBLEScan->setActiveScan(true);//beginning of the scan
} // End of setup.
// This is the Arduino main loop function.
void loop() {
// If the flag "doConnect" is true then we have scanned for and found the desired
// BLE Server with which we wish to connect. Now we connect to it. Once we are
// connected we set the connected flag to be true.
if (doConnect == true) {//static boolean initialized as false
if (connectToServer()) {
Serial.println("We are now connected to the BLE Server.");
} else {
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
}
doConnect = false;
//display time information
} else {
BLEScanResults foundDevices = pBLEScan->start(5, false);
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
}
//Serial.println(".");
//delay(1000);
} // End of loop