I need some help with parsing some returned webhook weather data.
I tried to follow @LukeUSMC parsing method above but it needs some tweaking for my application it looks like.
So by following Luke’s example I created a custom response template to pull in only the webhook data that I’m interested in. Here is the custom response code:
"responseTemplate": "{{#weather}}{{id}}~{{main}}~{{description}}~{{/weather}}{{main.temp_min}}~{{main.temp_max}}~{{main.humidity}}~{{wind.speed}}~{{wind.deg}}~{{sys.sunrise}}~{{sys.sunset}}~{{name}}"
This provides me with the following GET response:
{"data":"\"responseTemplate\": \"800~Clear~clear sky~78.8~87.1~24~3.36~200~1460977185~1461025619~Fishers\",","ttl":"60","published_at":"2016-04-18T22:02:18.622Z","coreid":"1a002d000347343339373536","name":"hook-response/FishersWeather/0"}
So the webhook is working just fine and it was very easy to setup which is all great news.
Now I need to parse this weather data into their own seperate variables so I can use it as needed and to do that I’m trying to use Lukes code below but I need to modify it.
I changed this code some attempting to handle the larger amount of returned data. I bumped the buffer size up to 500 from 125.
Basically I’m expecting this function below to parse the data that is in between
// This function will get called when weather data comes in
void gotWeatherData(const char *name, const char *data) {
Serial.print("Running gotweatherData function");
String str = String(data);
char strBuffer[500] = "";
str.toCharArray(strBuffer, 500); // example: "\"21~99~75~0~22~98~77~20~23~97~74~10~24~94~72~10~\""
int weathercode = atoi(strtok(strBuffer, "\"~"));
int maxtempday1 = atoi(strtok(NULL, "~"));
int mintempday1 = atoi(strtok(NULL, "~"));
int maxwindday1 = atoi(strtok(NULL, "~"));
int forecastday2 = atoi(strtok(NULL, "~"));
int maxtempday2 = atoi(strtok(NULL, "~"));
int mintempday2 = atoi(strtok(NULL, "~"));
int maxwindday2 = atoi(strtok(NULL, "~"));
int forecastday3 = atoi(strtok(NULL, "~"));
int maxtempday3 = atoi(strtok(NULL, "~"));
int mintempday3 = atoi(strtok(NULL, "~"));
int maxwindday3 = atoi(strtok(NULL, "~"));
Serial.println("Weaher Code");
Serial.println(weathercode);
Serial.println(maxtempday1);
Serial.println(maxtempday1);
Serial.println(maxwindday1);
Serial.println(maxtempday2);
Serial.println(mintempday2);
}
The serial output from the function above looks like this. I’m just testing to see if the data between the ~ & ~ signs get put into the variables but only a few get parsed for some unknown reason. I know the weather description and main are text character data so they probably shouldn’t be placed in INT holders. I tried changing them to char data types but that didn’t help.
@ScruffR So I guess my first question is if this is the most efficient way of parsing the webhook response or not. If it is then can you give me any hints about what needs to be changed?
All help is appreciated