how to transmit a float variable to APP via bluetooth?

1,550 views
Skip to first unread message

Steven Chung

unread,
Jul 28, 2016, 2:26:27 AM7/28/16
to MIT App Inventor Forum

dear sir:

    In my application, I need to transmit a float variable from MCU to APP by
    
Bluetooth Module. For MCU, float variable is  IEEE 754 32-Bit format, and I

convert it to four 1-byte variables as follows:

 union
{  float var;
   unsigned char var_byte[4];
} my_format;

Then the four bytes are transmitted to APP via bluetooth module, but I do not

know how to combine these bytes data to get original float variable. 

Could any one tell me how to do this?    THANKS!   

Ghica

unread,
Jul 28, 2016, 8:52:28 AM7/28/16
to MIT App Inventor Forum
It is just a lot easier to send this as a string and let App Inventor figure out how to interpret this as float (which it will do automatically).
You could also try to use receiveUnsigned4ByteNumber.
Cheers, Ghica.

Abraham Getzler

unread,
Jul 28, 2016, 12:02:10 PM7/28/16
to MIT App Inventor Forum

Ghica

unread,
Jul 28, 2016, 12:16:36 PM7/28/16
to MIT App Inventor Forum
ABG: ???
Cheers, Ghica.

Abraham Getzler

unread,
Jul 28, 2016, 12:49:38 PM7/28/16
to MIT App Inventor Forum
I posted that link in case the original poster did not know there was a way to format and transmit different data types as text.

Since I don't know the programming environment on his transmitting side,
I used a shotgun approach on my guess for what language construct to search for.

I vaguely remember the keyword has "print", with maybe an "s" or "f" added to it somewhere.

ABG

Abraham Getzler

unread,
Aug 1, 2016, 11:05:10 PM8/1/16
to MIT App Inventor Forum
OP replied by email ...
dear Sir:

   Thanks for your answer. But in my application, the MCU IDE does not support any function to convert float number to string. I can convert the float variable to 4-Bytes data in MCU program , and using  receiveUnsigned4ByteNumber to receive these data in APP. The transmission is OK, but APP threat these data as a 32-bit integer! Is there any method to restore this 32-bit integer to original float value? Thanks!


 

Ghica

unread,
Aug 2, 2016, 2:38:46 PM8/2/16
to mitappinv...@googlegroups.com
The mathematical way would be as follows:
Assume you have a four byte number b1b2b3b4. If you now have received these as four separate bytes: b1, b2, b3, b4, you can calculate the resulting 32 byte number:
((b1x256 + b2)x256 + b3)x256 + b4
Cheers, Ghica.

Abraham Getzler

unread,
Aug 2, 2016, 4:24:29 PM8/2/16
to MIT App Inventor Forum
IEEE 754 32-Bit format sent raw needs to be broken down and rebuilt,
extracting a 1 bit sign, an 8 bit exponent, and  a 23 bit mantissa.

There are some binary conversion blocks that might help, for positive numbers.

This will be pretty easy if the numbers are positive.
Is that the case?

ABG


Abraham Getzler

unread,
Aug 2, 2016, 4:31:16 PM8/2/16
to MIT App Inventor Forum
Could you identify the radix of the IEEE 754 32-Bit format,which is it, base 10 or base 2?
I see both possibilities mentioned in the Wikipedia article.
ABG

Ghica

unread,
Aug 2, 2016, 4:33:26 PM8/2/16
to MIT App Inventor Forum
I Googled on MCU IDE and it seems that you program it in C. So why would you not be able to send a string representation of the number?
There is print, printf, etc.
Cheers, Ghica

Abraham Getzler

unread,
Aug 4, 2016, 10:31:47 PM8/4/16
to MIT App Inventor Forum
(replied via email...)
 
Dear Sir:
Yes, it's a positive number with base 10. THANKS`~

 Could you send some test data thru your floating point number for me?

What comes out of your AI2 Bluetooth receive block if your original floating point number was ...

128. ?

64. ?

1. ?

I need test data to know if my conversion routine works with your data.

ABG



Abraham Getzler

unread,
Aug 4, 2016, 10:34:09 PM8/4/16
to MIT App Inventor Forum
Also, please show us the block(s) you are using to request those 4 bytes .

ABG

Abraham Getzler

unread,
Aug 6, 2016, 9:32:29 PM8/6/16
to MIT App Inventor Forum
(Replied by mail again.)

steven steve...@gmail.com

3:43 AM (17 hours ago)
to me
Dear Sir:

Thanks!
1.0  --->  0x3F 80 00 00  
2.0  --->  0x40 00 00 00
64.0 -->  0x42 80 00 00
128.0->  0x43 00 00 00
1.25 -->  0x3F A0 00 00
1.5 ---->  0x3F C0 00 00
5.5 ---->  0x40 B0 00 00

And the Block as follow with Low Byte First for BT.ReceivedUnsigned4ByteNumber:
內置圖片 1

Abraham Getzler

unread,
Aug 6, 2016, 9:39:44 PM8/6/16
to MIT App Inventor Forum
See attached for a step by step process according to 
that matches your test data.

The internal exponent uses a base 2 , as evidenced by your test values.

I leave it to you to incorporate this code into your routine,
and to verify that your Blue Tooth connection actually works
the way you think it does.

I used lots of labels, to show each step of the way.

Also, though this was fun, you should really take @Ghica's advice and learn to use printf formats.

ABG


5_5.png
128.png
blocks.png
Designer.png
IEEE754.aia
1_5.png

Abraham Getzler

unread,
Feb 20, 2018, 11:03:40 AM2/20/18
to MIT App Inventor Forum
A correction to my code allowing for zero and negative exponents and mantissas
has been posted here ...

Thank you, TFO!

ABG

Reply all
Reply to author
Forward
0 new messages