Store sensor data string to a text file

Hello,
I am trying to learn App inventor to build an app that will be able to connect to a Bluetooth-enabled device. That device is sensing 16 sensor values over Bluetooth, 10 times a second separated by semicolons. I want to make an app that will be able to store the sensor data in a text file. each line of the text file will have all 16 sensor values separated by semicolons. So each second it will write 10 lines in the text file.

Secondly, I want to add the date and time stamp each time the Bluetooth communication starts. So in a way, the first line of the text file would be time and data and from the second line, it will start to write the sensor data with the received time stamp of those 16 values.

When the app stops or is disconnected and reconnected, it will again start writing in the same file with the current time and date from the following line:

Any suggestions on which functions to look into? I have seen the "file" block, please correct me if I'm wrong but it looks like there is no option to save the data from a string.

Also, I am separating all the data by semicolons and after the last data, there will be a "space" or some other marker to indicate the end of each set of readings. So, I need to detect those markers to detect the start and end of the data stream for each reading.

I would really appreciate it if someone can suggest to me the "function block" that I need to look into to build the App.

Thank you.

Hello Ifthekhar

That volume of data processed in such a short time interval cannot be achieved. If it could be done, what would be the practical value in such a huge file? I suspect that all 10 values per second would be identical or nearly identical.

You don't mention what Bluetooth version you are using, which is important. App Inventor has built-in blocks for 'Classic' BT and a MIT defined extension for BLE.

See my website for more on Bluetooth: ProfessorCad: Tips & Tricks

2 Likes

Hello @ChrisWard,
Thank you for your reply and for sharing your Bluetooth resources. It will be really helpful. I am planning to record a few kinematic parameters. So in my case higher the recording speed the better, I would like to have a higher resolution. Then later on I'll use that data to train an AI. But 4 to 5 times a second would also be a decent start. Do you think that it would be an achievable goal?

I already have built a proof of concept with an HC-05 and Arduino. It is only able to send the data to the App but I still don't have the data logging feature added to the program, that is the problem that I am trying to solve now first, I want to store the received sensor data in a text file, Do you have any suggestions on which functions I should use to achieve that goal? And if you can direct me to an example code that would be even better. I would be able to extrapolate the concept from there.

But finally, I want to use the TI's CC2640R2F instead of the HC-05 and the Arduino as CC2640R2F is a BLE-enabled MCU. The CC2640R2F is capable of communicating over Bluetooth 4.2 and 5.1.

Thanks again for your reply.

I think it would require everything to be perfect! While you are using the HC-05 (Classic BT) you must ensure the data stays within the 20 bytes limit (per data package = Serial Print line). The App might cope with the process better if you append the data to a Label component first (or a variable), and when the stream is stopped, write/append the file, only adding Time, Date at that point.

That EOD marker needs to be \n - in the Sketch: Serial.Println(), in the App ASCII Char '10'.

You would use the 'Append to File' Block, but the speed required I think will prohibit stream-to-file. Note that the microprocessor could write the data directly to file.

To create a file in Android via your App takes more effort than it used to because of recent Google security measures. See:

Note that the App process time interval needs to be approximately 20% faster than the microprocessor (loop) time interval. So if the data is being sent every 250 milliseconds, the App time interval would be set at 200 milliseconds. Use a timed interval in your Sketch rather than Delay(). I would start with a larger time interval, say 500 milliseconds, and tweak that until the process throws a tantrum.

Basic BT Classic file to use or plunder:
BT_Basic_Setup_Receive.aia (7.2 KB)

1 Like

Is there a way to know if the App inventor supports the CC2640R2F BLE IC? Please correct me if I'm wrong, based on the documentation that you have sheared earlier, it looks like as long as all the information related to the UUID, GATT, and UUID Handels are correct, I can connect any BLE chip using App inventor, Am I correct?

Thank you very much. And also thanks for sharing the fine details regarding the development of the app.

You are correct, but there can be unexpected exceptions which we only learn about when somebody tries them.

1 Like

cc2640r2f.pdf (5.0 MB)

@ewpatton is there anything about this MCU that would make it incompatible with the BLE Extension?

1 Like

That's true. I'll first complete the prototype with the HC-05 as a proof of concept and then will go to the CC2640R2F. So that I have a known set of working hardware and software, it will make the debug process with the CC2640R2F BLE IC much easier.

Thanks again for all the help :slightly_smiling_face: