Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Reduce battery consumption bluefruit scanner
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Reduce battery consumption bluefruit scanner

by lemonTree27 on Wed Jun 17, 2020 12:17 pm

Hello,
I am using two nRF52 Bluefruit Feathers. One is acting as an "advertiser" whilst the other is the "scanner". The scanner is continuously scanning for the presence of the advertiser. If the advertiser is switched on and comes in range, an alarm goes off on the scanner.

What I want to do is make the scanner have as long battery life as possible.

I have already increased the scanning interval with the Bluefruit.Scanner.setInterval() function, because that intuitively makes sense. However I'm not sure whether that will have any effect because I have found the documentation to be quite limited so I'm searching in the dark. I have consulted both on the Adafruit website https://learn.adafruit.com/bluefruit-nrf52-feather-learning-guide/blescanner and the github repository https://github.com/adafruit/Adafruit_nRF52_Arduino/tree/master/libraries/Bluefruit52Lib/src.

Many thanks for any help :)

Here is the code for the scanner:

Code: Select all | TOGGLE FULL SIZE
/*********************************************************************
  This is an example for our nRF52 based Bluefruit LE modules

  Pick one up today in the adafruit shop!

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  MIT license, check LICENSE for more information
  All text above, and the splash screen below must be included in
  any redistribution
*********************************************************************/

#include <bluefruit.h>

/* For a list of EIR data types see:
      https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile
      Matching enum: cores/nRF5/SDK/components/softdevice/s132/headers/ble_gap.h */
#define VBATPIN            A7
#define BATLEDPIN          11
#define MOSFETPIN          7
#define BUZZPIN            16
unsigned long tLastSignal = 0;
int transInRange = 0;
void setup()
{
  Serial.begin(115200);
  while ( !Serial ) delay(10);   // for nrf52840 with native usb
  pinMode(BATLEDPIN,OUTPUT);
  pinMode(MOSFETPIN,OUTPUT);
  pinMode(BUZZPIN,OUTPUT);
  pinMode(33,OUTPUT);
//  pinMode(31,OUTPUT);
  digitalWrite(MOSFETPIN,LOW);
  digitalWrite(BATLEDPIN,LOW);
  digitalWrite(BUZZPIN,LOW);
  digitalWrite(33,LOW);
//  digitalWrite(31,LOW); 
  Serial.println("Bluefruit52 Central ADV Scan Example");
  Serial.println("------------------------------------\n");
  //        digitalWrite(MOSFETPIN, HIGH);
//        digitalWrite(BUZZPIN,HIGH);


  // Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
  // SRAM usage required by SoftDevice will increase dramatically with number of connections
  Bluefruit.begin(0, 1);
  Bluefruit.setTxPower(4);    // Check bluefruit.h for supported values

  /* Set the device name */
  Bluefruit.setName("Bluefruit52");

  /* Set the LED interval for blinky pattern on BLUE LED */
  Bluefruit.setConnLedInterval(100);

  /* Start Central Scanning
     - Enable auto scan if disconnected
     - Filter out packet with a min rssi
     - Interval = 100 ms, window = 50 ms
     - Use active scan (used to retrieve the optional scan response adv packet)
     - Start(0) = will scan forever since no timeout is given
  */
  //
  ////********
  //BLEUuid(0xE2, 0xC5, 0x6D, 0xB5, 0xDF, 0xFB, 0x48, 0xD2,
  //  0xB0, 0x60, 0xD0, 0xF5, 0xA7, 0x10, 0x96, 0xE0 );
  //
  //uint8_t  beaconUuid[16] =
  //{
  //  0xE2, 0xC5, 0x6D, 0xB5, 0xDF, 0xFB, 0x48, 0xD2,
  //  0xB0, 0x60, 0xD0, 0xF5, 0xA7, 0x10, 0x96, 0xE0,
  //};

  //BLEUuid BbeaconUuid(beaconUuid);
  //************
  //filterUuid(BLEUuid ble_uuid)

  Bluefruit.Scanner.setRxCallback(scan_callback);
  Bluefruit.Scanner.restartOnDisconnect(true);
//  Bluefruit.Scanner.filterRssi(-80);
  //  Bluefruit.Scanner.filterUuid(BbeaconUuid); // only invoke callback if detect bleuart service
  Bluefruit.Scanner.setInterval(160, 80);       // in units of 0.625 ms. i.e. 160*0.625 = 100 ms = 0.1 s. The brackets stand for (interval, window)
//  Bluefruit.Scanner.setInterval(1000, 80);       // in units of 0.625 ms. i.e. 160*0.625 = 100 ms = 0.1 s
  Bluefruit.Scanner.useActiveScan(false);        // Request scan response data
  Bluefruit.Scanner.start(0);                   // 0 = Don't stop scanning after n seconds

  Serial.println("Scanning ...");
}






void scan_callback(ble_gap_evt_adv_report_t* report)
{
  PRINT_LOCATION();
  uint8_t len = 0;
  uint8_t buffer[32];
  memset(buffer, 0, sizeof(buffer));

  /* Display the timestamp and device address */
  if (report->type.scan_response)
  {
    //    Serial.printf("[SR%10d] Packet received from ", millis());
  }
  else
  {
    //    Serial.printf("[ADV%9d] Packet received from ", millis());
  }
  // MAC is in little endian --> print reverse
  //  Serial.printBufferReverse(report->peer_addr.addr, 6, ':');
  //  Serial.print("\n");

  /* Raw buffer contents */
  //  Serial.printf("%14s %d bytes\n", "PAYLOAD", report->data.len);
  if (report->data.len)
  {
    //    Serial.printf("%15s", " ");
    //    Serial.printBuffer(report->data.p_data, report->data.len, '-');
    //    Serial.println();
  }
//  /* RSSI value */
//  Serial.printf("%14s %d dBm\n", "RSSI", report->rssi);

  /* Adv Type */
  //  Serial.printf("%14s ", "ADV TYPE");
  if ( report->type.connectable )
  {
    //    Serial.print("Connectable ");
  } else
  {
    //    Serial.print("Non-connectable ");
  }

  if ( report->type.directed )
  {
    //    Serial.println("directed");
  } else
  {
    //    Serial.println("undirected");
  }

  /* Shortened Local Name */
  if (Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, buffer, sizeof(buffer)))
  {
    //    Serial.printf("%14s %s\n", "SHORT NAME", buffer);
    memset(buffer, 0, sizeof(buffer));
  }

  /* Complete Local Name */
  if (Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, buffer, sizeof(buffer)))
  {
    //    Serial.printf("%14s %s\n", "COMPLETE NAME", buffer);
    memset(buffer, 0, sizeof(buffer));
  }

  /* TX Power Level */
  if (Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_TX_POWER_LEVEL, buffer, sizeof(buffer)))
  {
    //    Serial.printf("%14s %i\n", "TX PWR LEVEL", buffer[0]);
    memset(buffer, 0, sizeof(buffer));
  }

  /* Check for UUID16 Complete List */
  len = Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE, buffer, sizeof(buffer));
  if ( len )
  {
    printUuid16List(buffer, len);
  }

  /* Check for UUID16 More Available List */
  len = Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE, buffer, sizeof(buffer));
  if ( len )
  {
    printUuid16List(buffer, len);
  }

  /* Check for UUID128 Complete List */
  len = Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE, buffer, sizeof(buffer));
  if ( len )
  {
    printUuid128List(buffer, len);
  }

  /* Check for UUID128 More Available List */
  len = Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE, buffer, sizeof(buffer));
  if ( len )
  {
    printUuid128List(buffer, len);
  }

  /* Check for BLE UART UUID */
  if ( Bluefruit.Scanner.checkReportForUuid(report, BLEUART_UUID_SERVICE) )
  {
    //    Serial.printf("%14s %s\n", "BLE UART", "UUID Found!");
  }

  /* Check for DIS UUID */
  if ( Bluefruit.Scanner.checkReportForUuid(report, UUID16_SVC_DEVICE_INFORMATION) )
  {
    //    Serial.printf("%14s %s\n", "DIS", "UUID Found!");
  }

 
 
 
  /* Check for Manufacturer Specific Data */
  len = Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, buffer, sizeof(buffer));
  if (len)
  {
    uint8_t buffer2[32];
    memcpy(buffer2, buffer, sizeof(buffer2));
    memset(buffer, 0, sizeof(buffer));


    if (buffer2[4] == 0xE2 && buffer2[5] == 0xB5 && buffer2[6] == 0x6D && buffer2[7] == 0xB5 ) {
      Serial.printf("%14s ", "MAN SPEC DATA");
      Serial.printBuffer(buffer2, len, '-');
      Serial.println();
      /* RSSI value */
      Serial.printf("%14s %d dBm\n", "RSSI", report->rssi);
      Serial.println();

//**************** MAIN IMPORTANT PART ***********
      if (buffer2[5] == 0xB5){      // Groep B
        //          flashAndBeep();
          transInRange = 1; 
          tLastSignal = millis();
          Serial.println("B transmitter detected!");
      }

      else if (buffer2[5] == 0xC5){ // Groep A

      }
   
    else {
      // Do nothing
    }

    }
  }
 

  if (transInRange == 1) {
    flashAndBeep();
  }

 
  float measuredvbat = analogRead(VBATPIN);
  measuredvbat *= 2;    // we divided by 2, so multiply back
  measuredvbat *= 3.3;  // Multiply by 3.3V, our reference voltage
  measuredvbat /= 1024; // convert to voltage
  Serial.print("VBat: " ); Serial.println(measuredvbat);
  if (measuredvbat < 3.5){ //Low voltage 3,5
    digitalWrite(BATLEDPIN,HIGH);
  }
  else if (measuredvbat >= 3.5){ // High Voltage
    digitalWrite(BATLEDPIN,LOW);
  }
 
  if ( millis() - tLastSignal > 20000){
//        digitalWrite(MOSFETPIN, LOW);
//        digitalWrite(BUZZPIN,LOW);
        transInRange = 0;
        Serial.println("Transmitter no longer active");
  }
 
  // For Softdevice v6: after received a report, scanner will be paused
  // We need to call Scanner resume() to continue scanning
  Bluefruit.Scanner.resume();
}

void printUuid16List(uint8_t* buffer, uint8_t len)
{
  Serial.printf("%14s %s", "16-Bit UUID");
  for (int i = 0; i < len; i += 2)
  {
    uint16_t uuid16;
    memcpy(&uuid16, buffer + i, 2);
    Serial.printf("%04X ", uuid16);
  }
  Serial.println();
}



void printUuid128List(uint8_t* buffer, uint8_t len)
{
  (void) len;
  Serial.printf("%14s %s", "128-Bit UUID");

  // Print reversed order
  for (int i = 0; i < 16; i++)
  {
    const char* fm = (i == 4 || i == 6 || i == 8 || i == 10) ? "-%02X" : "%02X";
    Serial.printf(fm, buffer[15 - i]);
  }

  Serial.println();
}




void loop()
{
  // nothing to do
}

void flashAndBeep(void){
     Serial.print("Beacon is lighting up!");
    digitalWrite(MOSFETPIN, HIGH);
    digitalWrite(BUZZPIN,HIGH);
    delay(500); // Allow it to flash
    digitalWrite(MOSFETPIN, LOW);
    digitalWrite(BUZZPIN, LOW);
    delay(500);
}

lemonTree27
 
Posts: 10
Joined: Tue Apr 21, 2020 6:52 am

Re: Reduce battery consumption bluefruit scanner

by mikeysklar on Thu Jun 18, 2020 4:07 pm

Hello lemonTree27,

You should expect a significant power drain when running the scanner.

Also I see you are sending data out through Serial.print() type functions which will increase current draw if going thorugh the CP2104 chip.

Adding a delay() with as many milliseconds as possible in your main() loop can greatly reduce power consumption.

mikeysklar
 
Posts: 1758
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.