Bluetooth Conflict: Unable to Use Label and Button Simultaneously in MIT App Inventor with Arduino

Hello! I am working with a smartbox that has an Arduino inside, equipped with buttons with LEDs. In MIT App Inventor, I want the possible error codes to appear in "Label 3". Additionally, when I press a physical button on the box, the corresponding button in the app should change its color as shown below. The problem is that if I delete one of the bluetooth blocks (Reloj 2 or Reloj 3), the other works correctly. For example, if I remove the block for the label, the buttons work perfectly, and vice versa, if I remove the block for the buttons, the label works. But when I leave both blocks at the same time as shown in the picture, neither works. The code is too extensive to attach, but I understand that it's not a problem with the code itself since they work correctly when separated. I hope someone can help me.

Multiple Clocks grabbing everything in the BlueTooth buffer is inviting conflicts.

Better to just have one fast Clock reading the buffer, and since you only want 1 letter at a time, just ask for one byte, then interpret it according to notes you left for yourself in a global variable when you requested the BT response.

1 Like

Thank you for your suggestion. However, I need more than one byte because the error messages I want to display in Label 3 are composed of 6 bytes. I'm a bit new to this and not sure how to implement your advice. I tried combining both blocks into a single Clock, but only the command for the Label worked, not the one for the buttons. Any additional guidance would be greatly appreciated.

Maybe it's time to start using a message Delimiter (Line Feed)?

Stock 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.

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.

Some people send temperature and humidity in separate messages with distinctive prefixes like "t:" (for temperature) and "h:" (for humidity).
(That's YAML format.)

The AI2 Charts component can recognize these and graph them. See Bluetooth Client Polling Rate - #12 by ABG

To receive YAML format messages, test if the incoming message contains ':' . If true, split it at ':' into a list variable, and find the prefix in item 1 and the value in item 2.

...