App crashing when receiving data via Bluetooth

Hi guys, I’m having a problem that I don’t understand and I was wondering if anyone could help me out.

I have an Arduino sending a string of text once per second to the app. The string contains various ‘values’ separated by a “|” symbol. I then have a timer in the app that takes the string, splits it up according the the “|” symbol, and populates a number of text labels with the values.

edit: I forgot to mention that the app uses a timer that fires every 500ms.

Now this all works fine but after running for a few seconds, the app crashes saying that “the list index is too large”. I’m attaching a picture of the block that does this and as you can see there is a "Set ‘global list’ to ‘create empty list’ which I thought would clear the list every time! but it I guess that’s not working?

Any help would be greatly appreciated!
Tom


Please see the Delimiter article in FAQ

Please see the Delimiter article in FAQ

Be sure to use println() at the end of each message to send from the Arduino, to signal end of message. Do not rely on timing for this, which is unreliable.

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 Arduino, to not flood the AI2 buffers.

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 Arduino sneaks in some commentary messages with the data values.

1 Like

Hello Tom

In addition to ABG’s advice about the Arduino Sketch, it would be wise to slow everything down to what is really required. The human eye cannot watch values changing every 500ms, that is simply way too fast. Every 6 to 10 seconds would be more reasonable…

1 Like

Hi, Tom

I am doing something very similar to what you are doing. I am running my clock times a 200ms on the app and on the Arduino, sending large amounts of data without any problems at all.

Try changing your Global List to a local List, that way it is re-initialized each time.

Also on the Arduino side, set a Start and End Value of some sort so you can check the data before you do anything. In my case I use “RoD” as the start and “Z” as then end of my incoming data.

If the Start and End values are there, I assume the data was received correctly, if not I make another request to Arduino to re-send the data.

I send most of my returned data to a web page JS script, but extract some data for use within the app. (the JS timer is also set at 200ms on the web page without any problems at all)

See my blocks below.

Hope that helps.

Mike.

1 Like

Try this simple code:
http://kio4.com/appinventor/9B1_bluetooth_temp_humed.htm

1 Like

Hi Mike,
Thanks for sharing this! Would you mind sharing more of your blocks? I’m new to this and am having a hard time wrapping my head around all of this. Also, why do you send the character “o” near the top of your of your block? Does your Arduino only send the data when it sees this character? I made my that it sends the data in a loop when it’s just siting idle and also as needed whenever it’s a variety of while loops.

Tom

Hi TOM,

My app is HTML/JavaScript based. I use the App to display a Webviewer page, and to communicate with the Arduino. All my functions, on/off timers, buttons and coding is written in JavaScript and HTML. I chose this method mainly because the layout capabilities of App Inventor are very poor, and I wanted my App to look professional.

There is a JavaScript timer on the HTML page that fires every 200ms, it sends a “Read Only” request to the app that’s the “o” you are asking about. When the app see this, it sends a request for read only data from Arduino.

When a user changes any settings on the HTML page, it sends a “Save Request”, and saves the values to Tinydb. So when the App is reloaded all user settings are sent back to the HTML page and restored.

When any setting is changed that needs to be sent to Arduino, to turn something on/off, etc. Then the HTML page sends a “Send Data” request, and the values are sent to Arduino.

It works extremely well, it is even fast enough to send the PWM signals from Arduino in real time, so as the lights are ramping up, the percentage of brightness is updating in real time on the HTML page.

And also because it is being displayed on the Webviewer, and not on an App Inventor screen, and the fact that the JavaScript timer sends a request for “Read Only” data to the App every 200ms, the App will not go into sleep mode, so it can run 24/7 with no problems.

I designed the App to be used on a 7” tablet, dedicated for use as the Main Controller.

Here is a screen shot, I am still tweaking the graphical layout a bit, but the main coding is done, except for the Aquarium water parameters and Livestock log data base.

Mike.

1 Like

Mike,
Your GUI looks pretty good and it sounds like an awesome project! You must be really good at this.
So you the html page send a command to the app which in turn sends a command to the Arduino? So you don’t have clock in the app to trigger the getting of the bluetooth data? Sorry, I’m obviously slightly over my head here…
Also, as for variables. Do you just have the 2? The “Read Only” global and the “temp” local?

Tom

Here are a few shots of some of the setting and help screens.

Hi TOM,

The data is sent like this from the HTML page, this just a small sample:

Any data that needs to be saved to Tinydb arrives like this:

"SAVE#WLtsOn#WLtsOff#WEnabled#WDurStep#WmaxVal#BLtsOn#BLtsOff#BEnabled#BDurStep#BMaxVal#LightMode#RMaxVal”

So when the App WebView sees the word “SAVE”, it knows no data needs to be sent to Arduino, only saved it to Tinydb.

The same is true for when the HTML page sends setting values that need to be sent to Arduino only the key word is “SendData”, so it then sends the values to Arduino.

The “ReadOnly” data request is fired every 200ms from the HTML page, so the app is constantly asking Arduino for updated output states. It then sends them back to the HTML page with a new keyword “DataUpdate”. The JavaScript timers see this, stops asking for updates, loads the data, then resumes asking for updates.

There is a lot more to it but that’s the general idea.

Mike.