Thank you everyone for the help! I found the solution that I wanted it is from this site. The code for breaking up a single string into different variables are as follows:
if(Serial.available()){
String rxString = "";
String strArr[2]; //Set the size of the array to equal the number of values you will be receiveing.
//Keep looping until there is something in the buffer.
while (Serial.available()) {
//Delay to allow byte to arrive in input buffer.
delay(2);
//Read a single character from the buffer.
char ch = Serial.read();
//Append that single character to a string.
rxString+= ch;
}
int stringStart = 0;
int arrayIndex = 0;
for (int i=0; i < rxString.length(); i++){
//Get character and check if it's our "special" character.
if(rxString.charAt(i) == ','){
//Clear previous values from array.
strArr[arrayIndex] = "";
//Save substring into array.
strArr[arrayIndex] = rxString.substring(stringStart, i);
//Set new string starting point.
stringStart = (i+1);
arrayIndex++;
}
}
//Put values from the array into the variables.
String value1 = strArr[0];
String value2 = strArr[1];
//Convert string to int if you need it.
int intValue1 = value1.toInt();
}
I studied and changed the code to like this:
String value1,value2,value3,value4,value5;
char ch;
int aqi,co;
float temp,hum;
if(Serial2.available()){
String rxString = "";
String strArr[4]; //Set the size of the array to equal the number of values you will be receiveing.
//Keep looping until there is something in the buffer.
while (Serial2.available()) {
//Delay to allow byte to arrive in input buffer.
delay(2);
//Read a single character from the buffer.
ch = Serial2.read();
//Append that single character to a string.
rxString+= ch;
}
int stringStart = 0;
int arrayIndex = 0;
for (int i=0; i < rxString.length(); i++){
//Get character and check if it's our "special" character.
if(rxString.charAt(i) == ','){
//Clear previous values from array.
strArr[arrayIndex] = "";
//Save substring into array.
strArr[arrayIndex] = rxString.substring(stringStart, i);
//Set new string starting point.
stringStart = (i+1);
arrayIndex++;
}
}
//Put values from the array into the variables.
value1 = strArr[0];
value2 = strArr[1];
value3 = strArr[2];
value4 = strArr[3];
value5 = strArr[4];
//Convert string to int if you need it.
aqi = value1.toInt();
co = value2.toInt();
temp = value3.toFloat();
hum = value4.toFloat();
I am still a bit new to this. The solution provided by gcjr and J-M-L doesnt seem to work for me. Moreover, I am not familiar with the code, I dont even know like 80% of the code even does lol. Therefore, I found the next solution. I think that this solution is better for begineers like me and the code is easier to understand, I manged to tweak them by myself.
Anyways I hope it helps other people in the future! I am not a native English speaker, hopefully you guys can understand my english. Thank you very much!