lets say I have 8 integer values and I want to send these via Bluetooth to my PC, similar to the example code below.
// values that should be sent via Bluetooth
// initialization just for testing
val1 = val2 = val3= val4 = val5 = val6 = val7 = val8 = 200;
// initialize serial communication
// build Bluetooth package
String s = String(String(val1) + '|' + String(val2) + '|'
+ String(val3) + '|' + String(val4) + '|' + String(val5) + '|'
+ String(val6) + '|' + String(val7) + '|' + String(val8));
// send data via Bluetooth
When the package arrives on my PC, I want to split the string by the '|' characters because I need the values separated later. What is the most efficient way to do sth. like this? Build a string like in the code above and send the values altogether? Or is it better to send each value individually like in the code below?
if i would use the first variant, my string "s" would have sth. around 31 bytes (8 integer values with each 3 characters plus the 7 separators "|") each package. How do I know if my Bluetooth module can handle that amount of data? Is there a way to calculate that? I would like to use the Bluetooth module HC-05.
Thanks for your help in advance.
You can "build" the output on the fly like this - note that only the last item has println()
There is no need for delay()s between the instructions
There is no need for String(val3) in the print statements - I'm not even sure what it would do.
In general Strings (with a capital S) are a bad idea in the limited SRAM of an Arduino.
More efficient would be to send fixed length binary packets. For 8 integers, using a very simple packet structure, you could get the size down to 18 bytes.
If you think about it, sending the values 1000 to 8000 in 1000 steps would look, using your strings, like:
That's 40 bytes. Now, if you send it as binary, where each integer is exactly 2 bytes, and add a frame wrapper around it (say the ASCII characters STX and ETX), you then get 8*2 = 16 + 2 = 18 bytes. As hexadecimal, that would look like:
0x02 0x03 0x38 0x07 0xD0 0x0B 0xB8 0x0F 0xA0 0x13 0x88 0x17 0x70 0x1B 0x58 0x1F 1x40 0x03
You then receive your stream of characters and filter them through an 18 byte window. When the first character equals 0x02 and the last equals 0x03 you can be fairly certain you have a frame, and you can then decode the integers inside it.
You can make it more reliable by adding a checksum byte, so that you can be almost certain that you have a frame if the first and last character are 0x02 and 0x03, and the checksum calculates correctly.
Of course, if your values are only 0-255 then you only need one byte per value, which shrinks the packet even more.
Thank you very much majenkotech, I think this is the solution I was searching for!