I’m making something very similar to the next bus example https://docs.particle.io/tutorials/hardware-projects/maker-kit/#tutorial-2-next-bus-alert
Mine spins a servo if there’s a bus soon, and doesn’t have the LCD screen.
Getting the first upcoming bus works really well, but I can’t get the second upcoming bus. It returns this block of the XML instead of the ‘minutes’ number
rsion="1.0" encoding="utf-8" ?>
<body copyright="All data copyright Los Angeles Rail 2019.">
<predictions agencyTitle="Los Angeles Rail" routeTitle="Metro Red Line (802)" routeTag="802" stopTitle="Hollywood / Western Station" stopTag="80205_1">
<direction title="802 - North Hollywood Station">
<prediction epochTime="1556474667421" seconds="46" minutes="0" isDeparture=
I’m new to C coding, but I can’t see what I’ve done wrong here? I don’t think I’ve changed anything about this logic from the example code.
Can anyone shed light on what might be happening here? The problematic variable is ‘nextSoonestStr’
Thanks!
// This function will get called when NextBus webhook data comes in.
// It turns the full NextBus XML page into numbers to be displayed on the screen
void gotNextBusData(const char *name, const char *data) {
// put the incoming data (the XML page) into a string called "str"
String str = String(data);
// send str to the tryExtractString function, looking for the first instance (0) of "minutes=\""
soonestStr = tryExtractString(0, str, "minutes=\"", "\"");
// turn the extracted bus time into an integer and store it in soonestBusTime
soonestBusTime = soonestStr.toInt();
// send str to the tryExtractString function, looking for the second instance (1) of "minutes=\""
nextSoonestStr = tryExtractString(1, str, "minutes=\"", "\"");
// turn the extracted bus time into an integer and store it in nextSoonestBusTime
nextSoonestBusTime = nextSoonestStr.toInt();
//find out the next bus time in minutes in the console
Particle.publish("next bus =", String(soonestStr), PUBLIC);
Particle.publish("bus after next =", String(nextSoonestStr), PUBLIC);
//now everything is loaded, launch function to spin servo
turnServo();
}
// this function gets called by gotNextBusData to extract the bus times from the NextBus XML page
String tryExtractString(int matchNum, String str, const char* start, const char* end) {
if (str == "") {
return "";
}
int count = 0;
int lastIdx = 0;
while (count <= matchNum) {
int idx = str.indexOf(start, lastIdx);
int endIdx = str.indexOf(end, lastIdx + idx + strlen(start));
lastIdx = endIdx;
if (count == matchNum) {
return str.substring(idx + strlen(start), endIdx);
}
count++;
}
}