To parse out data received, I use "SourceStr.substring(Start, End - Start)" but get nothing even close to expected results. Made a test script to parse three different strings. One with numbers and commas, substring get close but shorts the substring by 3 characters. One with letters and commas, substring get close but shorts the substring by 3 characters. One with any keyboard character, the substring looks like most of the original string.
Below is the script being run on a ESP32 Deneyap Mini v2 development board. Also tried it with the same results on a ESP32 LOLIN S2 Mini
/*
* To test SourceStr.substring(Start, End - Start)
*
*/
//String PanTilt = "a,b,c,d,e,f,g,h,i,j,k,m";
//String PanTilt = "1,2,/n/r3,4/n/r,5,6,7,8,9,10,11,12";
String PanTilt = "POST / HTTP/1.1\r\nHost: 192.168.0.3\r\nConnection: Keep-Alive\r\nContent-Length: 24\r\n\r\nPanTilt_Az,numSteps_100;\r\n";
String numSteps;
int Count = 0;
void setup() {
Serial.begin(9600);
delay(10);
}
void loop() {
while(Count<4){
//numSteps = getBetween(PanTilt, "b", "f");
//numSteps = getBetween(PanTilt, "2", ",6");
numSteps = getBetween(PanTilt, "PanTilt_", ",numSteps_");
Serial.print("numSteps= ");
Serial.println(numSteps);
delay(2000);
Count = Count +1;
}
}
String getBetween(String SourceStr, String strStart, String strEnd) {
int Start = 0;
int End = 0;
//String SourceStr = strSource;
Serial.print("SourceStr= ");
Serial.println(SourceStr);
Serial.print("SourceStr.indexOf(strStart)= ");
Serial.println(SourceStr.indexOf(strStart));
Serial.print("SourceStr.indexOf(strEnd)= ");
Serial.println(SourceStr.indexOf(strEnd));
Serial.print("SourceStr.length()= ");
Serial.println(SourceStr.length());
if (SourceStr.indexOf(strStart) > -1 && SourceStr.indexOf(strEnd) > -1) {
Start = SourceStr.indexOf(strStart, 0) + strStart.length();
End = SourceStr.indexOf(strEnd, Start);
Serial.print("Start= ");
Serial.print(Start);
Serial.print(", End= ");
Serial.println(End);
Serial.println(SourceStr.substring(Start, End - Start));
return SourceStr.substring(Start, End - Start);
} else {
return "";
}
}
The following is the serial monitor output for one pass of the loop
08:09:16.892 -> SourceStr= POST / HTTP/1.1
08:09:16.892 -> Host: 192.168.0.3
08:09:16.892 -> Connection: Keep-Alive
08:09:16.892 -> Content-Length: 24
08:09:16.892 ->
08:09:16.892 -> PanTilt_Az,numSteps_100;
08:09:16.892 ->
08:09:16.892 -> SourceStr.indexOf(strStart)= 82
08:09:16.892 -> SourceStr.indexOf(strEnd)= 92
08:09:16.892 -> SourceStr.length()= 108
08:09:16.892 -> Start= 90, End= 92
08:09:16.892 -> ST / HTTP/1.1
08:09:16.892 -> Host: 192.168.0.3
08:09:16.892 -> Connection: Keep-Alive
08:09:16.892 -> Content-Length: 24
08:09:16.892 ->
08:09:16.892 -> PanTilt_
08:09:16.892 -> numSteps= ST / HTTP/1.1
08:09:16.892 -> Host: 192.168.0.3
08:09:16.892 -> Connection: Keep-Alive
08:09:16.892 -> Content-Length: 24
08:09:16.892 ->
08:09:16.892 -> PanTilt_
Wrote C# console code in VS2022 to be similar to the ESP32 script and it parse all three strings correctly.
Any help would be appreciated.