How to display temperature readings provided by Arduino sketch?

I'm experienced with Arduino and electonicas in general, but still very green with MIT App Inventor.

I've tried to find a good example or tutorial that might give me some clues on right way to display termperature readings taken by an Arduino and sent over bluetooth to my mobile running the App Inventor code.
I have bluetooth working and created a simple MIT App that successfully controls a LED on/off state on the Arduino from my mobile.(from Tutorial by "How-To-Mechatronics")
I would appreciate suggestions for the right "blocks" to display the temperature, which is already available as a float value in the arduino sketch, the same sketch that communicates over bluetooth classic to my Android phone.
thanks in advance ...

See the community discussions about using arduino/bluetooth/App Inventor perhaps

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.

...

1 Like

Dear @ninja2. you can take a look here:
http://kio4.com/appinventor/9B_bluetooth_arduino_basico.htm
The linked site is courtesy of @Juan_Antonio.
Though it's in Spanish language I believe that you can easily get useful hints from it.
Cheers.

1 Like

"to avoid taking a long walk on a short pier" :slightly_smiling_face:

many good clues here thanks @ABG. I'll come back to this once I get basic operations working.

Gracias @uskiara (and @Juan_Antonio). With a liitle helpo from Google translate I was able to run the random number generator sketch on my Arduino (a Teensy 4.1 from PJRC) and see the numbers via bluetooth on my Android running p9B_2_bluetooth_basico.aia

As an exercise I went to create my own English version of the p9B_2_bluetooth_basico App , but I could not find the "when ... do" block types? (see the snippet attached)

Where can I find these "when...do" blocks?

The When and Before events belong to a list picker.

Dear @ninja2,
(since I'm Italian, Spanish is not so difficult for me :grimacing:) but I agree that with Google translator more or less everything can be translated. Moreover also @ChrisWard's web site (professorcad.co.uk) has a huge amount of Arduino+BT examples in English.
Anyway, the best way to proceed is to make just one step at a time: once you are done with the BT Tx&Rx, then you can go on by coding the rest of your app.
Best wishes.

1 Like

I've made good progress, please see my new blocks arrangement attached.

I have an issue that is probably easy for you all to fix (if only I knew how !) :

The Arduino is constantly reading a temperature and sending it once / seconds over bluetooth serial. I am received the correct temperature reading in "global degC1" but the value won't sit still in the "Label" box on the mobile display. For example if the temperature is 12.34°C only three digits will be displayed, and it cycles through "12.3" then "4 12" (split over two lines) then ".34", and the cycles repeats forever...

How do I get this to sit still in the display?

(Note I have set the delimiter value to 10 in the properties tab)

One other issue: when the App is started it reports
"Error 515: Not connected to a Bluetooth device"
before I have a chance to select the right BT device in the list picker.
Even after selecting and connecting the error 515 message persists on the screen, for maybe 30 second before dissappearing.

How can I avoid this error?

TIA

Nice to read that something (though not everything) is working.
The error 515 probably is caused by the clock1 that is always running.
I don't see in your picture of blocks the screen1.initialize one: at designer level the clock1 shall be disabled and also the firing in background shall be disabled (both checkboxes shall be unticked),
Then you shall enable the clock1 only after the successful first connection, otherwise the clock1 tries to receive something on the BT line immediately at the starting of the app, but the BT isn't working yet.
As far as the numeric data receiving issue is concerned, I can suggest a couple of things: the first is to use the receive text block, instead of the receive bytes. The text terminator 10 (0x0A) works better if you use the receive text. In addition to this, if you receive 4 bytes, please be aware that each character received is a byte, therefore also the decimal point (the dot) consumes a byte. In other words: 12.3 is composed by four characters, including the dot and if you transmit 12.34 the fourth byte (the cypher 4) is lost.
In a nuthshell: use the ReceiveText block and use the -1 as length (and terminate the sending from Arduino with a .println(); ). In such a way you will live a easier life... :grin:
All the best.

1 Like