Need help with returning a value to AI2

Hello all

I am using AI2 for the first time. The app connects to a Bluetooth device, and I want it so that Button1 sends the letter M when I click it (this works), and then displays the value that is returned in Label3 (this does not work, nor does it display the test text "Received".

I am assuming the part where I call BluetoothClien1.ReceiveText needs to be in a clock, but I can't figure out how to use two clocks simultaneously.

All help appreciated

https://ibb.co/gP1Z5HD

(Canned Reply: ABG- Export & Upload .aia)
Export your .aia file and upload it here.
export_and_upload_aia

1 Like

Your image, for those of us who don't like image sites:

The problem is indeed asking for text in the same event where you sent the 'M'.

In the Button Click, just keep

if connected then
send text 'M'

Move everything else including another copy of the if connected test to the Clock Timer event.

Also, spend a penny and get some more Labels, instead of reusing Label3 for everything.
Otherwise you will see only the last update to it.

Think of your Bluetooth signal as a water pipe - water can only travel in one direction at a time.

You can't send and receive in the same instance of time, so separate those - use a Clock Timer for receive and set it to at least 1 second (1000 milliseconds).

Give your components meaningful names
https://www.professorcad.co.uk/appinventortips#TipsGui

Thanks very much for that input. I couldn't work out how to attach an image but you gave an explanation that I will use next time.

I am used to procedural programming rather than event based, so I didn't think to include the ReceiveText into the Timer block. I will give that a go later today.

All the Label3 stuff is me just putting in words so I can test / see what is happening, but I take your point.

I am very much enjoying this learning journey, especially at my ripe old age haha.

Thanks again

1 Like

Thanks again for your help, but I am clearly misunderstanding what you are suggesting I need to do.

I tried putting the ReceiveText into the existing Clock1 block. That didn't work. I created a second Clock1 block, but Ai2 complained about having two handlers. So I created a second Clock (Clock2) and put the ReceiveText in there, but the program just hangs.

Where did I go wrong? (Hopefully I have managed at least to upload an image of my project this time :slight_smile: )

Tanko2.aia (279.5 KB)

Hold the phone! I got it working! Yay!

Thanks for everyone's help and expect more help requests from me soon!

1 Like

Boy, am I finding this frustrating. I just can't get this clock/BT stuff sorted propertly.

When I disable the Clock2 block that receives the text from the Arduino, I can use the Button (MeasureButton) and it sends the appropriate text (M) to the Arduino. The Arduino responds and sends some text back, but clearly MeasureStatusLabel does not update as it is in a disabled block.

When I enable the Clock2 block, the MeasureButton does not even fire, i.e. MeasureStatusLabel does not update to Sent and the Arduino does not receive anything.

I am so confused by this and have been trying for hours and hours to work this out.

Any ideas?

Tanko2 (1).aia (279.6 KB)

Be sure your sketch is using println() to send its measurements.

Here is code that would work with println():


Tanko21 (1).aia (279.6 KB)

Standard advice:
Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

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.

Thank you for taking the time to put that together ABG.

It is almost perfect, except that I kept getting a "Not connected to Bluetooth" error whenever I started the app.

So solve this I changed your block above to look like this, and it seems to work perfectly!

How to integrate the whole thing into the actual tank measuring device proper. Exciting times!

Thanks again

1 Like