Skip to first unread message

Matt Z

unread,
Feb 24, 2019, 3:33:53 PM2/24/19
to MIT App Inventor Forum

I'm struggling with text coming from an arduino over bluetooth. I'm working on what should be a simple BT interface to an arduino sketch that's been around for a while and isn't my creation. I don't have the option of making changes to it.
I'm trying to place the text that comes in response to a command sent to the arduino into a label. An example is that by sending the text :00# to the arduino it responds with the position of a stepper motor in the format P1234# where P is the beginning of the string, # is the end, and 1234 is the position. The position can be anything from 1 to 2000000 so the number of digits can vary from 1 to 6. Always starting with the P and ending with the #.

In order for the counter to not lose track of its position I perform a check to see if the motor is currently stepping before telling it to move. Right now I just have a test label for that response. By sending :01# to the arduino it responds with I01# (that's a capital "I") if the motor is currently moving, and I00# if it is not. This should be easier since the response will be one of two options but it isn't working out that way.
What's happening is I'm getting half the step position string in the "is moving" label, and sometimes parts of the "is moving" response is showing up in  the position label. I can't find a way to reliably grab everything between the P and # and put it in the position label, or a way to positively identify IXX# as the response to the :01# command.

The bluetooth connection is stable and movement commands sent to arduino are executed reliably, although I'm looking at the motor to see if it's already moving before sending the command instead of checking the status via bluetooth.

Any advice would be greatly appreciated. The two sections giving me fits are below. I don't think that's the correct use of the "contains text piece" block but documentation and examples of it are scarce.



ABG

unread,
Feb 25, 2019, 10:20:23 AM2/25/19
to MIT App Inventor Forum
Your app need message delimiters:

Use println() to separate messages when you transmit.
Set Delimiter = 10 in BlueTooth Designer.
Check for Bytes Available > 0 in blocks,
but then request -1 bytes to get only full  messages.

also, expect a time delay between when you send a message from AI2
and when the reply will arrive from BlueTooth.

See the BlueTooth and Arduino section of FAQ
for samples.

ABG

Matt Z

unread,
Feb 27, 2019, 6:50:59 AM2/27/19
to MIT App Inventor Forum
Thanks ABG, that did the trick. Although as I mentioned I can't make changes to the arduino sketch, it's already using # as a delimiter. Putting # in as the delimiter in the bluetooth client properties made the difference. I had not seen that mentioned anywhere else other than in your first link.
Reply all
Reply to author
Forward
0 new messages