Dear Sir:
I have a question. Is anyone can help me to solve it? I am receiving data from bluethooth connection. I want to plot the data in the canvas. The data from bluetooth is the string variable, but I want to change it to the number varialbe. Therefore, I can plot the variable on the canvas. How can I do it. Thanks very much.
文本其实可以直接做乘法的,ai 会自动把文本当成数字。你这个错误是因为你获取的这个不是数字。看起来像三个数字中间带空格。
您好:
謝謝您的回覆, 藍芽傳回來的不是數字格式, 而是String格式。所以想把它改成數字格式,才可以繪圖。還有其它方法可以進行嗎? 感謝!
Dir Sir,
Thanks for your suggestion, but it did not work. Do you have another suggestion? Many thanks!
My post was not a suggestion, it just corrected how your App receives Bytes.
"BytesAvailableToReceive" is a flag, it is not the actual number of bytes ready to be received. To get all of the available bytes, use math value -1 as I marked-up your image.
String to Digital: App Inventor should normally accept a String Number as a Digital Number. However, if that is not happening and the String is clean (only consists of numbers), you can do this:
Since you are receiving only one value, why not receive it as a number (short/integer/float)?
Dear Sir,
Thanks for your reply. I have tried your method, but the bluetooth connection just did not receive any data. In fact, I am going to receive several numbers from my arduino sensors, and therefore, I use string numbers (..,...,...,). After that, I will cut the strings numbers into individual numbers, and then plot them on the canvas. My first step in try one number, and then try multiple numbers. Many thanks!
That is the method (-1). If no data was received, there is an error in either the app code or the microcontroller code. However, as stated before, App Inventor does not require a conversion from type string-number to type number. If the string-number fails, it will be because it contains something other than a number, such as a space or a hidden character such as a line-feed.
Here is an example Project that should help you get some of the details right.
BT_Receive_4_Vals.aia (8.9 KB)
Note that time duration is an important aspect. Firstly, the values should be sent to the App with the longest practical time interval. The App time interval must be at least 20% shorter than the microcontroller (Arduino) time interval to finish the processing of the current data set before the next one arrives.
Note also that the data packet size is 23bytes, of which a max 20bytes are available for your data, so if the values are floats, restrict the number of decimal places before sending.
Since you are using sensors, do not use delay() in your Sketch, instead manage the loop time interval with elapsed milliseconds - this will help ensure the sensor values collected are true (all values are only as good as the quality of the sensors).
I don't see any sketch in this thread, wher println() should be used to separate text messages.
So I am posting my standard BlueTooth Delimiter 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.
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.
Dear Sir:
With your help, I have soloved my problem. Now I can draw two dataline via bluetooth. I really appreciate your help.
Dear Sir:
With your help, I have soloved my problem. Now I can draw two dataline via bluetooth. I really appreciate your help.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.