Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. #include <BLEDevice.h>
  2. #include <BLEUtils.h>
  3. #include <BLEScan.h>
  4. #include <BLEAdvertisedDevice.h>
  5. #include <string>
  6. #include <BLEServer.h>
  7. #include <FS.h>
  8. #include <SD.h>
  9. #include <SPI.h>
  10. #include <sstream>
  11.  
  12. #define SERVICE_UUID        "635f284e-1ef9-11e9-ab14-d663bd873d93"
  13. #define CHARACTERISTIC_UUID "635f2ace-1ef9-11e9-ab14-d663bd873d93"
  14.  
  15. int rssi = -200;
  16. int toWrite = 1;
  17. BLEScan* pBLEScan;
  18. BLECharacteristic *pCharacteristic;
  19.  
  20. void setup() {
  21.   Serial.begin(115200);
  22.  
  23.   SD.begin(5);
  24.  
  25.   // Assegna un nome ad dispositivo bluetooth
  26.   BLEDevice::init("ESP32-Bluetooth");
  27.  
  28.   // Settaggi per il tipo di scansione BT da eseguire
  29.   pBLEScan = BLEDevice::getScan();
  30.   pBLEScan->setActiveScan(true);
  31.   pBLEScan->setInterval(500);
  32.   pBLEScan->setWindow(99);
  33.  
  34.   // Crea il server BT nel quale pubblicare il valore RSSI corrente
  35.   BLEServer *pServer = BLEDevice::createServer();
  36.   BLEService *pService = pServer->createService(SERVICE_UUID);
  37.   pCharacteristic = pService->createCharacteristic(
  38.                                          CHARACTERISTIC_UUID,
  39.                                          BLECharacteristic::PROPERTY_READ |
  40.                                          BLECharacteristic::PROPERTY_WRITE
  41.                                        );
  42.  
  43.   pCharacteristic->setValue("rssi is " + rssi);
  44.   //pService->start();
  45.  
  46.   // Effettua l'advertising del valore impostato nel passaggio precedente
  47.   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  48.   pAdvertising->addServiceUUID(SERVICE_UUID);
  49.   pAdvertising->setScanResponse(true);
  50.   pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  51.   pAdvertising->setMinPreferred(0x12);
  52.   BLEDevice::startAdvertising();
  53. }
  54.  
  55. void appendFile(fs::FS &fs, const char * path, const char * message){
  56.     Serial.printf("Appending to file: %s\n", path);
  57.  
  58.     File file = fs.open(path, FILE_APPEND);
  59.     if(!file){
  60.         Serial.println("Failed to open file for appending");
  61.         return;
  62.     }
  63.     if(file.print(message)){
  64.         toWrite = 0;
  65.         Serial.println("Message appended");
  66.     } else {
  67.         Serial.println("Append failed");
  68.     }
  69.     file.close();
  70. }
  71.  
  72.  
  73. void loop() {
  74.  
  75.   BLEScanResults foundDevices = pBLEScan->start(1, false);
  76.  
  77.   for (int i = 0; i < foundDevices.getCount(); i++)
  78.   {
  79.     BLEAdvertisedDevice app = foundDevices.getDevice(i);
  80.  
  81.     String str = app.getAddress().toString().c_str();
  82.  
  83.     if (str.equals("00:cd:ff:00:34:12") && toWrite == 1) {
  84.       Serial.println(app.getAddress().toString().c_str());
  85.       Serial.println(app.getRSSI());
  86.       pCharacteristic->setValue("rssi is " + rssi);
  87.       char str[4];
  88.       sprintf(str, "%d\n", app.getRSSI());
  89.       appendFile(SD, "/rssi.txt", str);
  90.       SD.end();
  91.     }
  92.   }
  93.  
  94.   pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
  95.  
  96.   delay(1000);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement