I have data which is looking like this:
header:dataA:dataB
I am trying to get the header and the dataA and dataB as separate strings. So I wrote this function which probably is not that good:
#define maxDataSize 50
//.....
char header[20];
char dataA[maxDataSize];
char dataB[maxDataSize];
//**** a call to the function that parse the data
getHeader(BLEcontent,1,header); // here "BLEcontent" has the incoming data
getHeader(BLEcontent,2,dataA);
getHeader(BLEcontent,3,dataB);
//***** call the function that use the data
newDataFromBLE(header,dataA, dataB);
//function that parses the data:
void getHeader( char localString[], int seperatorNum,char *newdata)
{
const char seperator=':';
int counter=0;
int divider=0;
//clear array when it has garbage it added
for( int i = 0; i < maxDataSize; ++i )
newdata[i] = (char)0;
for(int k=0;k<maxDataSize;k++)
{
if ( localString[k]== seperator )
{
counter++;
divider=k+1;
if(counter==seperatorNum)
{ return ; }
}
if( (seperatorNum-1) ==counter)
newdata[k-divider]=localString[k];
}
return ;
}
Later when I'm trying to use the function data, stored in header/dataA/dataB:
void newDataFromBLE(char header[],char dataA[], char dataB[])
{
Serial.println("got:");
Serial.println(header);
Serial.println(dataA);
Serial.println(dataB);
if (strcmp (header,"setWifi") == 0)
//... do stuff here
I get here some very strange results. For example, if the incoming data is setWifi:a, I get a good result. If it is abcd:abc I get garbage. And if it is setWifi:a:b, I also get garbage.
It seems that if what I send is not corresponding to the first if statement, it will give me garbage even though it's BEFORE the if. Seems that it knows(??) about the upcoming if..
It used to work before..