Thanks for your help everyone
Can "2021-03-05T02:15:00" fit in an array of 10 chars ?
That's a very good point. Typo on my part. I've increased it to 20, to give room to spare. Thought that could have been the reason, writing beyond the size of my char and corrupting memory but it still behaves the same
A good habit to get into when debugging array crashes is to put a Serial.flush () after every print.
Also, why are braces { } escaped in your delimiters? " "\",\{[\}:");
I've now put flushes after my Serial statements. That's a new thing to me.
I marked curly brackets as delimiters because they were being extracted as tokens. As I don't need them, treating them as delimiters meant they did not get extracted.
Also, if you've got two sets of delimiters, give them names.
As in create meaningful constants for reuse and readability?
Are you aware that strtok() destroys the original string, by putting NULLs in place of the tokens, returning pointers to the appropriate parts of the original memory?
I had read that. However that does not matter too much to me. Once I've looped through the tokens and got my tide information, the JSON string should be refreshed the next time I need tide details.
I've put the flushes in and sized tideTime appropriately but it still fails in the same way.
char array[] = "[{\"EventType\":\"LowWater\",\"DateTime\":\"2021-03-05T02:15:00\",\"IsApproximateTime\":false,\"Height\":1.3632871362992067,\"IsApproximateHeight\":false,\"Filtered\":false,\"Date\":\"2021-03-05T00:00:00\"},{\"EventType\":\"HighWater\",\"DateTime\":\"2021-03-05T08:25:00\",\"IsApproximateTime\":false,\"Height\":5.2168696552371623,\"IsApproximateHeight\":false,\"Filtered\":false,\"Date\":\"2021-03-05T00:00:00\"},{\"EventType\":\"LowWater\",\"DateTime\":\"2021-03-05T14:25:00\",\"IsApproximateTime\":false,\"Height\":1.8097719968974781,\"IsApproximateHeight\":false,\"Filtered\":false,\"Date\":\"2021-03-05T00:00:00\",{\"EventType\":\"HighWater\",\"DateTime\":\"2021-03-05T20:47:00\",\"IsApproximateTime\":false,\"Height\":5.3284583370257206,\"IsApproximateHeight\":false,\"Filtered\":false,\"Date\":\"2021-03-05T00:00:00\"";
char *ptr = NULL;
char tideType[9] = "";
char tideTime[20] = "";
float tideHeight = 0;
void setup()
{
Serial.begin(9600);
delay(500);
Serial.println("About to start Get Tide Details");
Serial.flush();
getTideDetails();
delay(500);
Serial.println("getTideDetails has completed");
Serial.flush();
Serial.print("Tide type is ");
Serial.flush();
Serial.println(tideType); //As soon as I try to output tideType it seems too restart
Serial.flush();
//Serial.print("Tide time is ");
//Serial.println(tideTime);
Serial.print("Tide Height is set at ");
Serial.println(tideHeight,4);
}
void getTideDetails(){
Serial.println("Starting Get Tide Details");
Serial.flush();
ptr = strtok(array, "\",\{[\}"); // takes a list of delimiters
while (ptr != NULL)
{
//Serial.println(ptr);
if (strcmp(ptr, "EventType") == 0) { //Identified tag called EventType
ptr = strtok(NULL, "\",\{[\}:"); //Put the pointer to the next tag
strncpy(tideType, ptr, strlen(ptr)); //copy low or high water across
tideType[strlen(ptr)] = '\0'; //terminate it to avoid corruption
} else if (strcmp(ptr, "DateTime") == 0) { //Identified tage called DateTime
ptr = strtok(NULL, "\",\{[\}"); //skip a tag two avoid unwanted colon
ptr = strtok(NULL, "\",\{[\}"); //Pointer is now at the start of the time element
strncpy(tideTime, ptr, strlen(ptr)); //copy it across to tide time string
tideTime[strlen(ptr)] = '\0'; //terminate it to avoid corruption
} else if (strcmp(ptr, "Height") == 0){ //Identified tag called Height
ptr = strtok(NULL, "\",\{[\}:"); //Pointer is now at the start of the tide height tag
tideHeight = atof(ptr); //Convert to float and assign.
}
ptr = strtok(NULL, "\",\{[\}"); // Move to the next tag
}
}
void loop()
{
// put your main code here, to run repeatedly:
}
Any further ideas?