I am trying to send an SMS that contains multiple values as a String, which then will be received by the Arduino board as a Serial.read(); command. For example, if i where to send the following SMS to the board, "Username PhoneNumber Email". The code will then receive the SMS and divides it into different String values, which i can then use in the code.
So far i have been able to do all of that, the issue that i am currently facing, is that i can seem to receive the text message as a String value, which means that the board is not receiving it as a String, instead it is receiving it as char[].
in result, i am getting the following output when i try to print out the value,
> 1: U
2:
3:
1: s
2:
3:
1: e
2:
3:
1: r
2:
3:
1: n
2:
3:
1: ame
2: PhoneNu
3:
1: mber
2: Email
3:
what i want is to get this output,
1:UserName
2:PhoneNumber
3:Email
The following is the code i am using:
void setup()
{
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0)
{
char string[100];
char byteRead;
int availableBytes = Serial.available();
for(int i=0; i<availableBytes; i++)
{
string[i] = Serial.read();
string[i+1] = '\0'; // Append a null
}
String dj = string;
//char inchar=Serial.read();
String split= dj;
String word1 = getValue(split, ' ', 0);
Serial.println("1: "+word1);
String word2 = getValue(split, ' ', 1);
Serial.println("2: "+word2);
String word3 = getValue(split, ' ', 2);
Serial.println("3: "+word3);
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
I want the userName to go into String 1, PhoneNumber into String 2 and Email into String 3.
Thank you!! :D