user2756724
user2756724

Reputation:

Arduino --> Android bluetooth communication (receive text with App Inventor)

I'm creating an Arduino based drone that can be controlled through an Android application. In order to improve the user experience, I'd like to show the accelerometer/compass sensor's values on the application, so I need to send them from Arduino to Android, via Bluetooth. The values are simple integer number between 0 and 180.

The best solution I thought is to concatenate all the values (separated with a comma) in one string, and send it to the app, that will separate the single values (the string will be sent only when the app require it, in this case when a 'z' byte is received by Arduino).

if (Serial.available() > 0) {
    if (Serial.read()=='z'){
        Serial.println(String((int)sensor1) + ',' + String((int)sensor2) + ',' + String((int)sensor3));
    }  
 }

Here are the App Inventor blocks: App Inventor pseudo-code

It seems that the values are being received quite well, but there is a critical issue: somethimes the string is not received well, and that cause a lot of errors. Sometimes the received string is (for example) 10,10,10, but somethimes it is 10,10,1010 or just 10,10 ecc...

I also tried to send the values one by one, but the result was nearly the same. I even tried to set 'numberOfBytes' to -1, using a delimiter byte, but this also was not succesful unfortunately.

I getting quite mad, so I hope there is another way to send thoose integers to Android, or to fix the system I'm already using.

Upvotes: 2

Views: 16704

Answers (2)

Andy Droid
Andy Droid

Reputation: 11

I used Serial.print to send each result and then used Serial.write('>'); as the end marker.

In appinventor designer window set the Delimiter byte for Bluetooth client to 62 (the ASCII value for the > character ).

In the blocks window, use Bluetooth cliant1.Receive text and set number of bytes to -1

App invented will then read until a delimiter is found.

However it will cause the app to hang if it doesn't find one.

Upvotes: 1

Leandro
Leandro

Reputation: 1

the problem is that you are not signaling the end of the string I used his example on a project and was something like this:

while(Serial.available()>0){
  Serial.println(String((int)Sensor1) + ',' + String((int)Sensor2)+ ',');
}

If you compare the two codes the difference will be a " , " the most at the end of the print and it solved the problem for you sitad

Upvotes: 0

Related Questions