Please, PLEASE help.
I'm losing my mind here.
I've got some programming experience (although I am currently rusty) but I am an Arduino and IoT beginner and know next to nothing about HTTP requests, so please be lenient with your responses. I've tried looking all over for a solution but have found nothing I can use.
I recently purchased the Adafruit CC3000 WiFi shield for my Arduino Uno in order to start building a project for my wife: a weather "station" where basically details about the weather pulled from a weather website will be displayed on the screen when she presses a button.
I have managed to successfully connect the shield and get it online and everything works fine. I even managed to get the information I needed from the GET request about the weather.
I modified the "WebClient" example from the shield's library to achieve this.
The trouble comes when I try to get the incoming chars that are read from the GET request (they are printed one by one) and get them into a String in order to be able to later parse that String and get the information I need.
I don't know if it's a timing issue with the request or if there is a character that somehow makes the concatenation of the char (converted to String) to my main String break, but the String that I end up with in my variable is not complete.
This is the part of the code in question (where the concatenation happens as the characters are read in). The String I want to concatenate everything to is dataprint and the character read from the server while the connection is open is c.
Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);
if (www.connected()) {
www.fastrprint(F("GET "));
www.fastrprint(url);
www.fastrprint(F(" HTTP/1.1\r\n"));
www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\r\n"));
www.fastrprint(F("\r\n"));
www.println();
} else {
Serial.println(F("Connection failed"));
return;
}
Serial.println(F("-------------------------------------"));
/* Read data until either the connection is closed, or the idle timeout is reached. */
unsigned long lastRead = millis();
while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
while (www.available()) {
char c = www.read();
dataprint.concat(String(c));
Serial.print(c);
lastRead = millis();
}
}
www.close();
In order to demonstrate the "breaking" that I am talking about, here is the HTTP GET request as printed on Serial when each individual char comes in:
HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Fri, 27 Feb 2015 22:13:13 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "06d218cfbd8a255b22db2786373c43d5"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: bff09e30-f16c-4010-b219-b3df63f39a3b
312
<div class="info city-fcast-text">
<h3>Today</h3>
<h4>Feb 28</h4>
<div class="icon i-14-m "></div>
<span class="cond">Partly sunny, showers around</span> <span class="hi-low">Hi</span> <strong class="temp">16<span>°</span></strong>
<span class="realfeel">RealFeel 15°</span> </div>
<!-- /.info -->
<a href="http://www.accuweather.com/en/gr/athens/182536/daily-weather-forecast/182536?day=1" class="bt-more">more</a>
<div class="overlay o-s"></div>
<div class="frame"></div>
0
...and here is how it looks when I do Serial.println(dataprint); :
HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Fri, 27 Feb 2015 22:13:13 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "06d218cfbd8a255b22db2786373c43d5"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: bff09e30-f16c-4010-b219-b3df63f39a3b
312
<div class="info city-fcast-text">
<h3>Today</h3>
<h4>Feb 28</h4>
<div class="icon i-14-m "></div>
//gibberish characters appear here that can't be copy-pasted
Does anyone know of a better way to do this? Can anyone please help me with what's wrong? I'm sure I have a lot of work ahead of me with parsing the information (since the actual request I want to make contains more information and is more complicated) but I'm already stuck here, so I could use someone's guidance to finally move on.
Thank you so much in advance. I attach the entire sketch for anyone who has the same shield I do and can test this. Please excuse the silly function names. A man's gotta do something to make things lighter when he's close to losing his mind.
WebClient_H.ino (5.45 KB)
