Hi
Hope to find any help from you.
With App Inventor I readout via bluetooth the temp and humidity from a Arduino with sensor. It works fine. When I send a number, for example 21, to set a limit for the temp to switch on a relays, I get the following problem:
SerialMonitor is reading:
25;46
25;46
25;46
...
That is ok and it split it into 25 for temp and 46 for hum.
When I send 21 for example, I see on SerialMonitor:
25;46
25;46
225;46 (reading the first number only)!
25;46
125;46 (reading the second number only)!
25;46
...
Does anybody know why the number (21) is not receiving together?
(Sorry for my English).
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.
Did read and set the DelimiterByte to 10, and Serial.print to Serial.println in Arduino code. But it's the same effect when I print 18:
...
25
;
42
;
1 (first number of 18)
;
25
;
42
;
8 (second number of 18)
;
25
;
42
...
Well, I think this project is too difficult for me. Of course I can implement a static threshold in the Arduino code, no problem. But it would have been nice if I could set the switch-on temperature for the relay via the AppInventor. That way I would be flexible.
Unfortunately, I haven't found a tutorial for this yet.
"BytesAvallableToReceive" is a true/false flag.
You are also complicating the local variable initialization
Since the Clock Timer is effectively a loop, I would suggest using Global variables, then they are initialized only once, but that is minor.
... and here is how the Bluetooth related blocks should look on Screen Initialization. Also note that the ErrorOccurred Block is very useful. I have also included the Activity Starter method of enabling Bluetooth.
Yes, have seen, thanks!
I've been in front of the screen for hours. I have to take a break, my eyes are getting tired. I'll get back to you soon with the results.
Have a nice weekend....
Somehow I can't get your code to work. But thank you very much for your effort.
I now have the code so far that it works for me. I can control the setpoint temperature up and down. This works. But now I get the following error:
This is the standard advice for handling line delimiters in BlueTooth:
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.
Thank you very much for pointing that out. I had actually overlooked that, sorry. Now everything works as it should, thanks to all for your patient and professional help! I was able to learn a lot again...!