Arduino and App Inventor Timing

I am sending data from an Arduino device via a Bluetooth module to an App Inventor app on my phone. Soon it will be Bluetooth to Bluetooth.
It's just a couple short lines of GPS data.
I want to send it every few seconds. Probably every ten seconds if I can.
Sometimes all of the incoming message on the phone gets printed out, and sometimes just a few characters.
I was wondering how to figure out what the phone application's clock should be set at.
Or is there another way to hold the data on the phone until another group of characters get sent?
Any help on this?
Thanks.

Please see the Delimiter article in FAQ

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

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
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 sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

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

2 Likes

Hello Tony and welcome to the forum.

I've been working on apps that receive GPS data and struck the same problem.

Rather than figure out the technique to have precise data transmission and reception, I went down the path of having the app filter out bad data packets.

In some cases the Arduino processes the GPS sentences and prepares a packet of 13 fields separated by commas. The app only processes the packet if it contains 13 fields.

In other cases the Arduino (or GPS without going thru an Arduino) sends the raw GPS output, which, for me, is six NMEA sentences. The app uses the "receive text" block with delimiter set to "-1" which makes it read till end of line. It then applies a number of tests to see if the packet is a full and correct sentence.

A better method would be to do a sum check on each packet, but I haven't looked into that. I've been told the extension to implement Linux commands provides a sum `check function.

Does that make sense to you? Are you sending raw GPS output?

My GPS runs at 1Hz and I made the app clock interval variable. Clock interval of zero works well.

(edit: I just read ABG's response. Their input has assisted me as well in the past.)

1 Like

Hi Tony.
Have you made any progress on this problem?

Still working on it.
Trying to get timing right. Read an article that says Receive speed should be 20% faster than Send speed. Will try that later.
Also probably forgetting to bother with receiving on phone, and will go arduino/BT to arduino/BT, which means pairing two modules. Doing the GPS thing for my kid and he doesn't have a smart phone. He's had this old phone 20 years and won't give it up.
But also having to change my bluetooth modules, been having problems with getting the HC-05 modules to get into Command mode. Still working on that. I have a couple Sparkfun BlueSMiRF that I had talking to each other, so going to set those up again.
Thanks for the help, though.
CU

Solved my timing problem sending from Arduino/Bluetooth to phone.
On my phone I have an App Inventor app that connects to the Arduino/Bluetooth module.
It has two clocks, one for the receiving of data, and one that transmits the character '1' to the Bluetooth/Android every 5 seconds.
When the android app receives the '1' character it sends the GPS data, which is only about 25 characters for testing, to the phone.
Now that I know that works fairly well, I'll pair two bluetooth modules.
Because my GPS module doesn't pick up the GPS signals in the house, I can then button up the GPS package and put it outside the house. Then I can work on getting what I need from the receiver GPS data in the house.
Good luck to me.
CU