Advertisement
TolentinoCotesta

Untitled

Oct 12th, 2021
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <BLEDevice.h>
  3. #include <BLEUtils.h>
  4. #include <BLEScan.h>
  5. #include <BLEAdvertisedDevice.h>
  6. #include <set>
  7.  
  8. // Durata minima dell'impulso di uscita
  9. #define MIN_PULSE_TIME 5000
  10.  
  11. // Pulsante per abilitare l'associazione del device
  12. const byte Button = 0;
  13.  
  14. // Uscita attiva quando il device è in prossimità
  15. const byte LED = 2;
  16.  
  17. // Per associare un dispositivo, questo deve essere molto vicino all'ESP32
  18. int linkRSSI = -85;
  19.  
  20. // Quando il dispositivo è abbastanza vicino, esegui azione
  21. int nearRSSI = -90;
  22.  
  23. // Variabile che diventa true quando il dispositivo è abbastanza vicino
  24. bool isNearDevice = false;
  25.  
  26. // Viene usato un set (lista di elementi univoci) per memorizzare i device autorizzati
  27. std::set<std::string> allowedDevices;
  28.  
  29.  
  30. BLEScan* pBLEScan;
  31. class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
  32.     void onResult(BLEAdvertisedDevice advertisedDevice) {
  33.  
  34.       if (advertisedDevice.haveServiceUUID()) {
  35.  
  36.         // Salva in una variabile std:struing una chiave univoca ce identifica il device
  37.         std::string uuid = advertisedDevice.getServiceUUID().toString();
  38.         //std::string mac_adr = advertisedDevice.getAddress().toString();
  39.         //Serial.printf("MAC %S - UUID %s (%ddB)\n", mac_adr.c_str(), uuid.c_str(), advertisedDevice.getRSSI());
  40.  
  41.         // Controlla se il device è nella lista di quelli autorizzati
  42.         bool authorized = ( allowedDevices.count(uuid) > 0);
  43.  
  44.         // Aggiungo alla lista dei device ammessi se molto vicino e pulsante premuto
  45.         if (digitalRead(Button) == LOW) {
  46.           Serial.println("Pulsante di abbinamento premuto");
  47.  
  48.           if (advertisedDevice.getRSSI() >= linkRSSI ) {
  49.             // Se è già presente in lista -> ret.second == false
  50.             auto ret = allowedDevices.emplace(uuid);
  51.             if (ret.second) {
  52.               Serial.printf("Dispositivo aggiunto alla lista: %s (%ddB)\n", uuid.c_str(), advertisedDevice.getRSSI());
  53.             }
  54.           }
  55.         }
  56.  
  57.         // Se il dispositivo è autorizzato ed è abbastanza vicino, isNearDevice -> true
  58.         if (advertisedDevice.getRSSI() > nearRSSI && authorized) {
  59.           isNearDevice = true;
  60.           Serial.printf("Dispositivo %s autorizzato (%ddB)\n", uuid.c_str(), advertisedDevice.getRSSI());
  61.         }
  62.       }
  63.     }
  64. };
  65.  
  66. // Task che esegue continuamente lo scan dei device BLE raggiungibili
  67. void bleScanTask(void * taskPar) {
  68.   Serial.print("Task \"BLE Scanning\" avviato sul core: ");
  69.   Serial.println(xPortGetCoreID());
  70.  
  71.   while (true) {
  72.     vTaskDelay(100 / portTICK_PERIOD_MS);
  73.     BLEScanResults foundDevices = pBLEScan->start(1, true); // duration( seconds), is_continue
  74.     pBLEScan->clearResults();                               // delete results from BLEScan buffer
  75.   }
  76.  
  77.   Serial.println("Chiusura del task \"BLE scanning\"");
  78.   vTaskDelete(NULL);
  79. }
  80.  
  81.  
  82. void setup() {
  83.   Serial.begin(115200);
  84.   Serial.println();
  85.   pinMode(Button, INPUT);
  86.   pinMode(LED, OUTPUT);
  87.   digitalWrite(LED , LOW);
  88.  
  89.   Serial.println("Scanning...");
  90.   BLEDevice::init("");
  91.   pBLEScan = BLEDevice::getScan();  // Create new scan object
  92.   pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  93.   pBLEScan->setActiveScan(true);    // Active scan uses more power, but get results faster
  94.   pBLEScan->setInterval(100);
  95.   pBLEScan->setWindow(99);          // Less or equal setInterval value
  96.  
  97.   // Siccome l'esecuzione dello scanning è bloccante, facciamola in un task dedicato
  98.   xTaskCreate(
  99.     bleScanTask,     // Function to be called
  100.     "BLE Scan task", // Name of the task (for debugging)
  101.     2048,            // Stack size (bytes)
  102.     NULL,            // Parameter to pass
  103.     1,               // Task priority
  104.     NULL             // Task handle
  105.   );
  106. }
  107.  
  108. void loop() {
  109.  
  110.   // Un dispositivo è passato abbastanza vicino, attivo un segnale
  111.   static uint32_t pulseTime;
  112.   if (isNearDevice) {
  113.     pulseTime = millis();
  114.     digitalWrite(LED , HIGH);
  115.     isNearDevice = false;
  116.   }
  117.  
  118.   // Il segnale è durato abbastanza -> reset
  119.   if (millis() - pulseTime > MIN_PULSE_TIME) {
  120.     digitalWrite(LED , LOW);
  121.   }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement