To parse xml files it's usually better to consider the '>' and '/n' as the end of line, so you don't need a larger buffer, since each xml file line are usually very big.
I use something similar to get some weather information. Here is an ideia.
void weather() {
byte idx = 0;
char character;
int timeout = 0;
char icon[12] = {};
char response[64] = {};
if (tcp.connect("site", TCP_PORT) == 1) {
while (tcp.connected()) {
while (tcp.available()) {
timeout = 0;
character = tcp.read();
if (idx < sizeof(response) - 1) {
response[idx++] = character;
}
if (character == '\n' || character == '>') {
if (strstr(response, "temperature") && strstr(response, "value")) {
sscanf(strstr(response, "value"), "value=\"%hhd\"", &temp);
//process
tcp.stop();
}
}
memset(response, 0, sizeof(response));
idx = 0;
}
if (timeout++ >= 1000) {
tcp.stop();
}
delay(1);
}
}
tcp.stop();
}
Can you please help me with your example for the code I have posted. Especially the functions strstr,sscanf and memset. How do I use them in a program for arduino?
Arank:
To parse xml files it's usually better to consider the '>' and '/n' as the end of line, so you don't need a larger buffer, since each xml file line are usually very big.
I use something similar to get some weather information. Here is an ideia.
void weather() {
byte idx = 0;
char character;
int timeout = 0;
char icon[12] = {};
char response[64] = {};
if (tcp.connect("site", TCP_PORT) == 1) {
while (tcp.connected()) {
while (tcp.available()) {
timeout = 0;
character = tcp.read();
if (idx < sizeof(response) - 1) {
response[idx++] = character;
}
if (character == '\n' || character == '>') {
if (strstr(response, "temperature") && strstr(response, "value")) {
sscanf(strstr(response, "value"), "value="%hhd"", &temp);
//process
tcp.stop();
}
}
memset(response, 0, sizeof(response));
idx = 0;
}
if (timeout++ >= 1000) {
tcp.stop();
}
delay(1);
}
}
tcp.stop();
}
Yes the data will be present between 'text'> and where the value 456 is present in the given example. The HTTP reply will remain same always , however the data can change and can be either of integer or float( signed or unsigned) datatype.
I think you could then use the function strcspn() to find the start of the standard preceding text and find the number by counting forward from that position.
Hi, I am using the web client example for arduino to obtain data from a xml source.
The HTTP response is obtained in the variable char c. I am trying to convert the characters obtained in the response to a string using the variable char a[] using the code given below. But I am unable to do so. Please help if something is wrong with the following piece of code.
if(client.available()) {
char c = client.read();
a[i]=c;
i=i+1;
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
i=0;
client.stop();
// Serial.println(i);
// do nothing forevermore:
while (true);
}