App Inventor - bluetooth - How to check whether whole text command is received

Hi :slight_smile:

(Originaly posted on SO)

In App Inventor, how do I make sure that the whole part of string is received?

I'm continously sending text commands between arduino and an android app. I use a function on arduino, which makes sure that the whole command arrived before processing it further:

(Credit: @Robin2)

    const byte numChars = 32;
    char receivedChars[numChars];
    boolean newData = false;

    void setup() {
        Serial.begin(38400);
    }

    void loop(){
        read_serial();
    }
    
    void read_serial() {
        static boolean recvInProgress = false;
        static byte ndx = 0;
        char startMarker = '<';
        char endMarker = '>';
        char rc;
    
        while (Serial.available() > 0 && newData == false) {
            rc = Serial.read();
    
            if (recvInProgress == true) {
                if (rc != endMarker) {
                    receivedChars[ndx] = rc;
                    ndx++;
                    if (ndx >= numChars) {
                        ndx = numChars - 1;
                    }
                }else{
                    receivedChars[ndx] = '\0'; // terminate the string
                    recvInProgress = false;
                    ndx = 0;
                    newData = true;
                    
                    char * strtokIndx;
                    strtokIndx = strtok(receivedChars,":");
                    int section = atoi(strtokIndx);
                    strtokIndx = strtok(NULL, ":");
                    int action = atoi(strtokIndx);
                    strtokIndx = strtok(NULL, ":");
                    int value = atoi(strtokIndx);
    
                    do_something_with(section, action, value);
                }
            }else if (rc == startMarker) {
                recvInProgress = true;
            }
        }
    }

Now, how to replicate similar functionality in App Inventor, to make sure that the Clock timer will not split my text commands into parts?

App Inventor (clock at 100ms):


Example:

Arduino (this part fires often):

    my_function(){
      Serial.print(NUMBER);
      Serial.print(":");
      Serial.print(ANOTHER_NUMBER);
      Serial.print(":");
      Serial.println(YET_ANOTHER_NUMBER);
    }

And the test output in my android app:

    • 1:3:150
    • 1:3:150
    • 1:3:150
    • 1:3:1
    • 50
    • 1:3:150
    • ...

As you can see, the 4'th command has been split into parts. How do I prevent this?

Kindly,
Artur.

Better place to ask (on this forum) might be Interfacing w/ Software on the Computer.

But I guess this is more a question for an appinventor forum :wink: If it exists.

Yeah, you're right, this topic would fit better in the section you pointed (is there any MOD?) :slight_smile:
As for the Appinventor forum, most ppl tend to ask on google forums, which I don't really like :confused:

This may help Reliable Bluetooth Comms Between Arduino and MIT App Inventor (AI2) – Arduino++

Thanks! Looks complicated :smiley: but I'll try to understand and implement
I'll report if I succeed

Code for a project that uses those concepts is here GitHub - MajicDesigns/Chroniker: A LED Neopixel Analog Clock with multiple control interfaces (Bluetooth, IR Remote, tact switch) but that can be confusing for non-experienced programmers unless you know the underlying principles.

Thanks @marco_c :slight_smile: I got many helpful thoughts and insights from your article

I got it however working using a bit simplier design :slight_smile: There're holes but it's good enough for my needs

without getting too technical..

(on either side of things)..

you would use a character delimiter to signify the beginning of a packet and the end of a packet (string/data..etc)

I have only used App Inventor to create apps to SEND BT (serial) data to an Arduino... never from Arduino to BT device yet. (but the same theory applies on the Arduino side of things.. knowing when to start and stop collecting data for your 'packet'..

xl97:
without getting too technical..

(on either side of things)..

you would use a character delimiter to signify the beginning of a packet and the end of a packet (string/data..etc)

I have only used App Inventor to create apps to SEND BT (serial) data to an Arduino... never from Arduino to BT device yet. (but the same theory applies on the Arduino side of things.. knowing when to start and stop collecting data for your 'packet'..

@xl97 would you be so kind as to post a picture of your blocks for your app inventor project on the matter you stated?

I am trying to achieve the same outcome as philip, in my instance receiving text from an SD card instead.

I have made an app that sets the received_data to BytesAvailableToRecieve but all I get is a bunch of "-8 -128 -8 -128" etc........