33 BLE + VL53L1X

My project goal is to output data from the ST VL53L1X LIDAR chip to my phone via the 33 BLE.
I have been able to transfer basic info (“BatteryMonitor” sketch) to the phone using a BT tool called nrfConnect.

So my next step is to get useful data. I have hooked up the VL53L1X chip to the 33 BLE. When connected to the PC I have successfully output continuous data from the sensor via the module to the computer, getting info on the serial port monitor (constant string of 4 digit numbers varying according to distance from subject to sensor).

Now, I have created a sketch to join the basic BT functions from “BatteryMonitor” and the serial data from the ST VL53L1X chip.

I’m getting the message “no serial profile found”.

I’m sure the code is pretty poor as I’m a newbie here, but I’m happy that at least I’ve been heading in the right direction…
Code attached…
Advice needed!

sketch_ble2.ino (2.43 KB)

geoffreybaker:
... and the serial data from the ST VL53L1X chip.

What do you mean with serial data?

geoffreybaker:
I'm getting the message "no serial profile found".

What do you do when and where do you get this message? Your BLE code works. Why did you try a String characteristic?

geoffreybaker:
Advice needed!

You must not use 16-bit UUIDs for your own services. They are reserved for services and characteristics defined by the Bluetooth SiG. You have to use 128-bit random UUIDs.

You should rewrite the loop code to remove the while() loop. Loop should run as often as possible. Make sure the sensor code and the BLE code are independent and only pass values. This will allow you to extend the code over time.

Try to reduce the code to the essential of the issue and post it using code tags instead of attaching a sketch file. Should look like this.

Example code

If not necessary avoid code that needs external hardware. This allows others without the sensor to test the code and provide hints.

Klaus_K:

What do you mean with serial data?
I mean I am looking to obtain data from the VL351X chip - it is a distance sensor, outputting continuous data in the form of 4 digits of information per line, between 0 and 9800, representing distance in millimeters. The code outputs this as a continuous stream

What do you do when and where do you get this message? Your BLE code works. Why did you try a String characteristic?
My BLE code works in so far as I can connect my phone to the Nano 33 BLE. I cannot obtain the data from the Vl351X chip as yet. (As I said earlier, I can see this data on the serial monitor when hooked up to the computer; I just cannot see it using a BT serial monitor app on my phone). I tried a string characteristic because I was assuming the error was occuring because I had not declared it. This might have something to do with me being a newb.... :slight_smile:

You must not use 16-bit UUIDs for your own services. They are reserved for services and characteristics defined by the Bluetooth SiG. You have to use 128-bit random UUIDs.
Thanks, I will try this.

You should rewrite the loop code to remove the while() loop. Loop should run as often as possible. Make sure the sensor code and the BLE code are independent and only pass values. This will allow you to extend the code over time.

Thanks for the help, will do more testing today.

So my next question would be (still using the same code as before)...
I have a BLE connection
I have a sensor outputting data to the Nano33BLE which is being output as 9600baud serial ouptut.

Do I need to transmit the data by declaring a service and then including that data output within that service?

OK, so BLE works different than what you describe. Its much better than a serial connection replacement. This BT Serial is something from Bluetooth Classic which is not supported by the Nano33BLE.

Here is what you need to do.

Think about what service and characteristic you want. Ether a high level abstraction of the application level function or low level sensor data. This is a design decision and there is no right or wrong. e.g.

VL53L1X Service with characteristics that represent the a raw values you read from the sensor e.g. a 16-bit unsigned integer value.

or a Distance service with a characteristics representation a metric or imperial value given as 16-bit unsigned integer or a float value. This will allow you to replace the sensor with a completely different one in the future without changing the client side.

Now you need to read the sensor as often as you think is necessary in the fastes use case. Then you compute the distance value and write it to the characteristic. The client can then read it at whatever rate it wants. When it subscribes to the characteristic the value is read at the maximum speed. But the client could also decide to only read it once every minute, hour or so.

So, you do not send a serial stream but you just store the actual value in the characteristic. No need for serial conversion. When you have multiple characteristics you can update them in different intervals. You do not need to take care of anything in the protocol. You just write on one side and read on the other side, in both directions.

Klaus_K:

OK, so BLE works different than what you describe. Its much better than a serial connection replacement. This BT Serial is something from Bluetooth Classic which is not supported by the Nano33BLE. --- etc...

This is really good information, than you so much. I have a somewhat different question now.
My question is that the VL351x chip is passing a stream of continuous information. Essentially it is calculating distance every 5 milliseconds, then averaging that data over repeated tests. The phone should receive this data continuously when they are synced and turned on. This would not be all the time, but a typical scenario might require the data stream for several minutes at a time, uninterrupted, and as fast as possible.
Is BLE the wrong protocol for this? Should I use BT instead?
I was using BT without difficulty, but the shield I used for BT is no longer in production, and so I went with the Nano 33 BLE as someone recommended it as a current small bluetooth board.
But perhaps it was the wrong choice?

BLEService distanceService("180C"); // User defined service
BLE.setLocalName("Nano33BLE"); // Set name for connection
BLE.addService(distanceService); // Add service
BLEStringCharacteristic distanceCharacteristic("2A19");
distanceCharacteristic.setValue(sensor.read()); // Set distance string
distanceService.addCharacteristic(distanceCharacteristic); // Add characteristic to service

Line 4 generates this error "no matching function for call to 'BLEStringCharacteristic::BLEStringCharacteristic(const char [5])'"

What am I doing wrong here?

Can you provide a link to the library for the VL53L1X sensor e.g. on GitHub or which one you picked in the library manager?

Klaus_K:

Can you provide a link to the library for the VL53L1X sensor e.g. on GitHub or which one you picked in the library manager?

sure... GitHub - pololu/vl53l1x-arduino: Pololu Arduino library for VL53L1X time-of-flight distance sensor

Klaus_K:
Think about what service and characteristic you want. Ether a high level abstraction of the application level function or low level sensor data. This is a design decision and there is no right or wrong. e.g.

There is just one thing going on between the arduino nano33 ble and the VL31Lx chip, and that is the chip is transmitting data on a constant basis once the sketch has been loaded. The data is 0-9999, in millimeters. Nothing else. All programming will be done on the Android phone app.
So it's pretty simple, really.

OK, so the sensor.read() function returns an uint16_t. You can use BLEUnsignedIntCharacteristic type.

https://www.arduino.cc/en/Reference/ArduinoBLEBLECharacteristicBLECharacteristic

To close up this thread, I'm abandoning the nano 33 BLE. Not because of coding difficulties, but because, I think, the board is failing. I get continuous com port errors (typically, I'll get an error saying no board at that com port, when I try to switch com ports it fails and becomes greyed out.

I've switched to using the nano every with the HC06 bluetooth chip and it is working perfectly, feeding serial data directly to the bluetooth device and readable on my phone. It's a much smaller form factor than my uno+shield that I started with, so I'm going to call it quits on this end of the project - it works.
Now I need to write the android code.

geoffreybaker:
To close up this thread, I’m abandoning the nano 33 BLE. Not because of coding difficulties, but because, I think, the board is failing. I get continuous com port errors (typically, I’ll get an error saying no board at that com port, when I try to switch com ports it fails and becomes greyed out.

The Arduino Nano 33 BLE and IoT have native USB and therefore virtual COM ports. This means the COM port will be restarted when the Arduino is programmed or reset. Additionally there are two USB endpoint configurations and therefore two different virtual COM ports, one for normal mode and one for bootloader mode (double click reset button). Only one is active at a time.

Windows recognizes the two different endpoint configurations and therefore assigns different COM port numbers. So, this is an issue while you develop your program because the Arduino needs to be reset a few times. But this has the advantage that you can use native USB for your applications, no need to worry about baudrates and you have the hardware UARTs for your application.

You just have to follow the same procedure while you work with the Arduinos with native USB and you will be fine.

  • double click for bootloader mode
  • select COM port
  • compile and download
  • wait for reconnect signal from Windows
  • select COM port (different number)
  • connect Serial Monitor

The older Arduinos do not have this issue because they have a separate chip on board that handles USB and never gets reset.

Good luck with your Android project.

What do you use to write the Android code? e.g. Windows/Linux PC, what tools and which phone family