Receive different data for two text boxes

I have an Arduino with a DHT11 temperature and humidity sensor and a HC05 bluetooth module. I want to send the temperature value (which is a float) and the humidity value (which I changed as an int) to the app via bluetooth. The way I did it, the app doesn't recognize which is which and just mixes the humidity value with half of the temperature value for the temperature text box, while the other half of the temperature value goes in the humidity text box. It's my first time using both Arduino and App Inventor, please be gentle with me


This is the code block

This is the Arduino code

1 Like

Here is the standard advice:

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.

If you want to send your temperature and humidity on separate lines, you could alternatively send tag:value pairs, like
T:98.6\n
H:100\n
which would arrive individually if you use Delimiter 10 (\n).
Your AI2 logic would look like

if BT.bytesAvailable > 0 then
  set local message to BT.ReadText(-1)   (to get only 1 line)
  if contains(message, ":") then
      set local splits to split message at ":"
      If (select item 1 of splits) = "T" then
         set LabelTemperature.Text to (select item 2 of splits)
      else if (select item 1 of splits) = "H" then
         set LabelHumidity.Text to (select item 2 of splits)
     else set LabelWhatHappenned to local message
end if

Hello, thank you very much for the reply. I used the links and advices you provided (hopefully correctly) and the data receives okay. One problem I have though:

I modified the arduino code to send a "t" text and a "H" text before sending the temperature and humidity data to distinguish them. However now both "t" and "H" get printed alongside the correct data. Is there a way to fix this?

I send a screenshot from the phone app, new arduino code and new code blocks

The easiest way to remove a "t" is by using the text REPLACE block, replacing "t" with the empty text block.

Similarly for "H", in the branch that displays the Humidity.

You could also do it like this:
Change your Arduino code to:
Serial.println(t + "#" + h);

And change your blocks to this:

Hope that helps.
Mike.

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