Attempt to get item number 4 of a list of length 3

149 views
Skip to first unread message
Assigned to chrisw...@gmail.com by me

Boris Novosad

unread,
May 21, 2019, 4:59:24 AM5/21/19
to MIT App Inventor Forum
Hi guys, I have a problem with printing the data in the App. 

I want to transfer string via BLE to my app. I am splitting the string and showing the values continuously. 

On my program side is this:


sprintf
(myConcatenation, "%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f", ax, myStr, ay, myStr, az, myStr,gx, myStr, gy, myStr, gz, myStr, mx, myStr, my, myStr, mz, myStr, yaw, myStr, pitch, myStr, roll); //connecting many variables that I want to display in one string

pCharacteristic
->setValue(myConcatenation); //sending to the App


On my App side I have this:

stringsReceived.png


As soon as I connect and start transferring data, I got this message. Do you know what might be wrong? 

Attempt to get item number 4 of a list of length 3: (989.00 707.00 16276.)

At the same time I am dealing problem with overloading the app with data. after receiving for about 20 secs. (sending data every 100ms) App gets frozen and crashes. Do you know how to optimize the speed so data is still useful and app is working fluently? Data is accelerometer reading so its necessary to be up to date and refreshed quickly.

Thank you for any helpful advice.

Boris

Chris Ward

unread,
May 21, 2019, 7:39:41 AM5/21/19
to MIT App Inventor Forum
Hello Boris

At the end of the data string, I think there needs to be a Line Feed (\n) to tell the App EOL (End of Line, End of Data). The myStr value delimiter is definitely a comma, right? 

Now, you don't show all your blocks so I can't see how you collect the data, and that's very important when streaming. You need to ensure that the whole packet of data is received.

Concerning the freezing of the App - in part, that will be because a whole packet is not received, so the App then tries to access a value in the list that does not exist. Also, to ensure good process flow, the App Inventor process (collection of the data) should be within a Clock Timer, and the timer duration must be less than the duration of the sending device. The App Timer duration must at the same time be adequate for the processing of the data received.

In your code, you initialise 12 local values, but none of them are required - just set the list Items directly to the relevant Text Labels. Simply append the file with the data string before it is split.

That's as much as I can tell you now, hopefully it's enough to get the running better.





ABG

unread,
May 21, 2019, 2:03:36 PM5/21/19
to MIT App Inventor Forum
I am wary of the stringValues contents that you received
at the top of the BLE.StringsReceived event.

I will bet that it is a list, not a text value,
because you found it necessary to strip ( ) off the ends.
Those usually happen when you do a text operation
(like SPLIT at Comma)  on a list.

Here is a belt and suspenders bulletproof approach to
dealing with your stringValues contents..
(See attached)


Also, I have a question regarding BLE Strings ...
Is there a length limit?  
(I haven't used it, not having hardware.)

ABG




stringsReceived.png

ABG

unread,
May 21, 2019, 2:14:46 PM5/21/19
to MIT App Inventor Forum
You can also bulletproof your select item from list blocks
by replacing them with calls to the attached item() function,
which will return a harmless 0 value if out of index bounds.

ABG

item.PNG
lists.aia

Chris Ward

unread,
May 21, 2019, 2:51:47 PM5/21/19
to MIT App Inventor Forum
...some BLE hardware has a 32 byte per data limit, 72 bytes being used here, but it's easy enough to check the received strings in the output file. 

Boris Novosad

unread,
May 21, 2019, 5:00:39 PM5/21/19
to MIT App Inventor Forum
Guys thanks for your replies!

I will now send the entire canvas (firstly I didn't want so I don't confuse you or so.)

First I want to ensure you that on my first attempt I was sending only 3 values in an array like this and everything worked fine (Except for the freezing after 20 secs) -- but it was probably because of the size of myConcatenation[80] as Chris Ward mentioned :

 the App then tries to access a value in the list that does not exist

(still, don´t know how to get rid of it :( ). 

Code part:

char myConcatenation[80];
char myStr[] = ",";

sprintf(myConcatenation, "%3.2f%s%3.2f%s%3.2f", yaw, myStr, pitch, myStr, roll); //connect yaw, pitch, roll into string


App side:


appka.jpg

blocks (1).png


This was the value of MyConcatenation in Serial Monitor:

953.00,512.00,16491.00


  • Since I didn't have to put \n in the version with 3 variables I think it is not necessary to have it (I tried it and it didn't change anything).
  • Regarding the appending the file and creating new variables from "already split list", if I understand it right, I will eventually have to make new variables since I want to display them in the app, right?
Reply to ABG - I was trying to follow your advice and implement the lists.aia but I didn't get where should I call it and how it might help me, I am sorry  I am a newbie in programming. I'll try to work on it and I will let keep you up to date with my progress.


------
Actual Code used now:

char myConcatenation[3];
char myStr[] = ",";

sprintf(myConcatenation, "%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f%s%3.2f\n", ax, myStr, ay, myStr, az, myStr,gx, myStr, gy, myStr, gz, myStr, mx, myStr, my, myStr, mz, myStr, yaw, myStr, pitch, myStr, roll); //connect yaw, pitch, roll into string

Actual App canvas now:

blocks (2).png




Serial Monitor MyConcatenation Output: 

871.00,618.00,16491.00,-7.00,5.00,4.00,-144.00,2.00,162.00,109.42,4.22,4.62


So after splitting in AppInventor, I suppose that there should be 12 values.

ABG

unread,
May 21, 2019, 5:08:27 PM5/21/19
to MIT App Inventor Forum

ABG

unread,
May 21, 2019, 5:15:11 PM5/21/19
to MIT App Inventor Forum
See attached for 2 places where you steamroll a list,
making it road kill text.

ABG

stringsReceived.png

ABG

unread,
May 21, 2019, 5:45:16 PM5/21/19
to MIT App Inventor Forum
In programming as in cooking,
you have to remove data from its container
before cooking it ...

Watch for where Curly adds a can of peas to the turkey.

ABG

Chris Ward

unread,
May 21, 2019, 5:46:17 PM5/21/19
to MIT App Inventor Forum
Hi Boris

If you post your Project file I can modify it to show you what I meant about the local vars.

The reason for suggesting \n was to be sure to keep data packets separate - if there is a blip in the timing, you could then effectively have more than 12 values in the string that arrives - App Inventor is reading to the end of the string. However, based on your tests, sounds OK either with or without \n. 

Another test would be - no stream, send 12 values only and see if the App Receives all of them. If it doesn't, that probably means we have gone beyond the limit of your hardware - actually, the manufacture's notes should tell you this limit, the maximum transmission unit (MTU).

If the limit is 32bytes, your device will have to stream the data in 3 packets, 4 values each.

Chris Ward

unread,
May 21, 2019, 6:05:25 PM5/21/19
to MIT App Inventor Forum
.... don't forget to remove the Notifier when your QA testing is complete :)

Ah, ABG has shown you how to append the file as I described. We just want to make the population of the Labels more efficient too. We can do that with a simple For Block.

If the data has to arrive in 3 packets, we will need to count them. We can possibly send an App Inventor format string List directly - if that were to work, no need for the Split function.


ABG

unread,
May 21, 2019, 6:10:01 PM5/21/19
to MIT App Inventor Forum
Ah, ABG has shown you how to append the file as I described.

No, I was showing the OP how to stuff a turkey.

The Append to File block asks for text,
but the OP was cramming a list into it.

I circled the offending sequence without
correcting it in my most recent post,
for focus.

ABG
 
Message has been deleted
Message has been deleted
Message has been deleted

Chris Ward

unread,
May 21, 2019, 6:24:56 PM5/21/19
to MIT App Inventor Forum
Hi ABG

I think it may not matter that the raw list is appended to the file, but the code will need significant change anyway if the data has to be split into multiple packets.

I can foresee problems when the turkey has been stuffed for a period of time. Might need to stop the data stream after x minutes, or use a bigger phone.


Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Chris Ward

unread,
May 21, 2019, 6:28:07 PM5/21/19
to MIT App Inventor Forum
Hi again Boris

The max packet size depends on your hardware's BLE version. As I mentioned before, it should be documented by the manufacturer.

ABG

unread,
May 21, 2019, 6:29:32 PM5/21/19
to MIT App Inventor Forum
I think it may not matter that the raw list is appended to the file,

Somewhere back in this thread, the OP had complained about '(' ')' removal.
Appending a raw list to a file will add ( ) to the file.

ABG
 

Chris Ward

unread,
May 21, 2019, 6:31:05 PM5/21/19
to MIT App Inventor Forum
Boris - are those deleted messages you? Attempting to upload your .aia file? Unfortunately Google Groups does not allow .aia files that contain an extension. However, that only seems to pertain to new posts - if you edit an existing post, you should be able to attach your file.

Boris Novosad

unread,
May 21, 2019, 6:33:29 PM5/21/19
to mitappinv...@googlegroups.com
Oh, didn't know about the restriction. I try to add the aia file here then.
app.aia

Boris Novosad

unread,
May 21, 2019, 6:36:54 PM5/21/19
to MIT App Inventor Forum
Okay, so the magic is to put the pea out of a can :D Thank you ABG, now I see.

Chris, I will definitely try the test with only the 12 values tomorrow, now it's 0:30 so ill take a rest. I will attach the .aia file so you can demonstrate what you meant by the EOL. 

Chris Ward

unread,
May 21, 2019, 6:38:39 PM5/21/19
to MIT App Inventor Forum
... App Inventor is adding the brackets, so the data should handle as an App Inventor List without needing to split it - but the value separator probably needs to change to be a space . It may depend on what is to be done with the file later, but given that the streaming needs to be fast, just saving the raw data would be efficient.

Boris Novosad

unread,
May 21, 2019, 6:43:37 PM5/21/19
to MIT App Inventor Forum
I am using the ESP32 that has BLE embedded. In its datasheet, I found only this but nothing regarding the maximal packet size. Am I looking at the right place? (https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf) Page 45. - Receiver/Transmitter characteristics.

Boris

ABG

unread,
May 21, 2019, 6:45:42 PM5/21/19
to MIT App Inventor Forum
I have a quick fix for ( ) on code.appinventor.mit.edu
Your Code to Re-enter is: NAT-SUP-MAE-SORE

(Going to supper)

ABG

Capture.PNG

Chris Ward

unread,
May 21, 2019, 6:46:58 PM5/21/19
to MIT App Inventor Forum
Boris

Quick Question: What happens to the file data? Are you going to load it into the App later? If it is going into the App, we probably do not want the column headers.... 

Boris Novosad

unread,
May 21, 2019, 6:55:00 PM5/21/19
to MIT App Inventor Forum
The data are saved to the file so they might be processed afterwards. The headers are not strictly necessary here. I put them there only for the clarity.

Boris Novosad

unread,
May 21, 2019, 7:02:09 PM5/21/19
to MIT App Inventor Forum
Thank you ABG, it doesn't show any error regarding entering the list that doesn't exist. But the body of the for each loop is not being processed I think. When I take a look in .xls or check the labels of the app... they are not being refreshed. 

Buon appetite.

Boris 

Chris Ward

unread,
May 21, 2019, 7:03:32 PM5/21/19
to MIT App Inventor Forum
OK - ESP-32 is using BLE 4.2, should be good for at least 32bytes but I have seen App Inventor code somewhere using more bytes from ESP-32 - so test your 12 value string (not streaming) and if it fails, test a 4-value string. Instead of using a comma delimiter, use a space, and put a \n at the end of the string.

Chris Ward

unread,
May 21, 2019, 7:30:02 PM5/21/19
to mitappinv...@googlegroups.com
Hi Boris

Attached Project "app_cjw.aia" cleans-up bits and bobs. For incoming data, it is relying on the string being in App Inventor List Format (spaces instead of commas). We will have to modify the .StringsReceived Block if we are forced to use multiple packets.

You did not have the latest BLE extension installed. It should be edu.mit.appinventor.ble-20181124.aix, which I have added. 

StreamedBleDataBlocks.png


app_cjw.aia

Chris Ward

unread,
May 21, 2019, 7:37:40 PM5/21/19
to MIT App Inventor Forum
OK, I'm in the UK so signing off now, it's late. Happy experimenting :)

ABG

unread,
May 21, 2019, 8:40:10 PM5/21/19
to MIT App Inventor Forum
This would be a good time to mention the Blocks Editor Do It
feature, that lets you see block contents at run time.


See the Debugging section of FAQ

ABG

Boris Novosad

unread,
May 23, 2019, 2:08:12 PM5/23/19
to MIT App Inventor Forum
Hello Chris, I tried out your code and now I understand what you did there. These changes are much clearer than what I had in my blocks before. But the thing is that even when I was changing the string value sent from my ESP (using spaces instead of commas, sending only 3 values). I still cannot recieve them. I was trying to change the notify time to 500ms so it goes with the time you set in app timer. But still no data is being shown on my labels nor in the .csv file. Only thing that works nicely is that headings (that we didn't  eventually need are in the .csv file).

Chris Ward

unread,
May 23, 2019, 5:58:55 PM5/23/19
to MIT App Inventor Forum
Hi Boris

Now that's interesting :)  

What happens if you use comma delimiters again, instead of spaces? Note that, if your sample string is only 3 values, you need to modify the For Next loop accordingly so it goes from 1 to 3 instead of 1 to 12.

Timing. The time intervals can be tweaked later to improve process speed, but for now they need to be generous so that the testing is not "contaminated" by a time issue. With the App running at 500 milliseconds, the Device must send data slower than that - 800 milliseconds would be about right.

If the File does get appended, fingers and toes crossed, please attach it to your Post so that I can examine it (forensically).

You might notice your hair is getting grey from working on this Project...... be prepared for hair loss later :)

Boris Novosad

unread,
May 25, 2019, 1:03:15 PM5/25/19
to mitappinv...@googlegroups.com

Hello Chris, Thank you for your responses :) Yes my hair is getting a little bit grayish, uff ... :D but I will solve it eventually!  The file is getting appended, but I am getting it only in one column. And the weird thing that I am little bit scared of is that even if I am trying to change the size of for loop, I am getting only 3 values (ax, ay, az) Check the code part. and even if I use NRFconnect (ble app from Nordic Semiconductor) I can't see it there.


SETUP:


setup1.PNG

My notify input from Arduino IDE (weird is that it won't notify more than 3 values even I put there 9) .



This is what I get into a .csv file (all data is in column A)
Screenshot_20190525-173308.jpg

Label print doest work either :/ 

Screenshot_20190525-173243.jpg

nrfConnect output: 

Screenshot_20190525-185744.jpg



I will attach my .aia file so you can check it out. 


I would be very glad if you would try to help me solve this or give some info on my troubleshooting process.


Thank you,


Boris



app_cjw_v2.aia

Chris Ward

unread,
May 25, 2019, 4:11:59 PM5/25/19
to MIT App Inventor Forum
Hi Boris

It's not really weird - just that the device cannot send all the values in one packet. I didn't know you are using the Arduino IDE - does that mean you are using an Arduino? 

In addition to the .aia, I would need the microprocessor script (Sketch (.ino)) too. 

I can spend a bit of time on it but not until Tuesday (UK time).

ABG

unread,
May 25, 2019, 10:31:45 PM5/25/19
to MIT App Inventor Forum
It looks like you're trying to stuff a watermelon through a keyhole.

Consider sending just 1 reading per message, labeled for identification:

ax:989.0

ay:707.00

az:162.76

gx:???

gy:???

gz:???

mx:???

my:???

mz:???
...

and at the receiving end, for each incoming string,
split it at : 
check what item 1 is ('ax', 'ay', ...) then take item 2 and put it into the appropriate global variable or label.text.

Using this technique, if some of the variables change slowly, they don't
need to be sent as frequently as the others.

ABG

Boris Novosad

unread,
May 26, 2019, 11:16:10 AM5/26/19
to MIT App Inventor Forum
Hello there,

before I try to change my code (since I am a newbie in coding) I tried to "make my keyhole larger" with changing MTU (maximal transmission unit) of communication. I found that trough nrfConnect it's possible to change the value of it. Is there a possibility to implement this in AppInventor? 

nrfConnect Output:

Screenshot_20190526-161509.jpg

IMG_20190526_163434.jpg


If there's no such a function to change MTU in AppInventor I will probably have to code, right? (let's slice the melon then).


Chris, I am using the Arduino IDE with ESP32. And I wish I could implement that code. I tried codes from GitHub and StackOverflow but I just don't have the programming skill to transform them into my desires.

If you would be so kind and helped me with the implementation of the code I really would be grateful. Entire .ino file I am using (fusion of BLE and MPU example from GitHub) is circa 1000 row file and from the beginning might be confusing so I will try to navigate you a bit. 

On row no. 555  is the connection of string I want to send. 
On rows, 585 - 591 is the notify function that sends it via BLE. 

The purpose of the code is to communicate with an accelerometer so other functions and calls are mainly for its calibration or register definitions.

PS: Not to confuse you, now is the code in the state when I want to call raw values from accelerometer so every offset function or calibration are commented. (That's the reason why in Serial.Monitor are Biases and Scales = 0).


Thank you for your patience with me.

Boris


MPU9250_BUTTERFLY_RAW_V1.ino
MadgwickQuaternionUpdate.ino
MahonyQuaternionUpdate.ino

Chris Ward

unread,
May 28, 2019, 4:17:36 AM5/28/19