Successful HTTP Request - Now to parse the results..?

Searched for a while last night but was unable to find an answer.

I’m building a device that can represent upcoming Chicago train times via WS2812 LED’s.
For example: LED 1 means 1 minute away, LED 2 means 2 minutes away, etc.

The Chicago train system API outputs XML, which I’ve converted via PHP to a simple string in which each space-separated number represents the amount of time until a train arrives.
Example string:
"4 10 16 " --> http://johnventions.com/trains/trains.php

I’ve successfully pulled that string of numbers in using the HTTPClient Library. I verified that it works properly by publishing the resulting response to the Dashboard, but I’m unsure of how to proceed from here.

Code Snippet:


  void loop() {
    if (nextTime > millis()) {
        return;
    }
    
    // Set up or url path
    request.hostname = "www.johnventions.com";
    request.port = 80;
    request.path = "/trains/trains.php?trip=30274";

    // Get request for train times
    http.get(request, response, headers);

    //set up a character array to copy the response to
    char result[] = "";
    int c = strlen( response.body );
    
    //copy the response to the results character array
    for (int i=0; i<c; i++) {
        result[i] = response.body[i];
    }
    
    //for debugging, publish the result to the dashboard to see if HTTP request works
    if (c > 0) {
        Particle.publish("jhCTA",result);
    } else {
        Particle.publish("jhCTA","error");
    }

    //here is where I would process the resulting data into an int array...
    
    //repeat again in 20 seconds
    nextTime = millis() + 20000;
}

Now that I have a resulting character array with the times in them. What is the best way to separate those values into a array of integers? i.e.

trains[] = [4, 10, 16]

I know the problem is rooted in my lack of understanding in C data types. If this were javascript or php, I’d simply convert it to a string, and split the array at every free space character. Any advice or links to read?
Thanks!

What keeps you from doing the same here?
This might help. This was useful to me as well, should do what you're looking for:

2 Likes

Thanks for the links @Moors7 - that looks like it should be the solution to what I’m struggling with. I’ll try it out and let you know if it works.

1 Like