I am struggling with sending data to multiple labels

Good day. I am currently busy with creating an app that has a button that is pressed on and off. I want to be able to also write data to this app, but the different data must be shown on different labels. Here is how my code looks for the app and my code for the ESP32. The problem is that both the 50 and 60 gets written to the same label1 and 60 overwrites 50, instead of writing 50 to label 1 and 60 to label2.



Hi. I am currently making an app that has a button but I also want to be able to display Characters in labels. At the moment I can only manage to write numbers to my app and when I try writing a character I get an error in my code. The snippet of the code I have provided works with the numbers but as soon as I say ESP_BT.write("K"




) , it does not work

Hello Yani

Your screenshots do not help us to help you - in future, right mouse click in the Blocks work area and select:

download blocks

Your ESP32 script can simply be uploaded as a file.

Apart from "Yani's App" your Script is not sending any characters (C String) to the App (the Script is not correct, ESP_BT.begin will not send anything?). I can't tell from the Blocks you have uploaded if you have properly accommodated the receipt of strings in the App. It doesn't look like you have. Note that, if strings (Text) are to be received, it is best to receive numerical values as strings too. App Inventor understands the difference.

Hi. Sorry about that. I did not know about not screenshotting. I have included the photo down below. I don't completely understand your response. I am very new to MIT app inventor so I am not sure how to change it to be able to receive characters. At the bottom of my code I use the ESP_BT.write() to send numbers but that does not work with the characters.

Still can't see your ESP_BT.write() but your App is set to only receive signed Byte Numbers.

Good day. I made an app that is a button and I also want to be able to write different values to different labels on my app. At the moment I can only write and display data in one label. I am using an ESP32 to write code to my app. My code is attached below.

If you use blocks that does not have any value so it does not change or if changed it got empty value,

hence you need to set text in the label to show in the app.

Time to learn how to use BlueTooth text delimiters.

Use print() instead of write() for BT

(canned Reply)
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.

...

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.