Arduino bluetooth and MIT app inventor 2

Hello,

I'm making an app for a smartphone using app inventor 2 and I'm using Arduino to send values coming from a Controller Area Network communication bus (from an electric bike) but the problem is that the bike sends values going from 0 to 100 in "byte" format...and for example if the arduino sends "40" app inventor sees it as one byte with 2 bytes in it so it doesn't really work, it keeps on giving me an error! So my question is how to split a number like 40 into two different bytes that I can send via bluetooth to the smartphone?

Thanks for your help.

app inventor sees it as one byte with 2 bytes in it

This makes no sense.

One thing you need to make sure is that the different ends of the comms are sending in the same format. A common error is sending the number 40 (one byte) as the ASCII characters '4' then '0'. This would happen, for example, if you Serial.print() the value. If you connect a terminal program instead of the receiver application and you can read the number on the screen , then you are reading ASCII characters. The Arduino can do a conversion of the ASCII characters into a number variable.

Also, posting your code would help others diagnose the problem.

Thanks for your answer. What I mean by that is that app inventor "reads" that it's going to receive one byte but actually receives 2, atleast that's what I understood by the error message on app inventor ("select list item: attempt to get item number 2 of a list of length 1")

I attached my arduino program to this message, and the code I'm using on app inventor is on this link: How To Receive Multiple Sensor Readings From Your ARDUINO Into Your MIT APP INVENTOR 2 APP | Z-hut_arduino

(Some of my arduino code has some french in it sorry)
The part that I'm trying to fix is the "bluetooth.print(...)" lines. There are 2 of thoses lines in my code but I don't know if I should send them as int or byte or maybe something else.

Thanks for your help.

programme_avec_bluetooth_FINALE.ino (2.84 KB)

Your Arduino code is sending values for speed and battery (voltage?) - 2 values. The AI2 code you show me is expecting to receive values for Temp, Humidity and Light. Aside from the fact the values have different meanings, don't you think that is you send 2 and expect to receive 3 this is going to be a problem?

You need to remove the AI2 set statement for list item 3, as this item is never sent.

Yes my arduino code is sending speed and battery (it's a percentage).
I modified that AI2 code to what I need it to do, so only show 2, I removed the third part from the code. I just used that guys app on AI2 as a base code.

Please then, post your actual code.

Here is just the part that is supposed to receive the data. The rest of the code is quite long and I don't know how to post it.

Data should be value-value. You are sending value-value-. This is probably causing split to fail on the last one.

Hum... I tried just sending value1-value2 but then when I send it a second time AI2 thinks that value2 and value1 are together...should I maybe put a higher delay? I never used AI2 so I don't know the speeds that it can receive and what kind of format it should receive data in (ASCII or byte or someting else...).

You are sending ASCII and it is expecting ASCII. As you are just putting it in the text label then it is consistent.

The code you use in AI2 is not great. For example, it does not check to see that it has the whole message before it process it and assumes if there are a few characters there that the whole message has been received. You know you have a new message when the end of line character is received.

BTooth is fast enough for your purposes and the AI2 code will only run as often as you have set the timer to check for characters, which you have not shown. I get totally acceptable results sending strings at 9600 bps and checking every 100ms, but I do process the message assuming that not all of it arrives together.

One thing I would do differently in the Arduino code is get both the values you need and then send them at the same time, without the additional CAN calls in between. This would make it more likely that they message gets there all at once and you know for sure that your message is formatted correctly.

Ok thanks for your advice, I just changed my arduino code so that both the values are sent at the same time.

As for AI2, I don't know how to check if it received the whole message before it process it. I also don't know how to change the timer to check for characters... and on arduino I send at 115200 bps...

Timer value is in the timer object, probably on your Screen.

I would suggest that you google a bit more around the Bluetooth on AI2 as there is other info out there you will find useful. Also try the tutorials and documentation for the objects used in AI2.

OK well thank you for your help!

This may also shed some light. A project I have just completed. Reliable Bluetooth Comms Between Arduino and MIT App Inventor (AI2) – Arduino++