# 04-19 Sampling rate

# Today's Progress

I had a small issue in my BLE Scanner Class where I called a member function to start the BLE scanner in the constructor. There is nothing wrong with calling a member function in the constructor, but it had calls to the display helper to print messages to the display. This one was not yet initialized and caused the program to crash on startup, but caused no compilation errors. I had been working in a branch called "models and webservices" from the development branch and had committed many classes that were unrelated to the issue. To zero in on the bug I decided to backtrack and copy over the new files into new branches, where I could immediately now which calls caused the issue. In "feature/scanner-class" I identified the bug and in "feature/https-register-client-on-startup" I introduced yesterdays changes with the vector queue for uploading measurements.

# Problem

Aside from making the POST requests asynchronous, I am more or less finished with the uploading mechanism. I briefly touched upon throughput and sampling rates in yesterday's article. Answering questions about polling rates is going to be the next question. I want to have as much data as possible to be pushed with as few requests as possible to the server and I want this to happen in the least possible time. My current BLE Client - Server constellation only allows for 1 Hz sampling of RSSI right now.

# Possible Solutions

# Connect instead of scan

Currently I have a rather passive scanner that sniffs for advertising devices. It delivers results only at 1 Hz. The scanner works by looking for devices that are offering a service with a common UUID and returns the advertised devices when it finishes scanning. The ESP32 api only takes integer numbers as arguments that symbolize the seconds. The alternative that I have used so far is callback on device discovery provided by one of the libraries abstract classes.

My previous idea was to turn on the server every 20 ms or so and scan for longer. Like this no connection is made and it is easy to scan. This does not solve the sampling rate issue from above, as according to the docs, the callback does not return duplicates by default. So of the 50 device discoveries a second, I only get one single on in the callback. Instead of having my client in observer mode, it might make more sense to actually connect to the servers because I can then request the RSSI as often as I want.

Using client->connect(advertised->device.getAddress()); I can connect to the found devices and read a fake service with no content to get the RSSI. It seems to be possible to have multiple clients connect to the same server.

# Set want duplicates member to true

In the high level class from Espressif System, I see that the BLE Scan class (at line 78) has a private member for getting duplicates. The benefit of messing around with the scan class is that I do not have to change my codebase for a large part. The clear downside is that I have to create a custom class. There are a lot of red flags popping up in this fix, since the notion of rewritng an official class means that my angle of attack is quite possibly wrong.

# Conclusion

The main reason why I decided that the Client should be a passive observer was because of this adafruit post. It claims that "a BLE peripheral can only be connected to one central device (a mobile phone, etc.) at a time!". This is not true, as the Bluetooth 4.1 specification allows for many to many connections. The authors of the BLE library discusses it in this post support multiple client connections. I will switch to using a connection and reading a remote characteristic at a defined interval. I then have control over the sampling rate by defining the connection interval. I am not sure if a connection is steady and will allow me to request data or I have to reconnect each time. It seems like the Server notifies the client and the client then requests the data.

If my understanding of BLE connections is wrong and I cannot request data at a fast enough rate, I can still mess around with the wantDuplicates flag in the scanner.

Last Updated: 11/23/2020, 9:42:47 PM