I'm using a HC-05 Bluetooth module in arduino to receive and send messages from/to AppInventor.
My App has a button to switch on/off a LED in arduino. After pressing the button, is waiting for an string acknowledge from Arduino. Arduino sends the string using println. For example:
bt_serial.println(String("ON"));
I'm using delimiter byte 10 in AppInventor Bluetooth client. Once I receive the string, I put the text in a debug label and the label shows "ON" .
Now, I want to compare the received string with "ON" before changing the image of the button, but the comparison never returns True. I don't understand why the comparison is not working.
These are the blocks that process the received data by the BluetoothClient:
Well, using the text comparison block might help, plus verbose testing (set both values to uppercase) but most programmers would not send a word - just a number. i.e. 1 = ON and 0 = OFF.
Also, send with print(), then send an empty println(). We have found that makes a difference (no logical reason).
If you are sending multiple times, the Clock timer should disable itself on receipt of the msg, otherwise it will continually return a negative result.
Oh, just noticed a fault in your Arduino code. It should be:
Hi, in addition to what @ABG and @ChrisWard have already said, I see that you use the boolean comparison (green blocks) instead of text comparison block (magenta).
Most probably this can foolish the comparison. Moreover sometimes it's better to avoid the comparison for equality but just look for the presence of the wanted string, something like:
in which, by converting whatever is received in uppercase, you also avoid that the comparison could fail due to a upper/lowercase mismatch between data.
Thanks for all the replies. Actually, I had tested with string comparation (magenta block) in first place, but it didn't work. I will try with all your valuable suggestions and let you know the result.
I've edited my previous post in which i've added a double test (as per your original code), so if the received message isn't OFF neither ON, it issues a warning.
Cheers.
EDIT Please also take care that the String function in Arduino creates a character array which is terminated by a Ctrl-Z (null character = 0x00) which is unprintable, and this is probably what is foolishing the comparison. So by applying the method that Chris has suggested, you should solve the problem or, also by using the "if contains", you should overcome the issue.
All the best !
Perfect !
But remember what @ChrisWard (and I) have said: the String function is not needed when you transmit already a string delimited by a couple of "; that is for example: "this is already a string".
Best wishes and have fun with AI2.