Hi there-
I'm trying to figure out how I can match strings contained in http GET responses. For example, the Arduino requests a certain page and the webserver replies. If the body contains a certain string, a function is executed.
How can I do this with the included libraries?
Thanks!
cc3000 - How to read/match HTTP response body?
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- mdwilson562
- Posts: 10
- Joined: Tue Sep 24, 2013 11:09 am
- adafruit_support_mike
- Posts: 67446
- Joined: Thu Feb 11, 2010 2:51 pm
Re: cc3000 - How to read/match HTTP response body?
How many functions do you plan to have, and how strict do you want to be about making the GET request match?
The simplest way is simply to compare the strings is to use `strncmp()` from <stdio.h>.
The simplest way is simply to compare the strings is to use `strncmp()` from <stdio.h>.
- mdwilson562
- Posts: 10
- Joined: Tue Sep 24, 2013 11:09 am
Re: cc3000 - How to read/match HTTP response body?
Nothing too fancy yet, just trying to get a handle on this aspect. I'd like to match portions of the body against some known patterns and have the arduino react accordingly (for example, if the page contains the word "RED", the red LED lights up. Same for BLUE and GREEN.)
- mdwilson562
- Posts: 10
- Joined: Tue Sep 24, 2013 11:09 am
Re: cc3000 - How to read/match HTTP response body?
I came up with something that works more or less:
Of course, this is really inflexible... It's assuming a fixed length for the header, which looks like this:
From here I can do what I need to, more or less- I can just assign certain strings to certain positions and scan for them. It would be nice if I could do it in a more flexible way.
Code: Select all
while (client.connected()) {
while (client.available()) {
// Read answer
char c = client.read();
response += c;
}
Serial.print(response);
Serial.println(response.substring(297,308));
if (response.substring(297,308) == "HALLO WELT!") {
Serial.println("MATCH!");
}
response = "";
}
Code: Select all
HTTP/1.1 200 OK
Date: Sun, 13 Oct 2013 00:22:23 GMT
Server: Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8y DAV/2 PHP/5.5.3
Last-Modified: Sun, 13 Oct 2013 00:16:04 GMT
ETag: "c0e767-b-4e89441d31900"
Accept-Ranges: bytes
Content-Length: 11
Connection: close
Content-Type: text/plain
HALLO WELT!
- adafruit_support_mike
- Posts: 67446
- Joined: Thu Feb 11, 2010 2:51 pm
Re: cc3000 - How to read/match HTTP response body?
Ah.. you're using the Arduino to make GET requests, then trying to read the HTML you get in return?
That gets a big more complicated. For that, I'd suggest starting here: http://playground.arduino.cc/Code/TextFinder
That gets a big more complicated. For that, I'd suggest starting here: http://playground.arduino.cc/Code/TextFinder
Please be positive and constructive with your questions and comments.