Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help with Advertising interval #66

Closed
chris4git opened this issue Mar 7, 2021 · 7 comments
Closed

Need help with Advertising interval #66

chris4git opened this issue Mar 7, 2021 · 7 comments

Comments

@chris4git
Copy link

Hi,
thanks to share your work.
May you help to understand the calculation of the advertising interval ?

If I set
Advertising interval 5000 , step 62,5 ms
would the interval be 5000 X 62,5 ms = 312,5 s
or 5000 s modulo 62,5 = 5000 s ?
or 5000 ms modulo 62,5 = 80 s ?

In fact, with this value, I see an advertisement every 20040 ms !

Confusing !

@pvvx
Copy link
Owner

pvvx commented Mar 10, 2021

image

@chris4git
Copy link
Author

Ok, it's clear. So I don't understand why I receive broadcasts only once every ~20000 ms when I put 5000 as advertising interval :-/

@pvvx
Copy link
Owner

pvvx commented Mar 13, 2021

So I don't understand why I receive broadcasts only once every ~20000 ms when I put 5000 as advertising interval :-/

Where, in which packet receiving sniffer, do you receive broadcasts in 20 second increments when the ad interval is 5 seconds?
The change of data in advertising occurs after a set number of advertising intervals.

@chris4git
Copy link
Author

Where, in which packet receiving sniffer, do you receive broadcasts in 20 second increments when the ad interval is 5 seconds?
The change of data in advertising occurs after a set number of advertising intervals.

It's my own application, built on top of Linux Bluez stack 5.43.
Example of traces :

387C51 seen 19792 ms ago (1615717646)
387C51 seen 19841 ms ago (1615717665)
387C51 seen 19821 ms ago (1615717685)
387C51 seen 19772 ms ago (1615717705)
387C51 seen 39592 ms ago (1615717745)
387C51 seen 19812 ms ago (1615717764)
387C51 seen 19821 ms ago (1615717784)

When I use Android BLE Scanner it gives advertising ranges from 5000 to more than 10000 ms but often around 5000.
So I suspect my application or Bluez...

@pvvx
Copy link
Owner

pvvx commented Mar 16, 2021

Testing for several days on RTL8722DM in a noisy radio environment:
Packets Lost: 1764, Total Received: 23530 Packets
Advertising interval 2.5 seconds.
BLEScanTest.ino Ameba Arduino.

/*
   Test Advertisements Scanning
*/

#include "BLEDevice.h"

#define AD_PERIOD_MS 2500
BLEAddr inMacAddress = BLEAddr("a4:c1:38:0b:5e:ed"); // The remote data device MAC
uint32_t tik_scan, rx_all_count = 0, rx_err_count = 0;
boolean flag_restart = false;
BLEAdvertData foundDevice;

void scanFunction(T_LE_CB_DATA *p_data)
{
  foundDevice.parseScanInfo(p_data);
  if (inMacAddress == foundDevice.getAddr()) {
    uint32_t tt = millis();
    uint32_t delta = tt - tik_scan;
    tik_scan = tt;
    if (rx_all_count++) {
      if (delta > AD_PERIOD_MS + AD_PERIOD_MS / 10) {
        rx_err_count++;
      }
    }
    printf("MAC: %s ------ delta: %d ms, lost: %d, total: %d\n", foundDevice.getAddr().str(), delta, rx_err_count, rx_all_count);
    flag_restart = true;
  }
}

void setup()
{
  Serial.begin(115200);
  BLE.init();
  BLE.configScan()->setScanMode(GAP_SCAN_MODE_PASSIVE); // Active mode requests for scan response packets GAP_SCAN_MODE_PASSIVE
  BLE.configScan()->setScanInterval(420);               // Start a scan every 420ms
  BLE.configScan()->setScanWindow(420);                 // Each scan lasts for 420ms
  BLE.configScan()->updateScanParams();
  BLE.setScanCallback(scanFunction);
  BLE.beginCentral(0);
  tik_scan = millis();
  BLE.configScan()->startScan(420 * 3);
}

void loop() {
  if (flag_restart) {
    flag_restart = false;
    BLE.configScan()->stopScan();
    vTaskDelay((AD_PERIOD_MS / 4) / portTICK_RATE_MS);
  } else if (!BLE.configScan()->scanInProgress()) {
    BLE.configScan()->setScanMode(GAP_SCAN_MODE_PASSIVE); // Passive mode scan response packets
    BLE.configScan()->setScanInterval(2100);               // Start a scan every 2100ms
    BLE.configScan()->setScanWindow(2100);                 // Each scan lasts for 2100ms
    BLE.configScan()->updateScanParams();
    BLE.configScan()->startScan();
    vTaskDelay((AD_PERIOD_MS / 5) / portTICK_RATE_MS);
  } else {
    vTaskDelay(10 / portTICK_RATE_MS);
  }
}

@pvvx
Copy link
Owner

pvvx commented Mar 16, 2021

Sniffer next to Xiaomi LYWSD03MMC:
image

@jenshenk
Copy link

II don't know whether the OP found a way to fix it but just by accident I fixed it in my case and wanted to share my experience since I thought maybe it helps towards perfecting this nice project!
I've a couple of thermostats flashed with the atc1441 firmware and wanted to give this fork a try since I liked all the additional options, especially the clock. Just for context: I use BLE-tasmota flashed ESP32s with the "MI32Option2 1" to act as a BLE to MQTT bridge and forward all broadcasts immediately.
Initially I had a similar experience like the OP: All atc1441 flashed thermostats were regularly broadcasting as configured only the pvvx one was quite inconsistent ranging from every 10s to sometimes 2 minutes. I thought maybe it's the clock function, so there's no temperature broadcast during the clock is shown yet switching the clock off didn't change anything. However, switching it on again led to a constant broadcast like the atc1441 ones. Very happy with it since now I've a clock in my bathroom in addition to broadcasts that work as expected.

@pvvx pvvx closed this as completed May 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants