Arduino + App Inventor Select list item: Attempt to get item number 2 of list of length 1(0)

I am researching about Arduino and control it through Blutooth and display data to Android.

I have problem with block list when design Android app. I want to display multi data sensor from Arduino ( status and temp sensor) and separate each value sensor with “|” into different label to display clearly.

When I run first programe it display on Android that “Select list item: Attempt to get item number 2 of list of lenght 1"

My ClockTimer block:

BluetoothClient in Designer:
btclient

Clock in Designer:
clock

Arduino Code:
code

I know that I need to add some block like BluetoothClient1.ReceiveText(-1), but I’m not sure how to modifiy my blocks. Thank you for support.

Hi

You are not receiving all the bytes from Ardunio. At the ReceiveText Block, set numberOfBytes to -1:

Get BT Data

That might be all there is to it. Ensure that your Ardunio Sketch Loop runs slower (10% to 20%) than the App Timer (The time needed to receive and process the data).

You also have to do a println() at the end of every transmission from your Arduino, to add the necessary New Line (decimal 10) character that tells AI2 where to look for the next message in the input stream.

Hi Chris,
Thanks a lot for reply. I changed my block to “-1” as per your suggestion.

My Serial.print is in Arduilo with delay(300) where App Timer 240.
code

The error message is still present. Am I corrent understand Sketch Loop runs? Thank you.

Note ABG’s comments.

Note also:

  1. You are running the process at an incredibly fast rate. This is most likely not necessary. Decide on the slowest rate acceptable and work with that. Once everything is working, then speeds can be tweaked if necessary.
  2. Do not use Delay() in you main Loop - It stops everything and is a probable reason why not all data is available to send. Instead, use elapsed time (milliseconds).
  3. Before selecting items from the List, check how many items it contains.

Example:
//ArduinoToApp.ino 29/10/2019 21:56:22

//Fake data stream to test App

// include the library code:
#include <SoftwareSerial.h>

//vars
unsigned int igUpdateTime;

void setup()
{
Serial.begin(9600);
igUpdateTime = millis();
}

void loop()
{
if(millis() - igUpdateTime > 1000) //Loop approx every 1 seconds
{
igUpdateTime = millis();

                     if (btSerial.connected())
                     {
                               //Bluetooth to App
                               Serial.print("Hello");
                               Serial.print("|");
                               Serial.print("World");
                               Serial.print("|");
                               Serial.println();        //This last line tells App "End of Data" = Ascii LineFeed Char Number 10
                     }
           }

}
ArduinoToApp.txt (1.1 KB)

Chris, ABG,
I corrected Arduino code as per your tips. It's clear to me and this part works very well - thanks for that. Then I changed App Timer to 500 and ... it works without errors !!!

Chris,
Please advise how to change my block as per your 3rd tip:

Good progress :slight_smile:

You are setting the variable “Input” with the data, so you can make a copy of that and send the copy to a Procedure, which:

  1. Verifies the length of the string
  2. Replaces all delimiters “|” with nothing
  3. Verifies the length of the string now.
  4. Subtracts (3) from (1) to determine how many delimiters existed - if it’s correct, split the original Input (That, and the rest of your process, should be done in the Procedure instead of the Clock Timer Block).

That method is not foolproof though, it proves the correct number of delimers is present, but if the result was, say: Hello|| (‘World’ missing), the process should accept an item of zero content but maybe we would like to count the values and reject the data if one or more values were missing. Since we do not have a built-in function, we could make our own by crawling through the data one character at a time, in a loop. Or, if we make a copy of the input var, split it, then count the items (=values). Something like this:

Hello there this is very heplfull as I have the same problem , Was the problem solved with you Czaruska ?
thanks