Based on some guidance received here, I've previously approached this problem using serial.find plus parse.int, BUT now I realize that solution doesn't work if I am looking for two different targets...So allow me to restate the problem:
I send a command to a bluetooth LE module, and receive back a serial response. For example, this:
OK+DISISOK+DISC:00000000:00000000000000000000000000000000:0000000
000:B8782E07067C:-074OK+DISC:4C000215:2F234454CF6D4A0FADF2
F4911BA9FFA6:00000001AC:0CF3EE041CCE:-046OK+DISC:65000337:2RG37784CF6D4A0FAD
F2F5688BA9FFA9:00000001AC:0DS3EW041BNE:-082OK+DISCE
(the response is one long string; in the above example, I split it into several lines and added color so it is easier to read in this forum)
In this example, the module has discovered 3 different bluetooth LE beacons in the room:
B8782E07067C with rssi value of -74 (the integer that follows directly after B8782E07067C)
0CF3EE041CCE with rssi value of -46
0DS3EW041BNE with rssi value of -82
I want to scan the response and store the rssi values for specific target beacons B8782E07067C and 0DS3EW041BNE but ignore everything else.
Complicating factors/requirements/helpful points:
-
I want to use an array and not use a String (capital S).
-
There is no terminating character on the data received from the module. But, it always ends with "OK+DISCE", so maybe that's good news?
-
The data received from the module is NOT of fixed length. For example, if there were 5 beacons in the room, the data response string would be longer. If there were 2 beacons, shorter. The module has no way of telling me in advance how many beacons are in the room. In other words, the length of the data received from the module can not be determined in advance. The order of the beacons in the data string is also variable.
-
The full data string always starts with "OK+DISIS". Eight characters.
-
Each beacon's info in the string is proceeded by "OK+DISC:", which in effect means, "hey, I discovered a beacon and here's its 70 character info".
-
The length of each beacon's substring data is always exactly 70 characters long, not including the OK+DISC: at the beginning. If you include the OK+DISC:, then each beacon's data substring is 78 characters long.
-
Summary of total data string received:
-always starts with same 8 characters, "OK+DISIS"
-each beacon's info substring always starts with same 8 characters, "OK+DISC:"
-each beacon's info substring contains 70 characters, not including the "OK+DISC:"
-each beacon's info substring contains the important identifying 12 character ID number from characters 54 to 65
-after the 12 character ID number of each beacon, there is a colon, then the negative integer that I want to store is located in characters 67 to 70 -
To clarify bit more, here's the same example data string, but I have broken it down and added notes to make it easier to understand:
8 characters: OK+DISIS
78 characters with ID number from characters 54 to 65 and rssi number from characters 67 to 70: OK+DISC:00000000:00000000000000000000000000000000:0000000000:B8782E07067C:-074
78 characters with ID number from characters 54 to 65 and rssi number from characters 67 to 70: OK+DISC:4C000215:2F234454CF6D4A0FADF2F4911BA9FFA6:00000001AC:0CF3EE041CCE:-046
78 characters with ID number from characters 54 to 65 and rssi number from characters 67 to 70: OK+DISC:65000337:2RG37784CF6D4A0FADF2F5688BA9FFA9:00000001AC:0DS3EW041BNE:-082
...maybe one or two more 78 character substrings here for other beacons in the room...
8 characters: OK+DISCE
Sample pseudo code:
void loop()
{
// read the input from the Bluetooth LE 4.0 scanner module
if (millis() - timestamp1 > 500) //Time delay between scans for beacons is 0.5 seconds; this prevents the program from sending too many requests for scan and overwhelming the Bluetooth LE 4.0 scanner module
{
BluetoothScanner.write("AT-DISI?"); // ask HM-10 to scan for bluetooth LE 4.0 beacons
timestamp1 = millis(); // reset the countdown timer for delay between scans
}
if (BluetoothScanner.available()) // If there is a message from the HM-10, then do what follows in brackets
{
...Scan the incoming message from bluetooth module to see if it contains the target beacons, B8782E07067C and 0DS3EW041BNE; if it does, then capture the integers following each and store those values in variables...
Serial.print("The rssi for B8782E07067C: ");
Serial.println(rssiTarget1);
Serial.print("The rssi for 0DS3EW041BNE: ");
Serial.println(rssiTarget2);
}
}
My rough idea is something like:
-Data string comes in from the module
-Count total characters in string to figure out number of beacons discovered, e.g. 328 characters received means 4 beacons in the room (8 characters at start, 8 characters at end, and 4 sets of 78 characters in between).
-Make a separate array for each of these sets of 78 characters
-Check characters # 54 to 65 for each array to see if it is one of the target beacons in which I'm interested
-If so, store the target ID number and its associated rssi integer (located from characters 67 to 70)
-Do something with those rssi integers
-Clear out all this info
-Loop again
Sorry for the wordy description. Thanks in advance for any ideas!