I'm developing on android with bluetooth LTE, I can send hex codes, but I send one code at time, so I need to send many informations in this way, how can I recognize a string and save the datas inside an array to parse it later?
Here is my sketch:
char c=' ';
String inString,one,check;
char inData[11];
char inChar;
int counter=0;
bool started = false;
bool ended = false;
byte index;
boolean NL = true;
const int LED1 = 6;
const int LED2 = 7;
const int LED3 = 2;
const int LED4 = 3;
const int LED5 = 4;
const int LED6 = 5;
void setup() {
Serial.begin(9600);
Serial.print("Sketch: "); Serial.println(__FILE__);
Serial.print("Uploaded: "); Serial.println(__DATE__);
Serial.println(" ");
BTserial.begin(9600);
Serial.println("BTserial started at 9600");
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
}
void loop() {
if (BTserial.available()>0)
{
Serial.print("bts: ");
Serial.println(BTserial.available());
c = BTserial.read();
check = String(c, HEX);
if(check=="ffbc"){
Serial.println("check");
index=0;
while (BTserial.available()>0){
inChar = BTserial.read();
check=String(inChar,HEX);
if(check != "0xfff3")
{
inData[index] = inChar;
index++;
}
}
for (int i=0;i<=11;i++){
one = String(inData[i], HEX);
Serial.print(i);Serial.print(": ");Serial.println(one);
};
}
if(check=="ffca"){
// Serial.write(c);
digitalWrite(LED1, !digitalRead(LED1));
}
if(check=="ffcb"){
//Serial.write(c);
digitalWrite(LED2, !digitalRead(LED2));
}
if(check=="ffcc"){
//Serial.write(c);
digitalWrite(LED3, !digitalRead(LED3));
}
if(check=="ffcd"){
//Serial.write(c);
digitalWrite(LED4, !digitalRead(LED4));
}
if(check=="ffce"){
// Serial.write(c);
digitalWrite(LED5, !digitalRead(LED5));
}
if(check=="ffcf"){
// Serial.write(c);
digitalWrite(LED6, !digitalRead(LED6));
}
}
}
Without the loop I can read all the single datas, but for my job it's not enough, to recognize the data applications I send a "sample string" to recognize the type of informations I want to send.
1 Answer 1
i think i solved in some way, i send always the same codes at the start and at the end of the datas, so i can racognize the end and then i parse the first element of the destination array. tell me what you think abou, if it could help someone else:
if (BTserial.available()>0)
{
c = BTserial.read();
check = String(c, HEX);
if(check!="fff3"){ // i always start wit an fff3
inData[index] = c;
index++;
}
else if(check=="fff3"){ //end of data
index=0; //reset the index for the next set of data
if(String(inData[1], HEX)=="ffbc"){
Serial.println("read the next 10 data of the array");
}else if(String(inData[1], HEX)=="11"){
Serial.println("turn on led 1");
}
// for more operations use a switch/case over the if
}
-
1Why are you using a string to check for the start/end data? You can simply use the hex representation:
if(c == 0xfff3)
. And I'm a bit confused, that you have a 4-digit hex code for one read operation. If you use the SoftwareSerial library as yourBTserial
object, a call of theread()
function returns one byte at a time, so it would be a 2-digit hex codechrisl– chrisl2018年02月04日 14:39:53 +00:00Commented Feb 4, 2018 at 14:39 -
hi, i'm using the bluetooth nativescript plugin, i don't know why but if i send a character after 9 arduino receives ffxx, (if i send 0xaa, it sends 0xffaa). when i made the first experiments i couldnt understnd the set of characters to parse, so i found the string. for the btserial i use altsoftserialpeppeg85– peppeg852018年02月05日 06:48:58 +00:00Commented Feb 5, 2018 at 6:48