This lab was dedicated to establishing a Bluetooth Low Energy (BLE) connection between the Artemis Nano
and our computer. This expands the capabilities of our robot considerably,
through features like remote control and offloading tough computations to the computer.
This lab involved Python programming on the computer side and Arduino programming on the Artemis side.
We also learned about the fundamentals of the BLE protocol.
The setup of this lab involved setting up a Python Virtual Enviornment and installing several libraries to this virtual enviornment. One of the installed libraries was Jupyter, which is what we will use to do most of the Python programming.
The first part of establishing a connection was running the Bluetooth Arduino sketch on the Artemis Nano. The next part was setting up and running the Python side. This involved putting the MAC address the Artemis Nano outputted into the connection.yaml file. Then I ran these two sections in the Jupyter Notebook to import the necessary modules and establish a conenction:
The first task was to send an ECHO command with a string from the computer to the Artemis and recieve the string echo'd back with an additional part. This didn't involve any major additions on the computer Python side, but did involve writing this piece of code on the Artemis Arduino side in the ECHO case statement that appends a ":)" to the end of the string:
After this was added and uploaded to the Artemis Board, I ran the ECHO command on the computer Python side, which successfully echo'd back an augmented string:
The second task was to send a SEND_THREE_FLOATS command with three floats from the computer to the Artemis, which will extract the three floats. Again, this didn't involve any major additions on the computer Python side, but did involve writing this piece of code on the Artemis Arduino side in the SEND_THREE_FLOATS case statement that extracts the three floats and stores the values in float_a, float_b, and float_c:
After this was added and uploaded to the Artemis Board, I ran the SEND_THREE_FLOATS command on the computer Python side:
On the Artemis side, it successfully extracted the three float values and printed them to the Serial Monitor:
The third task was to set up a notification handler on the computer side in Python that would automatically update a global variable every time the characteristic changes instead of manually calling receive_float(). This involved writing a callback function that would take as parameters a UUID and a bytearray of the characteristic value being read and save the float value of the bytearray in the global variable (named float_value). I then passed that callback function as a parameter to the start_notify(uuid,notification_handler) function, which activates notifications on the characteristic:
To test this, I continuously printed the float_value global variable that the notification handler updates:
Receiving a float using receive_float() on a characteristic defined as BLEFloatCharacteristic is different from recieving a float using receive_string() on a characteristic defined as BLECStringCharacteristic, because BLEFloatCharacteristic actually is a float. That is, it is a series of bytes that is interpreted as a float, and receive_float() returns a value as a float datatype. BLECStringCharacteristic on the other hand is essentially just a bunch of chars put together. For example, if we wanted to receive the float 3.14 from the Artemis, receive_float() on a BLEFloatCharacteristic would return the actual 3.14 value, while recieve_string() on a BLECStringCharacteristic would return "3.14" which is in most basic terms the chars "3", ".", "1", and "4" put together.