The Real-Time Data Logger was developed so that the data received by the MCU acting as the Central BLE (i.e., the Data Acquisition System) can be saved permanently in a certain location (e.g., Folder) of the PC (i.e., Local Database) so that it can be used later on for Machine Learning or ML (the procedure is explained in the ML section).
The Data Logger was develop based on python since python being the easiest language to learn due to its high-level API, which also guaranteed a steep learning curve for Python. Two types of Data Loggers were developed for the project:
- Text-based Data Logger
- MS Excel based Data Logger
The Text-based Data Logger has been a real necessity for the team since the ML Classifier developed by the team takes input from text files. But of course, it will not be so hard for MATLAB to take input from Excel files as well.
The Excel-based Data Logger was more complex since the string of data that were concatenated in the BLE Peripheral, i.e., sensor side to send it via a single Data Packet needs to be unpacked and cut in exact pieces of messages so that the sensor data remains intact. Making the wrong size for cutting the string will result in sensor data being mixed, thus totally corrupting the messages.
Text-Based Data Logger:
The text-based data logger just required one Python library known as “PySerial”. Microcontrollers, while sending data to a computer through a Universal Serial Bus (USB) device, target one of the Communications Port (COM Port) available. The PySerial library aims at targetting that COM port and collecting arriving data. Normally in Arduino IDE or any other IDE (e.g., Visual Studio Code), the data is being displayed on its Terminal Window, which remains directly connected to the COM port. If the IDE is displaying that data, PySerial in Python will not be able to connect to that COM port. So, the IDE running the BLE Central code should not be connected to the COM port so that the Python IDE (PyCharm used in this case) can receive the data and do whatever it is commanded to do with it afterward. For this reason, the PySerial library uses the following command to access the COM port:
ser = serial.Serial(port='COM5', baudrate=2000000, timeout=10)
So, this command takes the COM Port number, BAUD Rate, as the input. This information can be known from the Arduino (or any other IDE running the BLE code) IDE itself. It can also be learned from the Device Port Manager of the relevant PC. The “timeout” function is optional, but it can be useful for this project. It sets the length of time for which the data receiving can remain stopped while the data logger runs. In this case, the data logger will run for 10s in idle without receiving any data; after that, the program will terminate, and the files will get saved automatically.
We have four types of data (i.e., 4 Data Strings) coming from the data acquisition device: Right Leg FSR Data, Right Leg Temperature Data, Left Leg FSR Data, and Left Leg Temperature Data. These strings are saved in 4 different text files in the same folder using the following commands:
file1 = open(r'C:\Users\sakib\Desktop\Sakib Mahmud\Qatar University\Study\2020\Spring 2020\Senior Design Project (SDP)-II\Sample Collection\Text_Files\Subject_1 RL_Force.txt',"w+") file2 = open(r'C:\Users\sakib\Desktop\Sakib Mahmud\Qatar University\Study\2020\Spring 2020\Senior Design Project (SDP)-II\Sample Collection\Text_Files\Subject_1 RL_Temp.txt',"w+") file3 = open(r'C:\Users\sakib\Desktop\Sakib Mahmud\Qatar University\Study\2020\Spring 2020\Senior Design Project (SDP)-II\Sample Collection\Text_Files\Subject_1 LL_Force.txt',"w+") file4 = open(r'C:\Users\sakib\Desktop\Sakib Mahmud\Qatar University\Study\2020\Spring 2020\Senior Design Project (SDP)-II\Sample Collection\Text_Files\Subject_1 LL_Temp.txt',"w+")
This will save the data into four separate text files into the targeted directory. But each time the program is being run, the previous text files get replaced by the new ones if the name is not changed during the new iteration. So, this should be remembered while collecting data from various patients; otherwise, the previous patient data will get erased. The Text File name should be updated each time.
Finally, an infinite loop is created which runs until and unless a certain condition is evoked, which can be the number of rows reached, or the amount of time passed, or any interruption in the data, etc. as set by the user. One important aspect of the loop is to unpack the data packet every time, which is done by the following command:
reading = ser.readline().decode("utf-8", "ignore")
This command read the data coming into the targeted serial port (by ser) line by line and decoded from UNICODE to utf-8. The additional “ignore” parameter is added to ignore any faulty data which might crash the data logger forcibly. After decoding the data, a for loop was written to save data in columns (e.g., 16 columns for 16 FSR and one summed column) inside the text file, as shown in the code snippet below from the PyCharm IDE.
Here the string has been broken into 4 bytes each and saved in each column since the sent sensor data can reach up to 4 bytes (4095, due to esp32 having a 12-bit ADC). After the 16th column(One FSR reading on each), the new loop starts, which starts from the 64th Byte of the received string and saves the remaining temperature sensor data into a separate file while the previous loop saved the FSR Data into a different file.
If data comes from two peripherals during data acquisition (two legs), another variable can be included to read the second data string and save it into two other files.
Excel-Based Data Logger:
The fundamental workings of the excel-based data logger is the same except an extra library called “xlsxwriter” has been used in this case. This library helps with its commands to save data into excel.
One added feature of the excel-based data logger is that the file needs to be saved before the loop stops, which is different from the text-based logger explained above. So the following snippet is added at the end of the loop when the number of rows reaches a certain number; the workbook (Excel file) gets closed, and the program is terminated; which can be varied to run the code for a different duration.