Open Source EMDR Machine. The Whole Blog.
To begin there is an explanation on how Bluetooth works. How does Bluetooth work?
Why I chose Bluetooth
I chose Bluetooth over WiFi or serial cables for 4 reasons:
1) Arduino Nano 33 IoT has a built-in BLE (Bluetooth Low Energy) module.
2) I want to minimize use of wires and give both users of the device (therapist and client) as much flexibility as possible.
3) It is faster and more secure than WiFi. Unlike WiFi I don’t have to hardcode the SSID and passkey in my Arduino code. The code doesn’t need to be changed if the device is moved to another place.
4) It can be used in rural places where there is no WiFi.
So I needed to find a cross platform Bluetooth package for VS.NET that works on both Windows and Android, and also a reliable Bluetooth library for Arduino.
For my project the best approach to control the EMDR device is to send a command with all the parameters and parse the whole command. This way I could get all parameters at once and set them all in the same loop() call. It is important because my command contains parameters depending on each other (for example, starting LED number and ending LED number)
Classic Bluetooth would be perfect for it, but not BLE. This is why I wanted to go with classic Bluetooth not BLE.
Software implementation of standard Bluetooth and Bluetooth Low Energy technology is very different. From a programming point of view the Bluetooth library has calls to find the needed Bluetooth device and writes in its stream whatever the developer wants. BLE is much more rigid. It introduces Services, and different types of Characteristics, all identified by guids. For a short description of BLE hierarchy see this image
For an explanation - this page Arduino Nano 33 Sense | BLE Battery Level Tutorial
As far as I know there is no way for them to understand each other.
Attempt #1
Initially I wanted to use 32feet.NET bluetooth library (GitHub InTheHand.Net.Bluetooth )on the PC/Android side and some simple Bluetooth setup+library, like SoftwareSerial library Arduino's reference for the SoftwareSerial library on Arduino side.
However this setup only works with Arduino and a separate bluetooth module, like HC-05. Classic Bluetooth functionality is blocked on Arduino Nano IoT 33. It is not a hardware limitation because the module responsible for bluetooth is based on ESP32 processor, so it can support both. It is a software limitation.
I am not sure why it was done this way, but the decision to block it limits this microcontroller functionality. It is a flaw imho.
It is possible to get Bluetooth functionality by hacking - reprogramming Bluetooth module. Instructions can be found here: Can the NANO IOT 33 run Full Bluetooth (NOT BLE)? However it will be cheating for this project so I have to work with what is available.
Attempt #2
Then I tried to use 32feet.NET BLE library. InTheHand.BluetoothLE (GitHub InTheHand.BluetoothLE ) , it has the same set of objects as ArduinoBLE so they can definitely communicate with each other. However. It is in pre-release state and I could not even add the package to my VS correctly. Nuget installation didn’t show any errors but didn’t let me use any functionality from the package.
Attempt #3
On PC/Android side I am trying to use Xamarin BLE Plugin Github xabre/xamarin-bluetooth-le: Bluetooth LE plugin for Xamarin and ArduinoBLE on the Arduino side. And it is Cross Platform which is very important for me.
This plugin is marked as pre-release but it is working fine on Windows (UWP). I haven't tested the Android part of it yet, but the functionality definitely doesn't work with an Android emulator which you can run from Visual Studio to debug your Android code. And it will make the Android related part of the project more complicated than I expected.
And another ArduinoBLE difficulty
I assumed I can use BLEStringCharacteristic and pass my whole command into one characteristic. However I found a limitation on the Arduino side - the limit for MTU (Maximum Transmission Unit) is 23 bytes and there is no way to change it. This makes the maximum length of my command string only 20 bytes. It is not enough, my command string should be about 90 bytes. So I had to make several characteristics, for 4 groups of parameters. And to make each command string less descriptive but shorter. It is not the approach I wanted but I believe is is better than creating 12 different characteristics for each parameter. (And as I mentioned before, parameters are not independent)
This approach is working so far but I am not nearly done.
Top Comments