BT Runtime error on receiving and splitting string

Hello there!

Can anyone help me figure out what I am doing wrong?
I am building an App that connects via BT to esp32 and every 300ms send a character to esp32, get a reply and read a string with two numbers. Then separate them (using 'split text at "|" ' block) and display on screen. App seems to work fine exept once in a while i get pop up message:
"Select list item: List index too large
Select list item: Attempt to get item number 2 of a list of length 1:[" "] "

Here is what esp32 sends: OutputValues = (String)LeftRight + "|" + (String)FrontBack;
Block likely causing issue:

Here is my .aia:
My_Project_2.aia (7.7 KB)

Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

Some people send temperature and humidity in separate messages with distinctive prefixes like "t:" (for temperature) and "h:" (for humidity).
(That's YAML format.)

The AI2 Charts component can recognize these and graph them. See Bluetooth Client Polling Rate - #12 by ABG

To receive YAML format messages, test if the incoming message contains ':' . If true, split it at ':' into a list variable, and find the prefix in item 1 and the value in item 2.

...

When you split the received string into a List, check the List Length before trying to assign List items to labels. This is because occasionally the received string can be at fault, especially with fast process times.

Thank you for your reply!
So,

  1. Print Line() on esp32 side looks like:
    OutputValues = (String)LeftRight + "," + (String)FrontBack; SerialBT.println(OutputValues);
    Should be ok, right?
  2. Delimiter attribute set to 10.
  3. Esp32 produce numbers every 400ms, my clock set for 300ms

I am pretty new to Mit App inventor and still don't completly understand all that "blocks" concept. I do understand what you suggest to ad to my BtConnectionCheck.timer, but i am stupid enogh not to get HOW to do that. Can you ,please, help me to do nesessary mods to my Clock block?
Here is my block now:


Thank again!!

Thanks for your reply!
As, i said in reply to ABG, my problem is not what to do, but how to do it. Would you be kind enogh to show me actual blocks for checking the List Length?
Thanks again!

Thank you all very much!!

I manage to solve my problem. For anyone having simular issue, here is my working block:

If anyone feels like there should be anything else to improve, let me know and i will implement it.

Thanks,
Alex.Vosk

You didn't solve the problem.

You missed the -1 to use the Delimiter, so you might get truncated messages.

Here is an updated blocks sample illustrating these ideas ...

BlueTooth_delimiter_sample.aia (3.4 KB) global message

Thank you ABG!
I was thinking that my problem solved when i got rid of annoing 'Runtime error', but looking at your blocks, i just realize how far i am from perfecting MIT App Inventor :slight_smile:
Would you please, ansver a few questions?

  1. You initialize three labels. Label2 and Label3 are for my splitted values, but what Label4 for?
  2. You set DelimiterByte to 10 in Screen.Initialize. Is it the same as setting it in Designer view?

Thanks a lot for looking in my problem,
Alex.Vosk

This was a Canned Reply, one size fits all. Trim it to fit.

It's easier to notice in the Blocks this way, for demo purposes.
Otherwise, the same.

Thank you!
Last question, probably stupid one. How can i assgn splitted values (Label2 and Label3) to global variables, so i can use it elsewhere?

http://www.appinventor.org/bookChapters/chapter16.pdf

:+1: Thanks!